andywer / ideabox

Place to collect techy ideas and get feedback.
1 stars 1 forks source link

CLI interaction using generators #5

Closed andywer closed 7 years ago

andywer commented 7 years ago
import newTool, { print, prompt, sleep, spinner, waitFor } from 'new-tool'
import fs from 'mz/fs'

newTool(cli())

function *cli () {
  yield spinner('Idling a little')
  yield sleep(1000)     // just some timeout so you can see the spinner in action

  const name = yield prompt('What is your name?')
  yield print(`Hi ${name}!`)

  yield spinner('Reading package.json')
  // Can wait for promises to resolve:
  const packageContents = yield waitFor(fs.readFile('package.json', { encoding: 'utf-8' }))
  const packageMeta = JSON.parse(packageContents)

  yield print(`Welcome to ${packageMeta.name}.`)
}

Making it beautiful using https://github.com/sindresorhus/ora:

screenshot

andywer commented 7 years ago

Promise-based approach: https://github.com/leebyron/interactive-script

andywer commented 7 years ago

Closing this, since my idea might not be that good... ;)