Skip to content

Latest commit

 

History

History
57 lines (39 loc) · 1.53 KB

README.md

File metadata and controls

57 lines (39 loc) · 1.53 KB

interactive-script

Need to write a simple interactive terminal script in node? Dealing with readline can be overwhelming and result in callback hell.

There's a simpler way that easier to write!

To get started, we need to install a few dependencies:

npm install -g interactive-script async-to-gen minimist

Now let's write myScript.js:

var interactiveScript = require('interactive-script');
var minimist = require('minimist');

interactiveScript(async (say, ask) => {
  const { pirate } = minimist(process.argv.slice(2))

  say(pirate ? 'Avast, me hearty!' : 'Hello there')

  const name = await ask(pirate ? 'Whats ye name? ' : 'Who are you? ')

  say('HI THERE: ' + name.toUpperCase())
})

You're given two functions:

  • Print to the screen with say.
  • Prompt and wait for a response with ask.

You may have noticed that this script uses an async function which is not yet available out of the box in node.js. However you can install and use async-node to take advantage today.

Let's run this script:

$> async-node myScript.js --pirate
Avast, me hearty!
Whats ye name? Lee
HI THERE: LEE
$>

Tastes great with:

minimist: Read the arguments provided to your script from the terminal.

colors: Ask and say things in a rainbow of colors for better legibility.