1

这涉及 node.js 的 vorpal CLI,如下所示:

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

用户可以使用Ctrl-关闭 vorpal C,但是如何以编程方式关闭 vorpal?

例如,如果打开了 vorpal 终端会话,但 25 秒后没有收到进一步的标准输入,我想以编程方式关闭 vorpal,我的代码如下所示:

  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();
    }
  });
4

2 回答 2

3

虽然 Vorpal 没有关闭功能,但您可以使用或以exit编程方式运行本机命令。您可能也对隐藏 Vorpal 感兴趣。execexecSynchide

代码:

vorpal.exec("exit");
于 2017-11-06T01:09:23.017 回答
1

这有什么问题?

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)
于 2018-09-26T14:52:29.480 回答