gaw*_*awi 12 node.js npm package.json npm-scripts
我想创建一个package.json构建脚本,在从Windows,Linux,Mac运行时执行稍微不同的命令集.
问题是我无法找到一种方法将它放在package.json文件中,在每个系统上运行都没有问题.
这是我想要的一个例子:
"scripts" : {
"build.windows" : "echo do windows specific stuff",
"build.linux" : "echo do linux specific stuff",
"build.mac" : "echo do mac specific stuff",
"build" : "??????????????" <- what to put here to execute script designed for OS
on which npm is running
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*e P 16
有一个名为run-script-os(NPM | GitHub)的NPM软件包,它不需要你编写任何其他文件,如果你想要做的事情非常简单,这可能很方便.例如,在你的中package.json,你可能有类似的东西:
"scripts": {
"test": "run-script-os",
"test:darwin:linux": "export NODE_ENV=test && mocha",
"test:win32": "SET NODE_ENV=test&& mocha"
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以npm test在Windows,Mac或Linux上运行,并在每个上获得类似(或不同!)的结果.
hur*_*ane 15
您可以将脚本与node run-script命令一起使用.npm run是它的捷径.
包json:
"scripts" : {
"build-windows" : "node build-windows.js",
"build-linux" : "node build-linux.js",
"build-mac" : "node build-mac.js",
"build" : "node build.js"
}
Run Code Online (Sandbox Code Playgroud)
命令行:
npm run build-windows
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢它,可以使用node.js中的命令.
包json:
"scripts" : {
"build" : "node build.js"
}
Run Code Online (Sandbox Code Playgroud)
Build.js
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
var os = require('os');
//control OS
//then run command depengin on the OS
if (os.type() === 'Linux')
exec("node build-linux.js", puts);
else if (os.type() === 'Darwin')
exec("node build-mac.js", puts);
else if (os.type() === 'Windows_NT')
exec("node build-windows.js", puts);
else
throw new Error("Unsupported OS found: " + os.type());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6753 次 |
| 最近记录: |