Skip to content

Instantly share code, notes, and snippets.

@morganrallen
Last active Aug 29, 2015
Embed
What would you like to do?
NPM script runner.
var path = require("path");
var spawn = require("child_process").spawn;
var scripts = process.argv.slice(2);
console.log("Running from %s", process.cwd());
scripts.forEach(function(script) {
console.log("[executing] npm run %s", script);
var proc = spawn("npm", ["run", script]);
proc.stdout.on("data", function(data) {
process.stdout.write("[" + script + "] " + data);
});
proc.stderr.on("data", function(data) {
process.stderr.write("[" + script + "] " + data);
});
});
{
"name": "some-project",
"version": "0.0.0",
"description": "its just this project, you know?",
"main": "index.js",
"scripts": {
"watchify": "watchify src/js/index.js -d -o app/js/app.js",
"less-dev": "cd src/less && nodemon -x lessc ./index.less ../../app/css/index.css",
"dev-server": "nodemon server.js",
"dev": "node scripts/dev.js watchify less-dev dev-server"
},
"author": "Morgan \"ARR!\" Allen",
"license": "Beerware"
}
@morganrallen
Copy link
Author

morganrallen commented Feb 26, 2014

Demonstrates the direction I've been going with using NPM as an alternative to Grunt, gulp, derp, what ever. When ditching Grunt one of the main problems I encountered was not easily being able to run multiple script, maintaining output from a single command. Telling colleagues to run 3 commands in 3 terminals is a bit unacceptable for me.

Read the very timely (for me at least) article from @substack for starting context.
http://substack.net/task_automation_with_npm_run

But two things didn't sit well for me. Firstly using command & command background all but the last command, rendering them unkillable by a normal CTRL-C. Deal breaker. Secondly, these are too Bash-centric (or *nix even) in my opinion and
using purely a Node script should maintain reasonable compatibility with Window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment