Node Cli Starter

Create a Node CLI very quickly


While working on this site I wanted an easy way to upload books to my books section without having to make an admin section for the blog. The easiest solution was to make a program that I could run on the command line to create a book entry and deploy the site.

I found this article on scotch.io for making a CLI utility:
https://scotch.io/tutorials/build-an-interactive-command-line-application-with-nodejs

It's pretty cool, but I wanted to make a simple CLI without any packages.
I also wanted to avoid callback hell 🔥

So by using async/await and nothing other than packages included in Node I was able to create a simple cli to upload a book review.

you can clone the starter repo here:
https://github.com/danownsthisspace/node-cli-starter

The biggest trick to it is the async prompt function which allows you to wait for user input before Node executes the next line of code.

So rather than this:

  
    prompt('Do you enjoy callback hell?:', answer => {
      if (answer == 'yes') {
        prompt('WHY!?', reason => {
          console.log('Answer to insanity=', reason)
        })
      } else {
        console.log('Neither do I')
      }
    })
  

You can do this:

  
    const answer = await prompt('Do you enjoy callback hell?:')

    if (answer == 'yes') {
        const reason = prompt('WHY!?')
        console.log('Answer to insanity=', reason)
      } else {
        console.log('Neither do I')
    }

  

There isn't much more to it than that :)