npm package.json操作系统特定的脚本

我想创build一个package.json构build脚本,从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 } 

您可以使用脚本与节点运行脚本命令。 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" } 

命令行:

 npm run build-windows 

如果你不喜欢,你可以在node.js中使用命令。

包json:

 "scripts" : { "build" : "node build.js" } 

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());