go-pathman/npm/lib/exec.js

37 lines
945 B
JavaScript
Raw Permalink Normal View History

2019-08-09 09:39:18 +00:00
'use strict';
var pkg = require('../package.json');
var spawn = require('child_process').spawn;
var os = require('os');
var path = require('path');
var ext = /^win/i.test(os.platform()) ? '.exe' : '';
// @scope/packagename => packagename
// { bin: { "packagename": "bin/runner" } } => "bin/runner"
2019-08-09 09:45:29 +00:00
var bin = path.resolve(__dirname, '..', pkg.bin[pkg.name.replace(/.*\//, '')]);
2019-08-09 09:39:18 +00:00
function spawner(args) {
return new Promise(function(resolve, reject) {
var runner = spawn(path.join(bin + ext), args, {
windowsHide: true
});
runner.stdout.on('data', function(chunk) {
console.info(chunk.toString('utf8'));
});
runner.stderr.on('data', function(chunk) {
2019-08-10 07:17:32 +00:00
console.error(chunk.toString('utf8'));
2019-08-09 09:39:18 +00:00
});
runner.on('exit', function(code) {
if (0 !== code) {
reject(
new Error("exited with non-zero status code '" + code + "'")
);
return;
}
resolve({ code: code });
});
});
}
module.exports = spawner;