Skip to content

Instantly share code, notes, and snippets.

@dead-horse
Last active August 29, 2015 13:56
Show Gist options
  • Save dead-horse/9047153 to your computer and use it in GitHub Desktop.
Save dead-horse/9047153 to your computer and use it in GitHub Desktop.
Command.prototype.executeSubCommand = function(argv, args, unknown) {
args = args.concat(unknown);
if (!args.length) this.help();
if ('help' == args[0] && 1 == args.length) this.help();
// <cmd> --help
if ('help' == args[0]) {
args[0] = args[1];
args[1] = '--help';
}
// executable
var dir = dirname(argv[1]);
var bin = basename(argv[1]) + '-' + args[0];
// check for ./<bin> first
var local = path.join(dir, bin);
// run it
args = args.slice(1);
var proc = spawn(local, args, { stdio: 'inherit', customFds: [0, 1, 2] });
proc.on('error', function(err){
if (err.code == "ENOENT") {
if (process.platform == 'win32') {
return _executeSubCommandInWin();
}
console.error('\n %s(1) does not exist, try --help\n', bin);
} else if (err.code == "EACCES") {
console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
}
});
this.runningCommand = proc;
// try to excute sub command with node in windows
var self = this;
function _executeSubCommandInWin() {
args.unshift(local);
proc = spawn(process.execPath, args, { stdio: 'inherit', customFds: [0, 1, 2] });
proc.on('error', function () {
if (err.code == "ENOENT") {
console.error('\n %s(1) does not exist, try --help\n', bin);
} else if (err.code == "EACCES") {
console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
}
});
self.runningCommand = proc;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment