Skip to content

Instantly share code, notes, and snippets.

@morganrallen
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganrallen/9222028 to your computer and use it in GitHub Desktop.
Save morganrallen/9222028 to your computer and use it in GitHub Desktop.
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

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