Skip to content

Commit

Permalink
fix tj#372
Browse files Browse the repository at this point in the history
fix the bug discussed in tj#372
Also merge the `execArg` change in tj#305, thx @DigitalIO
  • Loading branch information
zhiyelee committed Apr 3, 2015
1 parent ec55bd5 commit 7b023b1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
56 changes: 43 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,30 +481,48 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {

// executable
var f = argv[1];
var link = readlink(f);
// name of the subcommand, link `pm-install`
var bin = basename(f, '.js') + '-' + args[0];


// In case of globally installed, get the base dir where executable
// subcommand file should be located at
var baseDir
, link = readlink(f);

// when symbolink is relative path
if (link !== f && link.charAt(0) !== '/') {
link = path.join(dirname(f), link)
}
var dir = dirname(link);
var bin = basename(f, '.js') + '-' + args[0];
baseDir = dirname(link);

// 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) {}
var localBin = path.join(baseDir, bin);

// whether bin file is a js script with explicit `.js` extension
var isExplicitJS = false;
if (exists(localBin + '.js')) {
bin = localBin + '.js';
isExplicitJS = true;
} else if (exists(localBin)) {
bin = localBin;
}

// run it
args = args.slice(1);

var proc;
if (process.platform !== 'win32') {
proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
if (isExplicitJS) {
args.unshift(localBin);
// add executable arguments to spawn
args = process.execArgv.concat(args);

proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
} else {
proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
}
} else {
args.unshift(local);
args.unshift(localBin);
proc = spawn(process.execPath, args, { stdio: 'inherit'});
}

Expand Down Expand Up @@ -1081,3 +1099,15 @@ function humanReadableArgName(arg) {
? '<' + nameOutput + '>'
: '[' + nameOutput + ']'
}

// for versions before node v0.8 when there weren't `fs.existsSync`
function exists(file) {
try {
if (fs.statSync(file).isFile()) {
return true;
}
} catch (e) {
return false;
}
}

1 change: 1 addition & 0 deletions test/fixtures/pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ program
.command('install [name]', 'install one or more packages')
.command('search [query]', 'search with optional query')
.command('list', 'list packages installed')
.command('publish', 'publish or update package')
.parse(process.argv);
2 changes: 2 additions & 0 deletions test/fixtures/pm-publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('publish');

5 changes: 5 additions & 0 deletions test/test.command.executableSubcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ exec(bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

// subcommand bin file with explicit extension
exec(bin + ' publish', function (error, stdout, stderr) {
stdout.should.equal('publish\n');
});

// spawn EACCES
exec(bin + ' search', function (error, stdout, stderr) {
// TODO error info are not the same in between <v0.10 and v0.12
Expand Down

0 comments on commit 7b023b1

Please sign in to comment.