Nodejs - 将指挥官参数传递给永远监控子进程
Nodejs - passing commander arguments to forever-monitor child process
我有这个节点 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}`);
});
我想集成 commander togive the user the ability to pass arguments that will be parsed and then passed to the child process that is running until terminal is closed with the help of forever-monitor npm 包。目前我尝试在子进程中使用 commander 但没有成功,它将被忽略。然后我将它移到我的 index.js
代码中,但我不知道如何将参数传递给子进程。目前,如果我将解析的参数传递给 forever monitor
的 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
谁能帮我一下?
forever-monitor:传递给 forever-monitor
的 args
配置选项需要一个数组。
'args': ['foo','bar']
指挥官:program.opts()
returns 一个常规 javascript 对象,它是一个散列,键是选项名称。 program.args
是已解析的命令参数的数组,其中已识别的选项和选项值已删除。
在 Commander 中,还请查看 .allowUnknownOption()
和 passThroughOptions()
,以便 Commander 允许在命令行上使用无法识别的选项。默认行为是显示无法识别的选项的错误。
我有这个节点 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}`);
});
我想集成 commander togive the user the ability to pass arguments that will be parsed and then passed to the child process that is running until terminal is closed with the help of forever-monitor npm 包。目前我尝试在子进程中使用 commander 但没有成功,它将被忽略。然后我将它移到我的 index.js
代码中,但我不知道如何将参数传递给子进程。目前,如果我将解析的参数传递给 forever monitor
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
谁能帮我一下?
forever-monitor:传递给 forever-monitor
的 args
配置选项需要一个数组。
'args': ['foo','bar']
指挥官:program.opts()
returns 一个常规 javascript 对象,它是一个散列,键是选项名称。 program.args
是已解析的命令参数的数组,其中已识别的选项和选项值已删除。
在 Commander 中,还请查看 .allowUnknownOption()
和 passThroughOptions()
,以便 Commander 允许在命令行上使用无法识别的选项。默认行为是显示无法识别的选项的错误。