Created
January 26, 2016 16:58
-
-
Save veeenu/a75b9fcf171ecac5939b to your computer and use it in GitHub Desktop.
Minimal node.js parallel task runner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const exec = require('child_process').exec; | |
| const conf = require('./package.json').buildjs || {}; | |
| const C0 = '\033[0m', | |
| C1 = '\033[1;34m', | |
| C2 = '\033[1;32m', | |
| C3 = '\033[1;31m'; | |
| const runCmd = (function() { | |
| var id = 0, trim = function(s) { return !!s? s.toString().replace(/^\s+|\s+$/g, '') : null; }; | |
| return function(str, opts) { | |
| var cid = id++, | |
| child = exec(str, opts); | |
| console.log(`[${C1}*${C0}] Process ${cid}: ${C1 + str + C0}`); | |
| child.stdout.on('data', (data) => { | |
| console.log(`[${C1 + cid + C0}] ${trim(data)}`); | |
| }); | |
| child.stderr.on('data', (data) => { | |
| console.log(`[${C3 + cid + C0}] ${trim(data)}`); | |
| }); | |
| child.on('close', (data) => { | |
| console.log(`[${C1 + cid + C0}] Process terminated with code ${C2 + trim(data) + C0}`); | |
| }); | |
| return child; | |
| } | |
| }()); | |
| var children = []; | |
| process.env.PATH = './node_modules/.bin;' + process.env.PATH; | |
| var tasks = process.argv.splice(2).reduce(function(o, i) { | |
| if(o.indexOf(i) === -1) { | |
| o.push(i); | |
| (conf[i].tasks || []).forEach(function(j) { | |
| o.push(j); | |
| }); | |
| } | |
| return o; | |
| }, []); | |
| tasks.forEach(function(i) { | |
| var task = conf[i]; | |
| if(!task) return; | |
| task.run.forEach(function(t) { | |
| children.push(runCmd(t, { | |
| cwd: task.cwd, | |
| env: task.env | |
| })); | |
| }); | |
| }); | |
| process.on('SIGINT', () => { | |
| children.forEach((i) => { | |
| i.kill(); | |
| }); | |
| console.log(`[${C1}*${C0}] Interrupted`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment