假设我有

"scripts": {
    "pre-build": "echo \"Welcome\" && exit 1",
    "build_logic": "start cmd.exe @cmd /k \"yo esri-appbuilder-js:widget && exit 1\"",
    "post_build":  "start C:\\WebAppBuilderForArcGIS\\startupShortcut",
    "exit" : "start cmd.exe @cmd /k \"echo \"goodbye\" && exit 1\""
  },

我可以运行什么NPM命令来让所有这些脚本按顺序启动。当我使用前/后修复,他们启动顺序,但他们不等待父脚本完成前执行。我假设唯一的解决方案是这样的:我如何让Gulp任务在异步中触发shell命令时顺序触发。级数辅助函数?? 我知道这可以用Gulp来完成,但我现在还是想继续用NPM来探索它的功能。谢谢你的帮助!


当前回答

您可以使用npm-run-all以许多不同的方式组合多个命令

例如,如果你的package.json中有以下脚本:

"scripts": {
    "clean": "rimraf dist",
    "lint":  "eslint src",
    "build": "babel src -o lib"
}

你可以像这样依次运行它们:

$ npm-run-all clean lint build

查看这个问题了解如何并行运行多个npm命令

其他回答

顺序与并行混合示例

如果您需要混合,以下是我所做的,以确保command_1首先运行并完成,而command_2a和command_2b可以并行运行。

    "dev": "yarn command_1 && (yarn command_2a & yarn command_2b)"

实际的例子:

    "dev": "yarn buildPackage && (yarn watchPackageSource & yarn watchExamplePage)"

遵循@Mobiletainment的精彩回答,你还可以使用npm-run-all来使命令更短,更易于阅读。在你的情况下:

"scripts": {
    ...
    "build": "run-s pre-build build_logic post_build exit"
}

Run-s是npm-run-all提供的快捷方式,它可以依次运行所有给定的npm脚本,因此是-s (Run-s是npm-run-all -s的缩写版本)。

通过npm run调用这些脚本,并用双&&连接它们:

npm run pre-build && npm run build_logic && npm run post_build && npm run exit

解释:

使用&&(双&号)进行顺序执行。 使用&(单个&号)进行并行执行。

有几个选项比公认的答案更好:

&& - runs commands sequentially, as long as each is successful: command && command && command; supported in most shells (early versions of Powershell did not support this) (thanks @Mobiletainment) ; - runs commands sequentially, ignoring success/failure; supported in most, if not all, shells the npm-run-all package - can run multiple commands either in sequence or in parallel; see the documentation for usage (thanks @Or A.) the concurrently package - runs multiple commands in parallel (included because this is a popular Google result); see the documentation for usage

你可以试试:


"scripts": {
  "clean-dist": "rm -f ./dist/*.js && rm -f ./dist/*.map",
  "build": "npm run clean-dist && parcel build ./packages/index.html"
},