1

This regards the vorpal CLI for node.js, like so:

const vorpal = require('vorpal')();

The user can close vorpal with Ctrl-C, but how can I close vorpal programmatically?

For example, if a vorpal terminal session is opened, but no further stdin is received after 25 seconds, I want to programmatically close vorpal, here is what my code looks like:

  vorpal
  .delimiter(shortCWD + chalk.magenta(' / suman>'))
  .show();

  const to = setTimeout(function () {
    vorpal.close();  // >>>  I want to programmatically close vorpal...but vorpal.close() is not a function
    process.stdin.end();
    log.error('No stdin was received after 25 seconds..closing...');
    p.killAllImmediately();
    process.exit(0);
  }, 25000);   // if no stdin has been received after 25 seconds

  process.stdin
  .setEncoding('utf8')
  .resume()
  .on('data', function customOnData(data: string) {
    clearTimeout(to);
    if (String(data) === 'q') {
      log.warning('killing all active workers.');
      p.killAllActiveWorkers();
    }
  });
evelynhathaway
  • 1,670
  • 14
  • 18
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

2 Answers2

3

While Vorpal doesn't have a close function, you can run the native exit command programmatically using exec or execSync. You may also be interested in hide for just hiding Vorpal.

Code:

vorpal.exec("exit");
evelynhathaway
  • 1,670
  • 14
  • 18
  • thanks, is this any better than just running `process.exit()`, do you think? just want to make sure stuff gets cleaned up properly. – Alexander Mills Nov 06 '17 at 01:18
  • 1
    @AlexanderMills I'd guess so, but possibly only slightly if you're only using Vorpal and this timeout function. Using a Vorpal command seems cleaner as well. – evelynhathaway Nov 06 '17 at 01:20
1

What's wrong with this?

const Vantage = require('vantage')

const v = Vantage()
    .delimiter('$test')
    .listen(9000)
    .show()

const close = () => {
    v.exec('exit', () => {
        console.log('DONE')
    })
}  
setTimeout(close, 1000)
Error: UI Prompt called when already mid prompt.
    at Object.prompt (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/ui.js:131:13)
    at Vorpal.vorpal.prompt (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:450:8)
    at Vorpal.vorpal.exit (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:1160:12)
    at Session.<anonymous> (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal-commons.js:49:19)
    at Vorpal.vorpal._exec (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:846:18)
    at Vorpal.vorpal._execQueueItem (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:594:10)
    at Vorpal.vorpal._queueHandler (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:579:10)
    at Vorpal.vorpal.exec (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:554:24)
    at Timeout.close [as _onTimeout] (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/xxx.js:9:7)
    at ontimeout (timers.js:380:14)
Aaron Bell
  • 852
  • 2
  • 11
  • 26
Giggioz
  • 457
  • 7
  • 21