Skip to content

Commit

Permalink
fix: don't prompt on npm exec [directory]
Browse files Browse the repository at this point in the history
Local directories have to be "installed" so that their bins are linked
and set up and callable, the user shouldn't need to be prompted to do
that.  Note that this does NOT affect anything passed via the
`--package` param, because that may also contain non-directory specs so
the existing behavior needs to be preserved.  This is a small QOL
improvement for the isolated use case of "npm exec [directory]"
  • Loading branch information
wraithgar committed Aug 11, 2022
1 parent 99ed4c9 commit 732d096
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
21 changes: 19 additions & 2 deletions workspaces/libnpmexec/lib/index.js
Expand Up @@ -132,7 +132,16 @@ const exec = async (opts) => {
packages.push(args[0])
}

// TODO We need any directiories in packages to pass through path.resolve
// Resolve any directory specs so that the npx directory is unique to the
// resolved directory, not the potentially relative one (i.e. "npx .")
for (const i in packages) {
const pkg = packages[i]
const spec = npa(pkg)
if (spec.type === 'directory') {
packages[i] = spec.fetchSpec
}
}

const localArb = new Arborist({ ...flatOptions, path })
const localTree = await localArb.loadActual()

Expand Down Expand Up @@ -184,7 +193,15 @@ const exec = async (opts) => {
throw new Error('Must provide a valid npxCache path')
}
const hash = crypto.createHash('sha512')
.update(packages.sort((a, b) => a.localeCompare(b, 'en')).join('\n'))
.update(packages.map(p => {
// Keeps the npx directory unique to the resolved directory, not the
// potentially relative one (i.e. "npx .")
const spec = npa(p)
if (spec.type === 'directory') {
return spec.fetchSpec
}
return p
}).sort((a, b) => a.localeCompare(b, 'en')).join('\n'))
.digest('hex')
.slice(0, 16)
const installDir = resolve(npxCache, hash)
Expand Down
11 changes: 10 additions & 1 deletion workspaces/libnpmexec/test/index.js
Expand Up @@ -482,7 +482,16 @@ require('fs').writeFileSync(process.argv.slice(2)[0], 'LOCAL PKG')`,
const executable = resolve(path, 'a/index.js')
fs.chmodSync(executable, 0o775)

await libexec({
const mockexec = t.mock('../lib/index.js', {
'@npmcli/ci-detect': () => true,
'proc-log': {
warn (title, msg) {
t.fail('should not warn about local file package install')
},
},
})

await mockexec({
...baseOpts,
args: [`file:${resolve(path, 'a')}`, 'resfile'],
cache,
Expand Down

0 comments on commit 732d096

Please sign in to comment.