From 6c33af8e54a58277406f0ef03696756e582d8c71 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Sun, 14 Dec 2014 10:30:38 +0800 Subject: [PATCH 01/11] solve #306, and bin in $PATH make sense now Almost revert #173 which lead to the bug in #306 --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 77006c9fe..8a6aefc30 100644 --- a/index.js +++ b/index.js @@ -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. @@ -449,13 +450,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { var dir = dirname(argv[1]); var bin = basename(argv[1], '.js') + '-' + args[0]; - // check for ./ first + // prefer local `./` to bin in the $PATH var local = path.join(dir, bin); + if (fs.existsSync(local)) bin = local; // run it args = args.slice(1); - args.unshift(local); - var proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] }); + + var proc = spawn(bin, args, { stdio: 'inherit'} ); proc.on('error', function(err) { if (err.code == "ENOENT") { console.error('\n %s(1) does not exist, try --help\n', bin); From efa2d82595118d7a04882cfe8d0fb695d6f86f66 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Thu, 25 Dec 2014 00:28:39 +0800 Subject: [PATCH 02/11] fix windows platform --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 730f8e909..30e573e2b 100644 --- a/index.js +++ b/index.js @@ -468,7 +468,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // run it args = args.slice(1); - var proc = spawn(bin, args, { stdio: 'inherit'} ); + var proc; + if (process.platform !== 'win32') { + proc = spawn(bin, args, { stdio: 'inherit'}); + } else { + args.unshift(local); + proc = spawn('node', args, { stdio: 'inherit'}); + } + proc.on('error', function(err) { if (err.code == "ENOENT") { console.error('\n %s(1) does not exist, try --help\n', bin); From 2ee5c66b7653ce8a2a7ce012422a3629e9ebdc1e Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Thu, 25 Dec 2014 23:22:07 +0800 Subject: [PATCH 03/11] Binary might not be called "node". For example, on ubuntu it's sometimes called "nodejs", and on windows it might be renamed too because of the conflict with node.exe service from microsoft. thx @rlidwka --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 30e573e2b..795c51c2f 100644 --- a/index.js +++ b/index.js @@ -473,7 +473,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { proc = spawn(bin, args, { stdio: 'inherit'}); } else { args.unshift(local); - proc = spawn('node', args, { stdio: 'inherit'}); + proc = spawn(process.execPath, args, { stdio: 'inherit'}); } proc.on('error', function(err) { From 0d676c5290f8438156401f7b954278f5cc91ae22 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Thu, 25 Dec 2014 23:58:05 +0800 Subject: [PATCH 04/11] add tests --- test/test.command.executableSubcommand.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/test.command.executableSubcommand.js diff --git a/test/test.command.executableSubcommand.js b/test/test.command.executableSubcommand.js new file mode 100644 index 000000000..b005f6cbc --- /dev/null +++ b/test/test.command.executableSubcommand.js @@ -0,0 +1,20 @@ +var program = require('../') + , util = require('util') + , should = require('should'); + +var oldError = console.error; +var err; +console.error = function (tpl, s) { + err = util.format(tpl, s); +}; +program + .command('exec [options]', 'this is my command'); + +program.parse('node test exec a'.split(' ')); + +process.nextTick(function () { + err.should.equal('\n test-exec(1) does not exist, try --help\n'); +}); + +console.error = oldError; + From 692946233f0f9ee808305e7126ad009e5a71bcf4 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Fri, 26 Dec 2014 00:00:17 +0800 Subject: [PATCH 05/11] fix test error --- test/test.command.executableSubcommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.command.executableSubcommand.js b/test/test.command.executableSubcommand.js index b005f6cbc..fb5211544 100644 --- a/test/test.command.executableSubcommand.js +++ b/test/test.command.executableSubcommand.js @@ -14,7 +14,7 @@ program.parse('node test exec a'.split(' ')); process.nextTick(function () { err.should.equal('\n test-exec(1) does not exist, try --help\n'); + console.error = oldError; }); -console.error = oldError; From c936e4722b6e2867272de93972b4e57a4713e755 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Sun, 14 Dec 2014 10:30:38 +0800 Subject: [PATCH 06/11] solve #306, and bin in $PATH make sense now Almost revert #173 which lead to the bug in #306 --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e0299d5c6..8ad7517bd 100644 --- a/index.js +++ b/index.js @@ -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. @@ -460,13 +461,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { var dir = dirname(argv[1]); var bin = basename(argv[1], '.js') + '-' + args[0]; - // check for ./ first + // prefer local `./` to bin in the $PATH var local = path.join(dir, bin); + if (fs.existsSync(local)) bin = local; // run it args = args.slice(1); - args.unshift(local); - var proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] }); + + var proc = spawn(bin, args, { stdio: 'inherit'} ); proc.on('error', function(err) { if (err.code == "ENOENT") { console.error('\n %s(1) does not exist, try --help\n', bin); From c366dd8da14c3b27bd730466f3ce8e051470d18a Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Thu, 25 Dec 2014 00:28:39 +0800 Subject: [PATCH 07/11] fix windows platform --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8ad7517bd..0f51ce11a 100644 --- a/index.js +++ b/index.js @@ -468,7 +468,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // run it args = args.slice(1); - var proc = spawn(bin, args, { stdio: 'inherit'} ); + var proc; + if (process.platform !== 'win32') { + proc = spawn(bin, args, { stdio: 'inherit'}); + } else { + args.unshift(local); + proc = spawn('node', args, { stdio: 'inherit'}); + } + proc.on('error', function(err) { if (err.code == "ENOENT") { console.error('\n %s(1) does not exist, try --help\n', bin); From 8980da63b033c5d6fbeffebad8297ccce017f4f0 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Thu, 25 Dec 2014 23:22:07 +0800 Subject: [PATCH 08/11] Binary might not be called "node". For example, on ubuntu it's sometimes called "nodejs", and on windows it might be renamed too because of the conflict with node.exe service from microsoft. thx @rlidwka --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0f51ce11a..25d80ad28 100644 --- a/index.js +++ b/index.js @@ -473,7 +473,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { proc = spawn(bin, args, { stdio: 'inherit'}); } else { args.unshift(local); - proc = spawn('node', args, { stdio: 'inherit'}); + proc = spawn(process.execPath, args, { stdio: 'inherit'}); } proc.on('error', function(err) { From ffc24d0f3ad810a01f078762a8474c07272ab305 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Thu, 25 Dec 2014 23:58:05 +0800 Subject: [PATCH 09/11] add tests --- test/test.command.executableSubcommand.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/test.command.executableSubcommand.js diff --git a/test/test.command.executableSubcommand.js b/test/test.command.executableSubcommand.js new file mode 100644 index 000000000..b005f6cbc --- /dev/null +++ b/test/test.command.executableSubcommand.js @@ -0,0 +1,20 @@ +var program = require('../') + , util = require('util') + , should = require('should'); + +var oldError = console.error; +var err; +console.error = function (tpl, s) { + err = util.format(tpl, s); +}; +program + .command('exec [options]', 'this is my command'); + +program.parse('node test exec a'.split(' ')); + +process.nextTick(function () { + err.should.equal('\n test-exec(1) does not exist, try --help\n'); +}); + +console.error = oldError; + From 0062ba7a73ca37b3272aaf15a7e3747751a9c370 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Fri, 26 Dec 2014 00:00:17 +0800 Subject: [PATCH 10/11] fix test error --- test/test.command.executableSubcommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.command.executableSubcommand.js b/test/test.command.executableSubcommand.js index b005f6cbc..fb5211544 100644 --- a/test/test.command.executableSubcommand.js +++ b/test/test.command.executableSubcommand.js @@ -14,7 +14,7 @@ program.parse('node test exec a'.split(' ')); process.nextTick(function () { err.should.equal('\n test-exec(1) does not exist, try --help\n'); + console.error = oldError; }); -console.error = oldError; From 505983b615691dfc0f76a45ab31a68e4310e1e44 Mon Sep 17 00:00:00 2001 From: zhiyelee Date: Fri, 9 Jan 2015 01:23:00 +0800 Subject: [PATCH 11/11] fix test errors --- index.js | 7 ++++++- test/test.command.executableSubcommand.js | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 25d80ad28..c511d09d7 100644 --- a/index.js +++ b/index.js @@ -463,7 +463,12 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // prefer local `./` to bin in the $PATH var local = path.join(dir, bin); - if (fs.existsSync(local)) bin = local; + 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); diff --git a/test/test.command.executableSubcommand.js b/test/test.command.executableSubcommand.js index fb5211544..5213be33d 100644 --- a/test/test.command.executableSubcommand.js +++ b/test/test.command.executableSubcommand.js @@ -3,7 +3,7 @@ var program = require('../') , should = require('should'); var oldError = console.error; -var err; +var err = ''; console.error = function (tpl, s) { err = util.format(tpl, s); }; @@ -12,9 +12,9 @@ program program.parse('node test exec a'.split(' ')); -process.nextTick(function () { +program.runningCommand.on('error', function() { err.should.equal('\n test-exec(1) does not exist, try --help\n'); - console.error = oldError; }); +// @todo test `EACCES` error