Skip to content
Myr

Merge pull request #387 from zhiyelee/bugfix_subcommand

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fix #372

authored by Zhiye Li · committed by Zhiye Li · parents 2666457, 7def9f1 · 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
487 unmodified lines
488 }489490 // executable491 var f = argv[1];492 // name of the subcommand, link `pm-install`493 var bin = basename(f, '.js') + '-' + args[0];494495496 // In case of globally installed, get the base dir where executable497 // subcommand file should be located at498 var baseDir492 var link = readlink(f);499 , link = readlink(f);500501 // when symbolink is relative path502 if (link !== f && link.charAt(0) !== '/') {503 link = path.join(dirname(f), link)504 }496 var dir = dirname(link);497 var bin = basename(f, '.js') + '-' + args[0];505 baseDir = dirname(link);506507 // prefer local `./<bin>` to bin in the $PATH500 var local = path.join(dir, bin);501 try {502 // for versions before node v0.8 when there weren't `fs.existsSync`503 if (fs.statSync(local).isFile()) {504 bin = local;505 }506 } catch (e) {}508 var localBin = path.join(baseDir, bin);509508 // run it510 // whether bin file is a js script with explicit `.js` extension511 var isExplicitJS = false;512 if (exists(localBin + '.js')) {513 bin = localBin + '.js';514 isExplicitJS = true;515 } else if (exists(localBin)) {516 bin = localBin;517 }518519 args = args.slice(1);520521 var proc;522 if (process.platform !== 'win32') {523 if (isExplicitJS) {524 args.unshift(localBin);525 // add executable arguments to spawn526 args = (process.execArgv || []).concat(args);527528 proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });529 } else {513 proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });530 proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });531 }532 } else {515 args.unshift(local);533 args.unshift(localBin);534 proc = spawn(process.execPath, args, { stdio: 'inherit'});535 }536537 proc.on('close', process.exit.bind(process));
570 unmodified lines
1108 return arg.required1109 ? '<' + nameOutput + '>'1110 : '[' + nameOutput + ']'1111}11121113// for versions before node v0.8 when there weren't `fs.existsSync`1114function exists(file) {1115 try {1116 if (fs.statSync(file).isFile()) {1117 return true;1118 }1119 } catch (e) {1120 return false;1121 }1122}1123