这关系到node.js的vorpal CLI,如下所示:
const vorpal = require('vorpal')();
用户可以使用 Ctrl - C 关闭vorpal,但是如何以编程方式关闭vorpal?
例如,如果打开了一个vorpal终端会话,但是在25秒后没有收到进一步的stdin,我想以编程方式关闭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();
}
});
答案 0 :(得分:2)
虽然Vorpal没有关闭功能,但您可以使用exec
或execSync
以编程方式运行本机exit
命令。你可能也对hide
感兴趣只是隐藏了Vorpal。
<强>代码:强>
vorpal.exec("exit");
答案 1 :(得分:1)
这怎么了?
const Vantage = require('vantage')
const v = Vantage()
.delimiter('$test')
.listen(9000)
.show()
const close = () => {
v.exec('exit', () => {
console.log('DONE')
})
}
setTimeout(close, 1000)
错误:已经在中间提示时调用了UI Prompt。 在Object.prompt(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/ui.js:131:13) 在Vorpal.vorpal.prompt(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:450:8) 在Vorpal.vorpal.exit(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:1160:12) 在会议上。 (/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal-commons.js:49:19) 在Vorpal.vorpal._exec(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:846:18) 在Vorpal.vorpal._execQueueItem(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:594:10) 在Vorpal.vorpal._queueHandler(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:579:10) 在Vorpal.vorpal.exec(/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/node_modules/vorpal/lib/vorpal.js:554:24) 在Timeout.close [作为_onTimeout](/Users/giggioz/Spaghetti/keeper/keeper-giochipiu-delegates/xxx.js:9:7) 在ontimeout(timers.js:380:14)