npm package.json OS specific script

gawi :

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
}
hurricane :

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.

edited at
0

Comments

0 comments
Login to comment

Related

Why does `npm install` hang with this specific `package.json`?

npm script, copy package.json to dist when bundling

Overriding package.json keys while running npm script command

Nodemon not working using npm script from package.json on Docker

Custom script in NPM package.json is ignored, runs as standalone

Hide specific exports in the npm package

npm: disable postinstall script for package

Python script not installing npm package

Is there any way to know whether an npm package has OS specific build requirements?

npm script command to run a script command from another package.json

Check if package.json has script with a certain name in shell script without using extra NPM packages

Npm package.json inheritance

Is there any way to fix package-lock.json lockfileVersion so npm uses a specific format?

Install npm dependencies for specific node version using package-lock.json

Find the package.json file from within an npm script that runs on preinstall

`npm build` doesn't run the script named "build" in package.json

" npm ERR! missing script: start" at Heroku despite set up in package.json and Procfile

npm build how to change package.json - homepage value by variable or script

child_process.execSync executing a npm run script cant find the package.json

How to npm publish specific folder but as package root

Skip a Specific npm Package on Build Definition on ADO

How to exclude a specific version of a npm package?

Call script from custom npm package

run npm script after package installing

NPM script to automate package increment and GIT push

How to fetch package version and date in npm script?

package.json script conditional

execute package.json script

How to call anaconda environment to run specific package of python(2.7) from other python(3.7) script via os.system()?