Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit 139c434

Browse files
committed
feat(exec): auto-guess binaries when different from pkg name
BREAKING CHANGE: `npx ember-cli` and such things will now execute the binary based on some guesswork, but only when using the shorthand format for npx execution, with no `-p` option or `-c`. This might cause npx to unintentionally execute the wrong binary if the package in question has multiple non-matching binaries, but that should be rare.
1 parent 8d071da commit 139c434

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,26 @@ function main (argv) {
7171
require('update-notifier')({pkg: require('./package.json')}).notify()
7272
// Some npm packages need to be installed. Let's install them!
7373
return ensurePackages(argv.package, argv).then(results => {
74-
results && !argv.q && console.error(Y()`npx: installed ${
75-
results.added.length + results.updated.length
76-
} in ${(Date.now() - startTime) / 1000}s`)
77-
}).then(() => existing)
74+
if (results && results.added && results.updated && !argv.q) {
75+
console.error(Y()`npx: installed ${
76+
results.added.length + results.updated.length
77+
} in ${(Date.now() - startTime) / 1000}s`)
78+
}
79+
if (
80+
argv.command &&
81+
!existing &&
82+
!argv.packageRequested &&
83+
argv.package.length === 1
84+
) {
85+
return promisify(fs.readdir)(results.bin).then(bins => {
86+
const cmd = new RegExp(`^${argv.command}(?:\\.cmd)?$`, 'i')
87+
const matching = bins.find(b => b.match(cmd))
88+
return path.resolve(results.bin, bins[matching] || bins[0])
89+
})
90+
} else {
91+
return existing
92+
}
93+
})
7894
} else {
7995
// We can skip any extra installation, 'cause everything exists.
8096
return existing
@@ -116,6 +132,9 @@ function ensurePackages (specs, opts) {
116132
// This is intentional, since npx assumes that if you went through
117133
// the trouble of doing `-p`, you're rather have that one. Right? ;)
118134
process.env.PATH = `${bins}${PATH_SEP}${process.env.PATH}`
135+
if (!info) { info = {} }
136+
info.prefix = prefix
137+
info.bin = bins
119138
return info
120139
})
121140
})

0 commit comments

Comments
 (0)