在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,因为我已经在另一个项目中使用了它。


当前回答

同时安装npm install

"scripts": {
    "start:build": "tsc -w",
    "start:run": "nodemon build/index.js",
    "start": "concurrently  npm:start:*"
  },

其他回答

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

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

所以我创建了npm运行并行,它异步运行npm脚本,并在完成后返回报告。

所以,对于您的脚本,它应该是:

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

由于您可能需要向该脚本中添加越来越多的内容,因此它将变得混乱且难以使用。如果您需要检查一些条件和使用的变量,该怎么办?所以我建议您看看允许使用js创建脚本的google/zx。

简单用法:

安装zx:npm i-g zx添加package.json命令(可选,您可以将所有内容移动到脚本中):

  "scripts": {
    "dev": "zx ./scripts/dev.mjs", // run script
    "build:dev": "tsc -w", // compile in watch mode
    "build": "tsc", // compile
    "start": "node dist/index.js", // run
    "start:dev": "nodemon dist/index.js", // run in watch mode
  },

创建dev.mjs脚本文件:

#!/usr/bin/env zx

await $`yarn build`; // prebuild if dist is empty
await Promise.all([$`yarn start:dev`, $`yarn build:dev`]); // run in parallel

现在,每当您想启动开发服务器时,只需运行yarn dev或npm run dev。

它将首先编译ts->js,然后在监视模式下并行运行typescrpt编译器和服务器。当您更改ts文件时->它将由tsc重新编译->nodemon将重新启动服务器。


高级编程使用

加载env变量,在监视模式下编译ts,然后从dist-on-changes(dev.mjs)重新运行服务器:

#!/usr/bin/env zx
import nodemon from "nodemon";
import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";

// load env variables
loadEnvVariables("../env/.env");

await Promise.all([
  // compile in watch mode (will recompile on changes in .ts files)
  $`tsc -w`,
  // wait for tsc to compile for first time and rerun server on any changes (tsc emited .js files)
  sleep(4000).then(() =>
    nodemon({
      script: "dist/index.js",
    })
  ),
]);

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

function getDirname() {
  return path.dirname(fileURLToPath(import.meta.url));
}

function loadEnvVariables(relativePath) {
  const { error, parsed } = dotenv.config({
    path: path.join(getDirname(), relativePath),
  });

  if (error) {
    throw error;
  }

  return parsed;
}

您还可以在特定脚本上使用pre和post作为前缀。

  "scripts": {
    "predev": "nodemon run-babel index.js &",
    "dev": "webpack-dev-server"
  }

然后运行:npm运行开发

我已经使用npm-run all有一段时间了,但我一直都不习惯,因为在监视模式下命令的输出不能很好地一起工作。例如,如果我在监视模式下开始创建react应用程序和jest,我将只能看到上次运行的命令的输出。所以大多数时候,我都在手动运行所有命令。。。

这就是为什么,我实现了自己的lib,运行屏幕。它仍然是一个非常年轻的项目(从昨天开始:p),但它可能值得一看,在您的情况下,它将是:

run-screen "npm run start-watch" "npm run wp-server"

然后按数字键1查看wp服务器的输出,按0查看start watch的输出。

使用npm运行多个并行脚本的分步指南。全局安装npm-run-all包

npm i -g npm-run-all

现在在package.json所在的项目中安装并保存此包

npm i npm-run-all --save-dev

现在以这种方式修改package.json文件中的脚本

"scripts": {
    "server": "live-server index.html",
    "watch": "node-sass scss/style.scss --watch",
    "all": "npm-run-all --parallel server watch"
},

现在运行此命令

npm run all

有关此包的详细信息,请参见给定的链接npm全部运行