Cypress running npm and npx scripts in one script sequentially - npm

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"

Related

Using command line arguments in npm run script

I am trying to change the {variable} part in my custom-defined npm run deploy script
"scripts":{
"deploy": "npm run build && scp -r ./public example#192.200.0.11:/home/{DIRECTORY}/index.js",
}
I want to run it like npm run deploy --DIRECTORY:project99
You can pass arguments to npm run as Environment variable. See Npm Docs
"scripts": {
"deploy": "npm run build && scp -r ./public example#192.200.0.11:/home/${NPM_CONFIG_DIRECTORY}/index.js"
},
This should work
npm run deploy --DIRECTORY=project99

How to conditionally run npm script

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"

How to run gulptask from package.json?

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?

Run browserify upon file change with tsc -w

I am new to npm build tools. I want to bundle the compiled typescript files as soon as there is a change in typescript files and run lite-server concurrently. To achieve that I have written following npm build script -
"build": "tsc",
"bundle": "browserify -s main app/goc-common/common.module.js > dist/bundle.js",
"build_dev": "npm run build && npm run bundle && concurrently \"tsc -w && npm run bundle\" \"lite-server\"",
However this doesn't seems to work, it just compiles the files and refresh the browser, donot bundle the files again.
You should use watchify to continue watching tsc's output files for changes:
"build": "tsc",
"bundle": "watchify -s main app/goc-common/common.module.js -o dist/bundle.js",
"build_dev": "npm run build && npm run bundle && concurrently \"tsc -w && npm run bundle\" \"lite-server\""
As you've noticed, browserify doesn't watch; it just runs once and then it's done. watchify's usage is identical to browserify's, except that the -o option is mandatory.

npm script command to run a script command from another package.json

I have two separate projects that use npm - so I have both :
some_base_folder/projectA/package.json and some_base_folder/projectB/package.json
Each of those files has a scripts section in it.
If I go to some_base_folder/projectA/ and run npm run-script test it executes the test command from the scripts section of some_base_folder/projectA/package.json as it should.
What can I put as the value of "scripts": {test_projectA:'????' in some_base_folder/projectB/package.json so that when I am in some_base_folder/projectB/ and I run npm run-script test_projectA it will be
execute the test script of Project A?
I tried ../projectA/npm run-script test but it says:
'..' is not recognized as an internal or external command,
operable program or batch file.
I am running under windows 7 but would prefer a solution that would also work properly on linux.
well it turns out to be quite simple:
"scripts": {
test_projectA:"cd ../projectA && npm run-script test"
}
You should use --prefix.
npm run [command] --prefix path/to/your/other/folder
And for yarn:
yarn --cwd path/to/your/other/folder [command]
I ended up using:
"scripts": {
"job": "cd ./sub && \"$npm_execpath\" run subjob",
...
}
because this also works with yarn.