I use a Mac, so the above chapters of NPM script run smoothly (in the first half of my life all the way open), but there are still Windows system development friends (many words, economic allow or suggest buying a Mac, know how to invest in yourself).
Here’s how our NPM script runs smoothly on Windows.
Git bash to run NPM script instead of CMD. Don’t say I didn’t tell you.
A substitute for a common command
The NPM community also provides cross-platform compatibility packages for file and directory creation, deletion, and copying
- Directory of newmake-dir-cliAnd the same
mkdir -p
; - Deletion of a file or directoryrimraf 或
del-cli
And the samerm -rf
; - A copy of a file or directorycprAnd the same
cp -r
; - Cross-platform variable reference, variable writing unified,cross-varFor example, Windows
%npm_package_name%
Linux writing$npm_package_name
;
Adding a dependency package
npm install make-dir-cli rimraf cpr cross-var -D
/ / or
yarn add make-dir-cli rimraf cpr cross-var -D
Copy the code
The command to rewrite
1. The file package. Json
// Not compatible with Windows
"scripts": {... ."/ /": "# Windows incompatible"."cover:cleanup": Rf-nyc_output rf-nyc_output rf-nyc_output."cover": "# generate coverage report \ nNYC -- Reporter = HTML NPM run test"."cover:archive": $npm_package_version && cp -r coverage/* Coverage_archive /$npm_package_version"."cover:server": "http-server coverage_archive/$npm_package_version -p $npm_package_config_port"."cover:open": "open http://localhost:$npm_package_config_port"."postcover": \n NPM -run-all cover:archive cover:cleanup --parallel cover:server cover:open"
}
/ / compatible with Windows
"scripts": {... ."/ /": # compatible with Windows ""."cover:cleanup": "# rimraf coverage report - \n Rimraf coverage && rimraf.nyc_output"."cover": "# generate coverage report \ nNYC -- Reporter = HTML NPM run test"."cover:archive": "# file coverage report \n cross-var \"make-dir coverage_archive/$npm_package_version && CPR coverage coverage_archive/$npm_package_version -o\""."cover:server": "cross-var http-server coverage_archive/$npm_package_version -p $npm_package_config_port"."cover:open": "cross-var open http://localhost:$npm_package_config_port"."precover": "npm run cover:cleanup"."postcover": \n NPM -run-all cover:archive -- Parallel cover:server cover:open"
}
Copy the code
2. Perform
npm run cover
Copy the code
The command analysis
- Copy of a file or directory
cp -r coverage/* coverage_archive/$npm_package_version
Windows writtencpr coverage coverage_archive/$npm_package_version -o
. There are two details to note: the first is the parameter location, Linux platform-p
Keep up with thecp
Back there, the Windows platform-o
在cpr
At the end. The second is copy content path writing, Linux platformcoverage/*
And the Windows platformcoverage
; - the
cover:cleanup
从postcover
Take it out put it inprecover
In the execution, avoidcpr
The condition of being cleared without finishing filing; - The command that uses variables is enclosed in double quotation marks
\
Escape, and remember to add at the beginningcross-var
;
Environment variables are set cross-platform
To get straight to the point, use cross-env.
Adding a dependency package
npm i cross-env -D
/ / or
yarn add cross-env -D
Copy the code
The command to rewrite
"scripts": {
"test": "cross-env NODE_ENV=test mocha test/"
}
Copy the code
You can…
Using NPM Script environment variables
Next: NPM Script command completion implementation
Directory: NPM Script small book