just-build
Version:
A simple task runner that doesn't bloat your package
50 lines (45 loc) • 1.73 kB
JavaScript
var ref = require('child_process');
var spawn = ref.spawn;
var fork = ref.fork;
var ref$1 = require('./tokenize');
var surroundWithQuotes = ref$1.surroundWithQuotes;
var ref$2 = require ('./which-local');
var whichLocal = ref$2.whichLocal;
var path = require('path');
var fs = require('fs');
var clr = require ('./console-colors');
var debug = require('./debug');
function spawnOrFork (cmd, args, ref) {
var cwd = ref.cwd;
var env = ref.env;
var locallyInstalledBinaryModuleEntry = whichLocal (cmd, cwd);
if (locallyInstalledBinaryModuleEntry) {
// Use fork instead of spawn to save resources.
var relativeBinPath = path.relative(cwd, locallyInstalledBinaryModuleEntry);
console.log(((clr.DIM) + "(node)> " + (clr.CYAN) + cmd + " " + (args.join(' ')) + (clr.RESET)));
return fork(locallyInstalledBinaryModuleEntry, args, {
cwd: cwd,
env: env,
silent: true
});
} else if (cmd === "node" && fs.existsSync(path.resolve(cwd, args[0]))) {
console.log(((clr.DIM) + "(node)> " + (clr.CYAN) + (args[0]) + " " + (args.slice(1).join(' ')) + (clr.RESET)));
return fork(args[0], args.slice(1), {
cwd: cwd,
env: env,
silent: true
});
} else {
debug.log(("Using spawn: " + cmd));
cmd = surroundWithQuotes(cmd);
debug.log(("cmd = " + cmd));
args = args.map(surroundWithQuotes);
console.log(((clr.DIM) + "> " + (clr.CYAN) + cmd + " " + (args.join(' ')) + (clr.RESET)));
return spawn(cmd, args, {
cwd: cwd,
env: env,
shell: true
});
}
}
module.exports = { spawnOrFork: spawnOrFork };