[NPM] Create a bash script to replace a complex npm script

In this lesson we will look at pulling out complex npm scripts into their own external bash scripts. This ends up simplifying your package.json file and abstracts the complexity into another file.

From:

    "pretest": "npm run lint",
    "test": "BABEL_ENV=test mocha spec/ --require babel-register",

To:

1. Create "scripts" folder and "test.sh" file:

#!/usr/bin/env bash

npm run lint
BABEL_ENV=test mocha spec/ --require babel-register

Then update package.json

"test": "./scripts/test.sh",

and remove "pretest" script.

2. Add premisson to the bash files:

cd scripts
chmod -R 777 . // change premission for all files in script folder

3. Can add some messages:

From :

"build": "npm-run-all build:*",
    "prebuild": "rm -rf public/$npm_package_version",
    "build:html": "pug --obj data.json src/index.pug --out public/$npm_package_version/",
    "build:css": "node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css",
    "build:js": "mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js",

TO:

"build": "./scripts/build.sh",
#!/usr/bin/env bash

echo "Building..."
rm -rf public/$npm_package_version
pug --obj data.json src/index.pug --out public/$npm_package_version/
node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css
mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js
echo "Finished building."
原文地址:https://www.cnblogs.com/Answer1215/p/6390395.html