I am looking for a cross-platform way of conditionally running a step in my build script.
I have a build step that is expensive, however checking to see if it is necessary is relatively quick. I have created a script that returns a non-zero error code if it is not necessary to do the build step. How can I write my npm script to only run the build if the check passes but provide errors if the build step fails?
Example package.json:
"scripts": {
"schema:build": "npm run schema:rebuild-check && npm run schema:force-build",
"schema:rebuild-check": "node tools/schema is-rebuild-necessary",
"schema:force-build": "npm run schema:validate && npm run schema:generate-index && npm run schema:bundle"
}
The problem with the above is that if the rebuild is not necessary my entire build fails. I can't just swallow the exit code with something like exitzero because I want to know if any of the commands fail in the schema:force-build script.
The problem arises from two things:
1) If an npm script exits with a non-zero code npm will always complain
2) Need to provide an "else" that returns non zero using the || operator
Working solution:
"schema:build": "node tools/schema is-rebuild-necessary && npm run schema:force-build || echo Skipping schema build",
"schema:force-build": "npm run schema:validate && npm run schema:generate-index && npm run schema:bundle"
Related
I am using Cypress and I have the following scripts:
"merge:reports": "mochawesome-merge mochawesome-report/*.json > cypress-combined-report.json",
"create:html:report": "npm run merge:reports && marge --reportDir final-report cypress-combined-report.json",
"delete:reportFolder": "if exist mochawesome-report rmdir /Q /S mochawesome-report && if exist final-report rmdir /Q /S final-report",
"start": "npm run delete:reportFolder && npx cypress run --browser chrome && npm run merge:reports && `enter code here`npm run create:html:report"
What I want is all those scripts running sequentially but I think there is something I am missing here as I am not that familiar with npm and npx as when I trigger this script only it is passing:
npx cypress run --browser chrome
But when I try to execute the script with all the 4 scripts I am getting an error which I think is due to having a npx script:
npm run start
Would be glad for any suggestions or advices where I am wrong, thanks!
If it is unintentional I could see enter code here written for start, please remove that and try running afterwards.
Delete the node_modules folder and package-lock.json files. Then run npm i to install all the dependencies again and then again try running npm run start
Or Remove npx from the cypress run:
"start": "npm run delete:reportFolder && cypress run --browser chrome && npm run merge:reports && npm run create:html:report"
In my package.json, I have:
...
"scripts": {
"generate": "nuxt generate & npm run css",
...
And run the command via npm run generate, with only one issue -- it seems to run in the background, so if I run it in a bash script with additional commands following the generation, they actually run during the generation. Is there an option to make nuxt generate run in the foreground?
That‘s because you only have one & in script which means the command before will be sent to the background. Try using && between the commands.
I'm trying to run preflight checks via npm before allowing other scripts to process.
The following works but I'm wondering if there's a better way
package.json
"deploy": "npm run _deploy:preflight && npm run _deploy:real",
"_deploy:preflight": "node ./build-utils/deploy-preflight.js",
build-utils/deploy-preflight.js
if (checksFail()) {
console.log("--------------");
console.log("preflight checks failed!");
console.log("--------------");
process.exit(1);
}
The problem isn't so much that it doesn't work, it's that the console is then littered with a huge npm ERR / stacktrace and I'd much rather just see the clean "preflight checks failed" message and still have it prevent npm run _deploy:real from running
Yes, I agree - npm can be rather noisy/verbose at times.
The following suggestion assumes you keep deploy-preflight.js as it currently is, whereby you continue to exit with an exit code 1 on error, i.e. process.exit(1).
The npm --silent command-line option, or it's shorthand equivalent -s, is probably the most favoured!. However, it does require you to include it with the CLI command that you run.
For instance:
$ npm run deploy -s
^
or
$ npm run -s deploy
^
I'm trying to understand and follows https://github.com/DMPRoadmap/roadmap/wiki/Installation
But I don't understand something they use.
What does these do?
1) npm run bundle
I know it equals to npm run-script bundle as according to npm doc about run script but I don't really understand where the bundle come from; in other words, I don't understand what npm doc about run script mean by
an arbitrary command from a package's script object
2) npm run bundle -- -p
Since I don't know where the bundle come from, I don't know how to work out the meaning of -- -p option. I want to find its documentation and see the details.
I'm not sure if npm doc about bundle is related, but it seems to be replaced by install as documented in npm doc about install.
And why is this option got so many - characters (3 in this case) before p? I normally see 2 - for long option name and 1 - for abbreviated option name
Any time you see npm run [x] anywhere it means that it's executing a command located in the scripts section of the package.json file. Therefore npm run bundle runs the bundle command located here: https://github.com/DMPRoadmap/roadmap/blob/master/lib/assets/package.json#L8 which in this case looks like all it's doing is running webpack
"scripts": {
"test": "./node_modules/.bin/karma start",
"bundle": "./node_modules/.bin/webpack",
"lint": "./node_modules/.bin/eslint --ext .js --cache ./javascripts/ || true"
}
I am trying to run my gulp lint task in my npm script like this, part of the package.json:
"lint":"cd somedirectory && npm run gulp-lint"
"gulp":"gulp",
"gulp-lint": "gulp --lint"
When I run npm run lint I get this error:
missing script : gulp-lint
How can I call the gulp task lint?