I would like to create a package.json
build script that executes slightly different set of commands when run from Windows, Linux, Mac.
The problem is that I cannot find a way to put it in package.json
file that will run without problems at every system.
Here is an example that I would like to have:
"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
}
You can use scripts with node run-script command. npm run
is a shortcut of it.
Package json:
"scripts" : {
"build-windows" : "node build-windows.js",
"build-linux" : "node build-linux.js",
"build-mac" : "node build-mac.js",
"build" : "node build.js"
}
Command line:
npm run build-windows
If you don't like it, you can use commands inside node.js.
Package json:
"scripts" : {
"build" : "node build.js"
}
Build.js
var sys = require('sys');
var exec = require('child_process').exec;
var os = require('os');
function puts(error, stdout, stderr) { sys.puts(stdout) }
// Run command depending 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());
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments