简体   繁体   中英

Running NPM scripts sequentially

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!

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

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.

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

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

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

You can prefix your scripts pre and post so they will execute automatically:

"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

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

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

For example, if you had the following scripts in your 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

you can try:


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

There are several options that are better than the accepted answer:

  • && - 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

Sequential & Parallel Mix Example

In case you need a mix, here's what I did to ensure command_1 runs and completes first, while command_2a and command_2b can run in parallel.

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

Practical Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM