0

我有这个节点 cli 脚本

#!/usr/bin/env node
const path = require('path');
const forever = require('forever-monitor');
const script = path.format({dir: __dirname, base: 'pager.js'});
const chalk = require('chalk');
const commander = require('commander');

commander.version('1.0.0')
.option('-m, --message', 'set awesome message')
.parse();

const args = commander.opts();

const header = `
+---------------------+  
| Awesome v1.0 |
+---------------------+
`;

const child = new (forever.Monitor)(script, {
    max: 2,
    silent: false,
    args: args
});

child.start();

child.on('start', (process) => {
    console.log(chalk.magenta.bold(header));
});

child.on('restart', () => {
    console.log(`Forever restarting script for ${child.times} time`);
});

child.on('exit:code', (code) => {
    console.log(`Forever detected script exited with code ${code}`);
});

我想集成指挥官,让用户能够传递将被解析的参数,然后传递给正在运行的子进程,直到终端在永远监控npm 包的帮助下关闭。目前我尝试在子进程中使用指挥官但没有成功,它将被忽略。然后我将它移到我的index.js代码中,但我不知道如何将参数传递给子进程。目前,如果我将解析的参数传递给args永远监视器的选项,我会收到此错误

host:awesome dev$ node . -m 'Hello!'
/Users/dev/Desktop/awesome/node_modules/forever-monitor/lib/forever-monitor/monitor.js:130
    this.args.unshift(script);
              ^

TypeError: this.args.unshift is not a function
    at new exports.Monitor (/Users/dev/Desktop/awesome/node_modules/forever-monitor/lib/forever-monitor/monitor.js:130:15)
    at Object.<anonymous> (/Users/dev/Desktop/awesome/index.js:20:15)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

谁能给我一些帮助?

4

1 回答 1

1

永远监视器:args传递给的配置选项forever-monitor需要一个数组。

'args':    ['foo','bar']

Commander:program.opts()返回一个常规的 javascript 对象,它是一个散列,键是选项名称。program.args是已解析的命令参数数组,其中已删除可识别的选项和选项值。

在 Commander 中,还可以查看.allowUnknownOption()passThroughOptions()Commander 允许在命令行上出现无法识别的选项。默认行为是为无法识别的选项显示错误。

于 2021-02-22T01:18:14.320 回答