Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何并行运行多个npm脚本?_Javascript_Node.js_Build - Fatal编程技术网

Javascript 如何并行运行多个npm脚本?

Javascript 如何并行运行多个npm脚本?,javascript,node.js,build,Javascript,Node.js,Build,在我的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" 。。。但这将

在我的
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"
。。。但这将等待
start watch
完成,然后再运行
wp server

如何并行运行这些命令?请记住,我需要查看这些命令的
输出。另外,如果您的解决方案涉及构建工具,我宁愿使用
gulp
而不是
grunt
,因为我已经在另一个项目中使用了它。

使用名为的包

npmi并发——保存开发

然后设置
npm运行dev
任务,如下所示:

"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
使用名为的包

npmi并发——保存开发

然后设置
npm运行dev
任务,如下所示:

"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""

更好的解决方案是使用
&

"dev": "npm run start-watch & npm run wp-server"

更好的解决方案是使用
&

"dev": "npm run start-watch & npm run wp-server"
快速解决方案 在这种情况下,我认为最好的选择是如果此脚本用于仅在基于*nix的机器上运行的专用模块,则可以使用控制运算符进行分叉处理,如下所示:
&

"dev": "npm run start-watch & npm run wp-server"
在partial package.json文件中执行此操作的示例:

{
  "name": "npm-scripts-forking-example",
  "scripts": {
    "bundle": "watchify -vd -p browserify-hmr index.js -o bundle.js",
    "serve":  "http-server -c 1 -a localhost",
    "serve-bundle": "npm run bundle & npm run serve &"
  }
然后通过
npm-run-service-bundle
并行执行它们。您可以增强脚本,将分叉进程的PID输出到如下文件:

"serve-bundle": "npm run bundle & echo \"$!\" > build/bundle.pid && npm run serve & echo \"$!\" > build/serve.pid && npm run open-browser",
谷歌类似于bash的forking控制运营商,以了解其工作原理。我还提供了以下关于在节点项目中利用Unix技术的更多内容:

更多上下文:Unix工具和Node.js 如果您不在Windows上,Unix工具/技术通常可以很好地使用节点脚本实现某些功能,因为:

  • Node.js中的许多内容都非常喜欢模仿Unix原则
  • 你在*nix(包括OSX)上,NPM无论如何都在使用shell
  • Nodeland中的系统任务模块通常也是Unix工具的抽象或近似,从
    fs
    streams

    快速解决方案 在这种情况下,我认为最好的选择是如果此脚本用于仅在基于*nix的机器上运行的专用模块,则可以使用控制运算符进行分叉处理,如下所示:
    &

    "dev": "npm run start-watch & npm run wp-server"
    
    在partial package.json文件中执行此操作的示例:

    {
      "name": "npm-scripts-forking-example",
      "scripts": {
        "bundle": "watchify -vd -p browserify-hmr index.js -o bundle.js",
        "serve":  "http-server -c 1 -a localhost",
        "serve-bundle": "npm run bundle & npm run serve &"
      }
    
    然后通过
    npm-run-service-bundle
    并行执行它们。您可以增强脚本,将分叉进程的PID输出到如下文件:

    "serve-bundle": "npm run bundle & echo \"$!\" > build/bundle.pid && npm run serve & echo \"$!\" > build/serve.pid && npm run open-browser",
    
    谷歌类似于bash的forking控制运营商,以了解其工作原理。我还提供了以下关于在节点项目中利用Unix技术的更多内容:

    更多上下文:Unix工具和Node.js 如果您不在Windows上,Unix工具/技术通常可以很好地使用节点脚本实现某些功能,因为:

  • Node.js中的许多内容都非常喜欢模仿Unix原则
  • 你在*nix(包括OSX)上,NPM无论如何都在使用shell

  • Nodeland中的系统任务模块通常也是Unix工具的抽象或近似,从
    fs
    streams
    如果您使用的是类Unix环境,只需使用
    &
    作为分隔符:

    "dev": "npm run start-watch & npm run wp-server"
    
    否则,如果您对跨平台解决方案感兴趣,可以使用模块:


    如果您使用的是类UNIX环境,只需使用
    &
    作为分隔符:

    "dev": "npm run start-watch & npm run wp-server"
    
    否则,如果您对跨平台解决方案感兴趣,可以使用模块:


    我遇到了
    &
    |
    的问题,它们分别退出状态和抛出错误

    其他解决方案希望使用给定名称运行任何任务,比如npm run all,这不是我的用例

    所以我创建了一个异步运行npm脚本并在完成后报告的程序

    因此,对于您的脚本,应该是:


    npm run parallel wp server start watch

    我遇到了
    &
    |
    的问题,它们分别退出状态和抛出错误

    其他解决方案希望使用给定名称运行任何任务,比如npm run all,这不是我的用例

    所以我创建了一个异步运行npm脚本并在完成后报告的程序

    因此,对于您的脚本,应该是:


    npm运行并行wp服务器启动监视

    从windows cmd您可以使用:


    以这种方式启动的每个命令都在其自己的窗口中启动。

    从windows cmd可以使用:

    以这种方式启动的每个命令都在其自己的窗口中启动。

    您应该同时使用(或
    parallelshell
    ),因为它对启动和终止命令有更多的控制。操作符
    &
    |
    是个坏主意,因为在所有测试完成后,您需要手动停止它

    这是通过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驱动程序。

    您应该同时使用(或
    parallelshell
    ),因为它对启动和终止命令有更多的控制。操作符
    &
    |
    是个坏主意,因为在所有测试完成后,您需要手动停止它

    这是通过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服务器(到se
    // 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());
    
    npm run --prefix react start&  npm run --prefix express start&
    
    run-screen "npm run start-watch" "npm run wp-server"
    
    "dev": "npm run start-watch & npm run wp-server"
    
    const { spawn } = require('child_process');
    const readline = require('readline');
    
    [
      spawn('npm', ['run', 'start-watch']),
      spawn('npm', ['run', 'wp-server'])
    ].forEach(child => {
        readline.createInterface({
            input: child.stdout
        }).on('line', console.log);
    
        readline.createInterface({
            input: child.stderr,
        }).on('line', console.log);
    });
    
    const { spawn } = require("child_process");
    
    function logData(data) {
        console.info(`stdout: ${data}`);
    }
    
    function runProcess(target) {
        let command = "npm";
        if (process.platform === "win32") {
            command = "npm.cmd"; // I shit you not
        }
        const myProcess = spawn(command, ["run", target]); // npm run server
    
        myProcess.stdout.on("data", logData);
        myProcess.stderr.on("data", logData);
    }
    
    (() => {
        runProcess("server"); // package json script
        runProcess("client");
    })();
    
    npm install npm-run-all --save-dev
    
    "scripts": {
      "start-watch": "...",
      "wp-server": "...",
      "dev": "npm-run-all --parallel start-watch wp-server"
    }
    
    "dev": "(cd api && start npm run start) & (cd ../client && start npm run start)"
    
    {
      ...
      "scripts": {
        ...
        "start": "react-scripts start", // or whatever else depends on your project
        "dev": "(cd server && npm run start) & (cd ../client && npm run start)"
      }
    }
    
    {
    "start-express": "tsc && nodemon dist/server/server.js",
    "start-react": "react-scripts start",
    "start-both": "npm -p -r run start-react && -p -r npm run start-express"
    }
    
    {"server": "tsc-watch --onSuccess \"node ./dist/server/index.js\"",
    "start-server-dev": "npm run build-server-dev && node src/server/index.js",
    "client": "webpack-dev-server --mode development --devtool inline-source-map --hot",
    "dev": "concurrently \"npm run build-server-dev\"  \"npm run server\" \"npm run client\""}
    
    npm i -g npm-run-all
    
    npm i npm-run-all --save-dev
    
    "scripts": {
        "server": "live-server index.html",
        "watch": "node-sass scss/style.scss --watch",
        "all": "npm-run-all --parallel server watch"
    },
    
    npm run all