Externally hosted "npm run" scripts in a .json package on Windows?

As we know, you can run arbitrary commands using npm run by adding a hash of scripts to your package.json :

 "scripts": { "build-js": "browserify browser/main.js | uglifyjs -mc > static/bundle.js" } 

which will then be launched with npm run build-js .

You can also move these commands to separate scripts, such as bash scripts, as such:

 "scripts": { "build-js": "bin/build.sh" } 

This obviously does not work on Windows due to the fact that Windows cannot run bash scripts. You can install bash ports, etc., but I would like to use some design for Windows to do the same.

I tried other approaches, including using child_process.exec to run arbitrary commands from a standard node script file:

 "scripts": { "build-js": "node bin/build.js" } 

But I noticed child_process chokes in relatively large / intensive operations, which makes use impossible.

Is there any Windows (or even better, cross-platform) way to move these package.json npm run scripts to separate files? Preferably one that does not require bash?

+6
source share
3 answers

From a useful article on using NPM as a build tool, why not just use a JavaScript file?

Here is an example in an article (slightly modified for clarity):

 // scripts/favicon.js var favicons = require('favicons'); var path = require('path'); favicons({ source: path.resolve('../assets/images/logo.png'), dest: path.resolve('../dist/'), }); // package.json "scripts": { "build-favicon": "node scripts/favicon.js", }, "devDependencies": { "favicons": "latest", } 

which will be launched in the terminal using the npm run build-favicon command

+11
source

Using node to run the JS file is an option, but if you need to run system commands for both Windows and * NIX, this will make the script useless. For cross-platform scripts, try the shelljs package.

+3
source

Using the make also a good option.

Makefile

 build-js: browserify browser/main.js | uglifyjs -mc > static/bundle.js 

package.json

 "scripts": { "build-js": "make build-js", } 

Read more about make here .

0
source

All Articles