Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leebyron/interactive-script
Easy to write interactive scripts
https://github.com/leebyron/interactive-script
Last synced: 4 months ago
JSON representation
Easy to write interactive scripts
- Host: GitHub
- URL: https://github.com/leebyron/interactive-script
- Owner: leebyron
- Created: 2016-08-11T06:21:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-11T06:46:54.000Z (over 8 years ago)
- Last Synced: 2024-08-29T18:36:32.558Z (5 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 51
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
interactive-script
==================Need to write a simple interactive terminal script in node? Dealing with [readline](https://nodejs.org/api/readline.html) can be overwhelming and result in [callback hell](http://callbackhell.com/).
There's a simpler way that easier to write!
To get started, we need to install a few dependencies:
```bash
npm install -g interactive-script async-to-gen minimist
```Now let's write `myScript.js`:
```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](https://github.com/tc39/ecmascript-asyncawait)
which is not yet available out of the box in node.js. However you can install
and use [async-node](https://github.com/leebyron/async-to-gen) to take
advantage today.Let's run this script:
```bash
$> async-node myScript.js --pirate
Avast, me hearty!
Whats ye name? Lee
HI THERE: LEE
$>
```# Tastes great with:
[minimist](https://github.com/substack/minimist): Read the arguments provided to
your script from the terminal.[colors](https://github.com/marak/colors.js/): Ask and say things in a rainbow
of colors for better legibility.