在package.json中,我有两个脚本:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
}
每次开始在Node.js中开发时,我都必须并行运行这两个脚本。我首先想到的是添加第三个脚本,如下所示:
"dev": "npm run start-watch && npm run wp-server"
…但这将在运行wp服务器之前等待开始监视完成。
如何并行运行这些?请记住,我需要查看这些命令的输出。此外,如果您的解决方案涉及构建工具,我宁愿使用gulf而不是gulf,因为我已经在另一个项目中使用了它。
分叉怎么样
运行多个节点脚本的另一种选择是使用单个节点脚本,它可以派生许多其他脚本。在Node中本机支持分叉,因此它不添加依赖项,并且是跨平台的。
最小示例
这将只是按原样运行脚本,并假设它们位于父脚本的目录中。
// fork-minimal.js - run with: node fork-minimal.js
const childProcess = require('child_process');
let scripts = ['some-script.js', 'some-other-script.js'];
scripts.forEach(script => childProcess.fork(script));
详细示例
这将使用参数运行脚本,并通过许多可用选项进行配置。
// fork-verbose.js - run with: node fork-verbose.js
const childProcess = require('child_process');
let scripts = [
{
path: 'some-script.js',
args: ['-some_arg', '/some_other_arg'],
options: {cwd: './', env: {NODE_ENV: 'development'}}
},
{
path: 'some-other-script.js',
args: ['-another_arg', '/yet_other_arg'],
options: {cwd: '/some/where/else', env: {NODE_ENV: 'development'}}
}
];
let runningScripts= [];
scripts.forEach(script => {
let runningScript = childProcess.fork(script.path, script.args, script.options);
// Optionally attach event listeners to the script
runningScript.on('close', () => console.log('Time to die...'))
runningScripts.push(runningScript); // Keep a reference to the script for later use
});
与分叉脚本通信
分叉还有一个额外的好处,即父脚本可以从分叉的子进程接收事件并发回。一个常见的例子是父脚本杀死其分叉的子脚本。
runningScripts.forEach(runningScript => runningScript.kill());
有关更多可用事件和方法,请参阅ChildProcess文档
我有一个没有任何额外模块的跨平台解决方案。我正在寻找一个可以在cmd.exe和bash中使用的try-catch块。
解决方案是command1||command2,它似乎在两种环境中都一样。因此,OP的解决方案是:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
// first command is for the cmd.exe, second one is for the bash
"dev": "(start npm run start-watch && start npm run wp-server) || (npm run start-watch & npm run wp-server)",
"start": "npm run dev"
}
然后,简单的npm-start(和npm-run-dev)将在所有平台上运行!
来一份老式的Makefile怎么样?
这允许您进行大量控制,包括如何管理子外壳、脚本之间的依赖关系等。
# run both scripts
start: server client
# start server and use & to background it
server:
npm run serve &
# start the client
client:
npm start
调用此Makefile,然后您可以键入
让一切开始。因为当您使用ctrl-C时,服务器命令实际上是在start命令的子进程中运行的,所以服务器命令也会停止,这与您自己在shell中备份不同。Make还可以完成命令行,至少在我使用的shell上是这样。另外,第一个命令将始终运行,因此您可以在这里自己键入make。
我总是在我的项目中加入一个makefile,这样我就可以在以后快速扫描每个项目的所有常用命令和参数。
您应该使用npm-run-all(或并发并行shell),因为它对启动和终止命令有更多的控制权。运算符&,|是个坏主意,因为在所有测试完成后,您需要手动停止它。
这是通过npm进行量角器测试的示例:
scripts: {
"webdriver-start": "./node_modules/protractor/bin/webdriver-manager update && ./node_modules/protractor/bin/webdriver-manager start",
"protractor": "./node_modules/protractor/bin/protractor ./tests/protractor.conf.js",
"http-server": "./node_modules/http-server/bin/http-server -a localhost -p 8000",
"test": "npm-run-all -p -r webdriver-start http-server protractor"
}
-p=并行运行命令。
-r=当其中一个命令结束时,退出代码为零,则终止所有命令。
运行npm运行测试将启动Selenium驱动程序,启动http服务器(为您提供文件)并运行量角器测试。完成所有测试后,它将关闭http服务器和selenium驱动程序。