[NPM] Make npm scripts cross-environment friendly
Unfortunately not all shell commands work across various environments. Two main techniques to support cross-environment scripts is to either use cross-platform commands or to use normalized node packages. In this lesson, we will look at updating an existing set of npm scripts and make sure they work on Mac OS X, Linux, and Windows.
1. Use cross-env
npm i -D cross-env
"test": "cross-env BABEL_ENV=test mocha spec/ --require babel-register",
2. Use 'rimraf' instead of 'rm -rf'
npm i -D rimraf
"postcover": "rimraf .nyc_output",
3. Use 'opn-cli' instead of 'open'
npm i -D opn-cli
"cover:open": "opn coverage/index.html",
4. Windows need double qoutes, to make it works for all envs, we can use \"
"lint:css": "stylelint \"**/*.scss\" --syntax scss",
5. Windows : '%npm_package_version%',
Mac and Linux: '$npm_package_version'
npm i -D cross-var
"prebuild": "cross-var rimraf public/$npm_package_version",
Should be careful when we use '|' pipe and '>' output symbol, we should wrap the whole command in a \"\":
"build:css": "cross-var \"node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css\"",
"scripts": {
"start": "node index.js",
"poststart": "npm run build && npm run server",
"pretest": "npm run lint",
"test": "cross-env BABEL_ENV=test mocha spec/ --require babel-register",
"cover": "nyc npm t",
"postcover": "rimraf .nyc_output",
"cover:open": "opn 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": "cross-var rimraf public/$npm_package_version",
"xbuild:html:mac": "pug --obj data.json src/index.pug --out public/$npm_package_version/",
"xbuild:html:win": "pug --obj data.json src/index.pug --out public/%npm_package_version%/",
"build:html": "cross-var pug --obj data.json src/index.pug --out public/$npm_package_version/",
"xbuild:css:mac": "node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css",
"xbuild:css:win": "node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/%npm_package_version%/index.min.css",
"build:css": "cross-var \"node-sass src/index.scss | postcss -c .postcssrc.json | cssmin > public/$npm_package_version/index.min.css\"",
"xbuild:js:mac": "mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js",
"xbuild:js:win": "mustache data.json src/index.mustache.js | uglifyjs > public/%npm_package_version%/index.min.js",
"build:js": "cross-var \"mustache data.json src/index.mustache.js | uglifyjs > public/$npm_package_version/index.min.js\"",
"server": "npm-run-all --parallel server:*",
"server:create": "cross-var http-server public/$npm_package_version -p $npm_package_config_port",
"server:launch": "cross-var opn http://localhost:$npm_package_config_port",
"prepush": "npm run lint"
},
[NPM] Make npm scripts cross-environment friendly的更多相关文章
- npm中npm install 始终出错解决办法
npm中npm install 始终出错解决办法 错误信息: C:\Windows\System32>npm install -g gulp npm ERR! Windows_NT 6.1.76 ...
- 一次执行两个npm "start": "concurrently 'npm:dev' 'npm:json-server'"
用的这个程序 concurrently 说是再有异步的时候,&& 就不好使,而且&& 也不能执行npm 只能执行命令 官方地址:https://www.npmjs.co ...
- 遇到 npm WARN npm npm does not support Node.js vx.x.x的解决办法
遇到如下警告 PS C:\Users\Administrator> npm npm WARN npm npm does not support Node.js v9.11.2 npm WARN ...
- npm & private npm service & nrm & nvm
npm & private npm service & nrm & nvm npm server # nrm https://www.cnblogs.com/xgqfrms/t ...
- tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:56281 npm ERR! network This is most likely not a problem with npm itself npm ERR! network and is related to network
tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:56281npm ERR! networ ...
- [Node.js] Configuring npm package.json scripts
With a node package manager's (npm) package.json script property, you can preconfigure common tasks ...
- [NPM] Run npm scripts in parallel
In this lesson we will look at running several npm scripts in parallel. Sometimes you don’t need scr ...
- [NPM] Run npm scripts in series
After creating several npm script it becomes useful to run multiple scripts back-to-back in series. ...
- [NPM] Run npm scripts when files change with onchange
In this lesson we will look at how we can setup our npm scripts to execute when the file system has ...
随机推荐
- 一个虐你千百遍的问题:“RPC好,还是RESTful好?”
看到知乎上有这样一个问题 WEB开发中,使用JSON-RPC好,还是RESTful API好? 还有其他优秀的推荐方案吗? -------------------------------------- ...
- 使用Microsoft.Office.Interop.Excel时,64位问题
前不久,碰到一个问题. 曾经用的好好的Microsoft.Office.Interop.Excel实现的导出Excel,迁移至64位server后,就出现: 检索 COM 类工厂中 CLSID 为 { ...
- 在安卓(手机)上运行 Ubuntu (Linux)
在安卓(手机)上运行 Ubuntu (Linux) 由于x86 和 arm 是跨平台的,所使用的编译器自然也不同.如果要在电脑上编译安卓手机上的程序,则需在电脑端建立ARM交叉编译环境,这个过程是在耗 ...
- PHP服务器环境打开配置文件
MAC 1. cd /usr/local/etc/nginx/servers vim www.xxx.com 2. 在/usr/local/etc/nginx/servers目录下,不同的 .con ...
- .vsdc和.svf用于formal verification tools
svf:Setup Verification for Formality
- ClickOnce
Clic WPF ClickOnce应用程序IIS部署发布攻略 WPF程序非常适合公司内网使用,唯一缺点就是客户端要安装.net框架4.0.优势也很明显,在客户端运行的是一个WinForm程序,自 ...
- Photon + Unity3D 线上游戏开发 学习笔记(三)
好的,说了两篇了 如今我们正式的入手,揭开photon 的盖头哈 建立photon项目 第一步: 在Visual studio建立一个空的 待会为了測试也会在里面建立一个client 项目 (只 ...
- 1.1 Python基础知识 - 变量
1.什么是变量? 变量是可以通过变量名访问的内存地址,变量通常是可变的. 2.怎样去定义? 变量格式: 变量名 = "变量值" 例如: name = "Zhanghk&q ...
- UVA 11800 - Determine the Shape 几何
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 【Codeforces Round #301 (Div. 2) E】Infinite Inversions
[链接] 我是链接,点我呀:) [题意] 给你一个无限长的序列1,2,3,4... 然后给你n个操作. 每个操作ai,bi; 表示调换位置为ai和位置为bi的数的位置. (ai,bi<=10^9 ...