A technique you might use once you start having lots of npm scripts is to use a node package that allows you to define your scripts in an external package-scripts.js file. By pulling out your scripts it can help with organization, enable better comments, provide shortcuts, and more.

Install:

npm i -D p-s

Run:

node_modules/.bin/p-s init

Then if we check package.json file, we found that the scripts from:

  "scripts": {
"start": "node index.js",
"poststart": "npm run build && npm run server",
"pretest": "npm run lint",
"test": "BABEL_ENV=test mocha spec/ --require babel-register",
"cover": "nyc npm t",
"postcover": "rm -rf .nyc_output",
"cover:open": "open coverage/index.html",
"lint": "npm-run-all lint:**",
"lint:js": "eslint --cache --fix ./",
"lint:css": "stylelint '**/*.scss' --syntax scss",
"lint:css:fix": "stylefmt -R src/",
"watch": "npm-run-all --parallel watch:*",
"watch:test": "npm t -- --watch",
"watch:lint": "onchange 'src/**/*.js' 'src/**/*.scss' -- npm run lint",
"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",
"server": "npm-run-all --parallel server:*",
"server:create": "http-server public/$npm_package_version -p $npm_package_config_port",
"server:launch": "open http://localhost:$npm_package_config_port",
"prepush": "npm run lint"
},

to:

  "scripts": {
"start": "nps",
"test": "nps test"
},

We still can run 'npm start' & 'npm t', the actual scripts points to package-script.js file:

module.exports = {
scripts: {
default: 'node index.js',
poststart: 'npm run build && npm run server',
pretest: 'npm run lint',
test: 'BABEL_ENV=test mocha spec/ --require babel-register',
cover: {
default: 'nyc npm t',
open: 'open coverage/index.html'
},
postcover: 'rm -rf .nyc_output',
lint: {
default: 'npm-run-all lint:**',
js: 'eslint --cache --fix ./',
css: {
default: 'stylelint \'**/*.scss\' --syntax scss',
fix: 'stylefmt -R src/'
}
},
watch: {
default: 'npm-run-all --parallel watch:*',
test: 'npm t -- --watch',
lint: 'onchange \'src/**/*.js\' \'src/**/*.scss\' -- npm run lint'
},
build: {
default: 'npm-run-all build:*',
html: 'pug --obj data.json src/index.pug --out public/$npm_package_version/',
css: 'node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css',
js: 'mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js'
},
prebuild: 'rm -rf public/$npm_package_version',
server: {
default: 'npm-run-all --parallel server:*',
create: 'http-server public/$npm_package_version -p $npm_package_config_port',
launch: 'open http://localhost:$npm_package_config_port'
},
prepush: 'npm run lint'
}
};

Now there are some problems we need to fix before it can run all the scripts.

1. Add help command:

Now if we run

npm run

/*
start
nps
test
nps test
*/

We can only see two scripts.

But we can run:

npm start -- --help

Then we able to see all the scripts:

For a more convenient way, we can create a help script:

"help": "npm start -- --help"

or just:

"help": "nps --help"

2. We can add "description" to package-scripts.js to make each command more clear why to use.

So for example, I can "test" script in package-scripts.js from:

test: 'BABEL_ENV=test mocha spec/ --require babel-register',

to:

    test: {
script: 'BABEL_ENV=test mocha spec/ --require babel-register',
description: 'Run the mocha tests'
},

Now if we run help command:

test - Run the mocha tests - BABEL_ENV=test mocha spec/ --require babel-register

We can see the description also.

3. There is no 'npm-run-all' package for 'p-s'

For example:

    lint: {
default: 'npm-run-all lint:**',
js: 'eslint --cache --fix ./',
css: {
default: 'stylelint \'**/*.scss\' --syntax scss',
fix: 'stylefmt -R src/'
}
},

We can change to:

    lint: {
default: 'nps lint.js,lint.css,lint.fix',
js: 'eslint --cache --fix ./',
css: {
default: 'stylelint \'**/*.scss\' --syntax scss',
fix: 'stylefmt -R src/'
}
},

Another example:

    watch: {
default: 'npm-run-all --parallel watch:*',
test: 'npm t -- --watch',
lint: 'onchange \'src/**/*.js\' \'src/**/*.scss\' -- npm run lint'
},

'nps' understand '--parallel' flag, so we can change this script to:

    watch: {
default: 'nps watch.test,watch.lint --parallel',
test: 'npm t -- --watch',
lint: 'onchange \'src/**/*.js\' \'src/**/*.scss\' -- npm run lint'
},

4. 'nps' doesn't support 'pre-' or 'post-' scripts.

For the post- scripts: using '&&' 

For example:

    default: 'node index.js',
poststart: 'npm run build && npm run server',

We have poststart, we can change to:

default: 'node index.js && nps build,server',

For the pre- scripts:

For example:

    pretest: 'npm run lint',
test: {
script: 'BABEL_ENV=test mocha spec/ --require babel-register',
description: 'Run the mocha tests'
},

we can change to:

    test: {
default: {
script: 'nps lint,test.run',
description: 'Run the mocha tests'
},
run: 'BABEL_ENV=test mocha spec/ --require babel-register',
},

Example2:

change from:

    cover: {
default: 'nyc npm t',
open: 'open coverage/index.html'
},
postcover: 'rm -rf .nyc_output',

to:

    cover: {
default: 'nyc npm t && nps cover.clean',
open: 'open coverage/index.html',
clean: 'rm -rf .nyc_output',
},

Example3:

From:

    build: {
default: 'nps build.html,build.css,build.js',
html: 'pug --obj data.json src/index.pug --out public/$npm_package_version/',
css: 'node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css',
js: 'mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js'
},
prebuild: 'rm -rf public/$npm_package_version',

to:

    build: {
default: 'nps build.clean,build.html,build.css,build.js',
clean: 'rm -rf public/$npm_package_version',
html: 'pug --obj data.json src/index.pug --out public/$npm_package_version/',
css: 'node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css',
js: 'mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js'
},

Example4:

prepush: 'npm run lint'

Move to package.json file:

"prepush": "nps lint"

So now if you want to run any script, you can do:

npm start lint
npm start build.js

Aslo nps provides shortcut to run script:

npm start build.html

// the same as

npm start b.h

If you install 'p-s' grobally, you can run any command by:

nps lint
nps build

[NPM] Pull out npm scripts into another file with p-s的更多相关文章

  1. NPM 使用及npm升级中问题解决

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  2. [转] 合理使用npm version与npm dist-tag详解

    第一步:发布第一个稳定版本 npm publish//1.0.0 第二步:修改文件继续发布第二个版本 git add -A && git commit -m "c" ...

  3. npm link & run npm script

    npm link & run npm script https://blog.csdn.net/juhaotian/article/details/78672390 npm link命令可以将 ...

  4. [转]adb pull Permission denied及no such file错误

    adb pull  Permission denied及no such file错误 http://www.the8m.com/blog/article/javadk/adbpull.html XP系 ...

  5. angular2 学习笔记 ( angular cli & npm version manage npm 版本管理 )

    更新 : 2017-05-05 现在流行 Yarn ! 它是 facebook google 推出的东西. 算是补助 npm 做的不够好的地方. 源码依然是发布去 npm,只是下载接口换掉罢了哦. n ...

  6. 【npm】利用npm安装/删除/发布/更新/撤销发布包

      什么是npm? npm是javascript的包管理工具,是前端模块化下的一个标志性产物 简单地地说,就是通过npm下载模块,复用已有的代码,提高工作效率   1.从社区的角度:把针对某一特定问题 ...

  7. npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script build`?

    跑npm build结果如下: npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script ...

  8. npm汇总:npm命令 + 实用插件

    一.npm常用命令,以便查阅: npm install     //运行npm install可根据package.json的配置自动安装所有依赖包 npm uninstall   //卸载依赖,如n ...

  9. npm 取消代理 npm config delete proxy

    今天在安装electron时设置了代理,发现再npm install 安装别的总是装不上,只好取消代理. npm 取消代理 npm config delete proxy

随机推荐

  1. 【AIM Tech Round 4 (Div. 2) B】Rectangles

    [链接]http://codeforces.com/contest/844/problem/B [题意] 也是道计数水题,没什么记录意义 [题解] 枚举每个点的位置在,然后往右往下 枚举和它一样颜色的 ...

  2. 关于java中String的一点理解

      String类是java的最基本类之中的一个,非常好的掌握它的原理非常是必要的!   1.String的Final类型的.是不可继承 的.final类默认的方法都为final类型,保证了方法不能被 ...

  3. hdu-3642--Get The Treasury-线段树求面积并

    求空间中叠加3次及3次以上的体积. 由于|z|<=500.所以直接把z轴剥离出来1000层. 然后对于每一层进行线段树求面积并. #include<stdio.h> #include ...

  4. 1.13 Python基础知识 - 字典和集合

    一.字典 字典是一组键-值对的数据结构.每个键对应一个值.在字典中,键不能重复.根据键可以查询到值.字典是键和值的映射关系 字典的定义: 字典通过花括号中用逗号分隔的元素(键-值.键-值对使用冒号分隔 ...

  5. 通过WMI的方式去设置LCD背光亮度

    code例如以下: #include "stdafx.h" #include <objbase.h> #include <windows.h> #inclu ...

  6. 常用加密算法的Java实现总结(二)

    常用加密算法的Java实现总结(二) ——对称加密算法DES.3DES和AES 摘自:http://www.blogjava.net/amigoxie/archive/2014/07/06/41550 ...

  7. jemter--录制的脚本设置循环次数不起作用

    以下是比较jmeter线程组中设置循环次数和循环控制器中设置循环次数的区别 1.jmeter生成的脚本没有step1(循环控制器)控制器,故循环在线程组中设置   2.badboy录制的脚本有setp ...

  8. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

  9. cookie和session使用

    cookie和session使用 一.总结 1.需要使用的场景:验证用户是否登录时    获取用户的用户名时  退出登录时 2.cookie和session在什么时候记录:在登录成功之后 二.cook ...

  10. UVA 10970 - Big Chocolate 洪水@。@

    先横着切m-1刀,矩形巧克力就变成了1*n (有m个)然后每个都要切n-1下,所以有 m*(n-1) +(m-1)= n*m-1 #include<cstdio> int main() { ...