Skip to content
Myr

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

4 files+51 added−13 removed

modified:test/fixtures/pm+1 added
5 unmodified lines
6 .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
modified:test/test.command.executableSubcommand.js+5 added
modified:index.js+43 added-13 removed
479 unmodified lines
480 }481482 // executable483 var f = argv[1];484 // name of the subcommand, link `pm-install`485 var bin = basename(f, '.js') + '-' + args[0];486487488 // 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);492493 // 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);498499 // 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);501500 // 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 }510511 args = args.slice(1);512513 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);519520 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 }528529 proc.on('close', process.exit.bind(process));
568 unmodified lines
1098 return arg.required1099 ? '<' + nameOutput + '>'1100 : '[' + nameOutput + ']'1101}11021103// for versions before node v0.8 when there weren't `fs.existsSync`1104function exists(file) {1105 try {1106 if (fs.statSync(file).isFile()) {1107 return true;1108 }1109 } catch (e) {1110 return false;1111 }1112}1113