Javascript 按顺序运行 NPM 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39172536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:17:49  来源:igfitidea点击:

Running NPM scripts sequentially

javascriptnode.jsnpmcmd

提问by Rice

Let's say I have

假设我有

"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\""
  },

What NPM command can I run to let all of these scripts launch sequentially. When I use pre/post fixing they launch sequentially but they don't wait for the parent script to finish before executing. I am assuming the only solution is like: How do I get Gulp tasks to fire sequentially when firing shell commands in an async.series helper function?? I know this can be done with Gulp but I would like to stick with NPM for now to explore its capabilities. Thanks for any help!

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

回答by Mobiletainment

Invoke these scripts via npm run and chain them with double ampersand &&:

通过 npm run 调用这些脚本并使用双 & 将它们链接起来&&

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

Explanation:

解释:

  • Use &&(double ampersand) for sequential execution.
  • Use &(single ampersand) for parallel execution.
  • 使用&&(双与号)进行顺序执行。
  • 使用&(单与号)进行并行执行。

回答by Or A.

Following @Mobiletainment's great answer, you can also use npm-run-allto make the command much shorter and much more readable. In your case:

遵循@Mobiletainment 的精彩回答,您还可以使用npm-run-all使命令更短且更具可读性。在你的情况下:

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

run-sis a shortcut npm-run-allprovides, that runs all the given npm-scripts sequentially, hence the -s(run-sis a shorter version of npm-run-all -s).

run-snpm-run-all提供的快捷方式,它按顺序运行所有给定的 npm 脚本,因此-s(run-s是 的较短版本npm-run-all -s)。

回答by Dave V

You could just string them into another script. "start": "pre-build && build_logic && post_build && exit"

您可以将它们串成另一个脚本。 "start": "pre-build && build_logic && post_build && exit"

回答by Tzach Ovadia

You can prefix your scripts preand postso they will execute automatically:

您可以为脚本添加前缀prepost以便它们自动执行:

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

then run npm run build

然后运行 npm run build

回答by Muhammed Moussa

you can try:

你可以试试:


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

回答by KyleMit

You can use npm-run-allto combine multiple commands in a lot of different ways

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

For example, if you had the following scripts in your package.json:

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

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

You could run them all sequentially like this:

您可以像这样按顺序运行它们:

$ npm-run-all clean lint build

See this question for how to run multiple npm commands in parallel

请参阅此问题以了解如何并行运行多个 npm 命令