fix #372
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fix the bug discussed in #372 Also merge the `execArg` change in #305, thx @DigitalIO
authored by zhiyelee · committed by zhiyelee · parent ec55bd5 · Browse files · 4 files·+51 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+51 added−13 removed
- tests:+8 added−0 removed
- other:+43 added−13 removed
Provenance
- Commit
7b023b1- Tree
a72d51b- Author
zhiyelee <zhiyelee@gmail.com>- Committer
zhiyelee <zhiyelee@gmail.com>- Parent
ec55bd5
4 files changed+51 added−13 removed
modified:test/fixtures/pm+1 added
5 unmodified lines6 .version('0.0.1')7 .command('install [name]', 'install one or more packages')8 .command('search [query]', 'search with optional query')9 .command('list', 'list packages installed')10+ .command('publish', 'publish or update package')11 .parse(process.argv);added:test/fixtures/pm-publish.js+2 added
12console.log('publish');
modified:test/test.command.executableSubcommand.js+5 added
15 unmodified lines161718192021222324252627288 unmodified lines15 unmodified linesexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});
// subcommand bin file with explicit extensionexec(bin + ' publish', function (error, stdout, stderr) { stdout.should.equal('publish\n');});
// spawn EACCESexec(bin + ' search', function (error, stdout, stderr) { // TODO error info are not the same in between <v0.10 and v0.12 should.notEqual(0, stderr.length);8 unmodified linesmodified:index.js+43 added-13 removed
479 unmodified lines480 }481 482 // executable483 var f = argv[1];484+ // name of the subcommand, link `pm-install`485+ var bin = basename(f, '.js') + '-' + args[0];486+487+488+ // In case of globally installed, get the base dir where executable489+ // subcommand file should be located at490+ var baseDir484- var link = readlink(f);491+ , link = readlink(f);492+493+ // when symbolink is relative path494 if (link !== f && link.charAt(0) !== '/') {495 link = path.join(dirname(f), link)496 }488- var dir = dirname(link);489- var bin = basename(f, '.js') + '-' + args[0];497+ baseDir = dirname(link);498 499 // prefer local `./<bin>` to bin in the $PATH492- var local = path.join(dir, bin);493- try {494- // for versions before node v0.8 when there weren't `fs.existsSync`495- if (fs.statSync(local).isFile()) {496- bin = local;497- }498- } catch (e) {}500+ var localBin = path.join(baseDir, bin);501 500- // run it502+ // whether bin file is a js script with explicit `.js` extension503+ var isExplicitJS = false;504+ if (exists(localBin + '.js')) {505+ bin = localBin + '.js';506+ isExplicitJS = true;507+ } else if (exists(localBin)) {508+ bin = localBin;509+ }510+511 args = args.slice(1);512 513 var proc;514 if (process.platform !== 'win32') {515+ if (isExplicitJS) {516+ args.unshift(localBin);517+ // add executable arguments to spawn518+ args = process.execArgv.concat(args);519+520+ proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });521+ } else {505- proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });522+ proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });523+ }524 } else {507- args.unshift(local);525+ args.unshift(localBin);526 proc = spawn(process.execPath, args, { stdio: 'inherit'});527 }528 529 proc.on('close', process.exit.bind(process));568 unmodified lines1098 return arg.required1099 ? '<' + nameOutput + '>'1100 : '[' + nameOutput + ']'1101 }1102+1103+// for versions before node v0.8 when there weren't `fs.existsSync`1104+function exists(file) {1105+ try {1106+ if (fs.statSync(file).isFile()) {1107+ return true;1108+ }1109+ } catch (e) {1110+ return false;1111+ }1112+}1113+