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
Provenance
- Commit
dae7802- Tree
d7aa43f- Author
Zhiye Li <zhiyelee@gmail.com>- Committer
Zhiye Li <zhiyelee@gmail.com>- Parent 1
2666457- Parent 2
7def9f1
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
487 unmodified lines488 }489 490 // executable491 var f = argv[1];492+ // name of the subcommand, link `pm-install`493+ var bin = basename(f, '.js') + '-' + args[0];494+495+496+ // 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);500+501+ // 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);506 507 // 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);509 508- // 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+ }518+519 args = args.slice(1);520 521 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);527+528+ 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 }536 537 proc.on('close', process.exit.bind(process));570 unmodified lines1108 return arg.required1109 ? '<' + nameOutput + '>'1110 : '[' + nameOutput + ']'1111 }1112+1113+// for versions before node v0.8 when there weren't `fs.existsSync`1114+function exists(file) {1115+ try {1116+ if (fs.statSync(file).isFile()) {1117+ return true;1118+ }1119+ } catch (e) {1120+ return false;1121+ }1122+}1123+