Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm package.json OS specific script

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 } 
like image 438
gawi Avatar asked Jul 13 '17 13:07

gawi


People also ask

How do I run a script defined package in json?

To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.

Can you specify node version in package json?

Use the engines keyword in the package. json file to specify the Node. js version that you want your application to use. You can also specify a version range using npm notation.

How do I run a script after npm install?

You can use npm hook scripts to do something after package is installed. Create node_modules/. hooks/postinstall executable and it will be run also after npm install <package> .


2 Answers

There's an NPM package called run-script-os ( NPM | GitHub ) that doesn't require you to write any additional files, and this can be convenient if what you're trying to do is very simple. For example, in your package.json, you might have something like:

"scripts": {     "test": "run-script-os",     "test:darwin:linux": "export NODE_ENV=test && mocha",     "test:win32": "SET NODE_ENV=test&& mocha" } 

Then you could run npm test on Windows, Mac, or Linux and get similar (or different!) results on each.

like image 56
Dave P Avatar answered Oct 01 '22 08:10

Dave P


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()); 
like image 39
hurricane Avatar answered Oct 01 '22 08:10

hurricane