Skip to content

Commit

Permalink
solve tj#306, and bin in $PATH make sense
Browse files Browse the repository at this point in the history
solve tj#306, and bin in $PATH make sense now tj#196
Almost revert tj#173 which lead to the bug in tj#306

Also remove deprecated customFds option in spawn
  • Loading branch information
zhiyelee committed Jan 10, 2015
1 parent c6807fd commit 154fb8b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var spawn = require('child_process').spawn;
var path = require('path');
var dirname = path.dirname;
var basename = path.basename;
var fs = require('fs');

/**
* Expose the root command.
Expand Down Expand Up @@ -460,13 +461,26 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
var dir = dirname(argv[1]);
var bin = basename(argv[1], '.js') + '-' + args[0];

// check for ./<bin> first
// prefer local `./<bin>` to bin in the $PATH
var local = path.join(dir, bin);
try {
// for versions before node v0.8 when there weren't `fs.existsSync`
if (fs.statSync(local).isFile()) {
bin = local;
}
} catch (e) {}

// run it
args = args.slice(1);
args.unshift(local);
var proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });

var proc;
if (process.platform !== 'win32') {
proc = spawn(bin, args, { stdio: 'inherit'});
} else {
args.unshift(local);
proc = spawn(process.execPath, args, { stdio: 'inherit'});
}

proc.on('error', function(err) {
if (err.code == "ENOENT") {
console.error('\n %s(1) does not exist, try --help\n', bin);
Expand Down

0 comments on commit 154fb8b

Please sign in to comment.