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!
Start
command, you should be able to use /wait
parameter (Start application and wait for it to terminate)
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
).
npm-run-all
reveals that it has 69 dependencies (not counting dev deps!), see e.g. npm.broofa.com/?q=npm-run-all . No thanks, I dont want to be a victim of some kind of sideload attack or a farce like the left-pad
issue.
run-s
is really just replacing npm run pre-build && npm run build_logic && npm run post_build && npm run exit
? why would I introduce another dependency, that confuses every developer, who knows the &&
command? In my case I am working on project that uses run-s
(introduced by another dev) and it confuses me rather than helping me! Can I remove run-s securely or am I missing a difference here?
build
and clean
(for example) then I prefer to use the &&
syntax.
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"
start
command you are executing in windows technically is finished. Use the /wait
flag with start
to force the start
application to remain open until the internal method is also finished.
start notepad
from a command prompt, then take a look at the original command prompt. You should be able to type in another command. Then run start /wait notepad
. The start
command should continue to "run" while your notepad window is open (take a look at the command prompt again). Then once you close notepad, start
will be finished.
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"
},
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)"
Success story sharing
&&
are evaluated by the shell and don't work on Windows.&&
syntax is a UNIX construct. It will behave incorrectly on Window's machines, with potentially breaking consequences for your build process.npm-run-all
docs: “we sometimes use&
to run multiple command in parallel, but Windows'cmd.exe
... does not support [this operator].” So it'd appear you were right — at least from my brief research the&&
operator seems perfectly cross-platform compatible.