Skip to content

Commit

Permalink
fix: properly find locally/globally/npxCache packages
Browse files Browse the repository at this point in the history
Lots of bugfixes here, we properly parse ranges and versions, and we
also now work with git repos and gists, and know when they are already
installed.
  • Loading branch information
wraithgar authored and nlf committed Aug 2, 2022
1 parent d0be9a2 commit ea44995
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 155 deletions.
2 changes: 1 addition & 1 deletion docs/content/commands/npm-exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ $ npm exec -- foo@latest bar --package=@npmcli/foo
* Default:
* Type: String (can be set multiple times)

The package to install for [`npm exec`](/commands/npm-exec)
The package or packages to install for [`npm exec`](/commands/npm-exec)

<!-- automatically generated, do not edit manually -->
<!-- see lib/utils/config/definitions.js -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/using-npm/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ Directory in which `npm pack` will save tarballs.
* Default:
* Type: String (can be set multiple times)

The package to install for [`npm exec`](/commands/npm-exec)
The package or packages to install for [`npm exec`](/commands/npm-exec)

<!-- automatically generated, do not edit manually -->
<!-- see lib/utils/config/definitions.js -->
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class Exec extends BaseCommand {
static isShellout = true

async exec (_args, { locationMsg, runPath } = {}) {
// This is where libnpmexec will look for locally installed packages
const path = this.npm.localPrefix

// This is where libnpmexec will actually run the scripts from
if (!runPath) {
runPath = process.cwd()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ define('package', {
hint: '<package-spec>',
type: [String, Array],
description: `
The package to install for [\`npm exec\`](/commands/npm-exec)
The package or packages to install for [\`npm exec\`](/commands/npm-exec)
`,
flatten,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ exports[`test/lib/utils/config/definitions.js TAP > config description for packa
* Default:
* Type: String (can be set multiple times)
The package to install for [\`npm exec\`](/commands/npm-exec)
The package or packages to install for [\`npm exec\`](/commands/npm-exec)
`

exports[`test/lib/utils/config/definitions.js TAP > config description for package-lock 1`] = `
Expand Down
20 changes: 0 additions & 20 deletions workspaces/libnpmexec/lib/cache-install-dir.js

This file was deleted.

22 changes: 12 additions & 10 deletions workspaces/libnpmexec/lib/file-exists.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const { resolve } = require('path')
const { promisify } = require('util')
const stat = promisify(require('fs').stat)
const fs = require('@npmcli/fs')
const walkUp = require('walk-up-path')

const fileExists = (file) => stat(file)
.then((res) => res.isFile())
.catch(() => false)

const localFileExists = async (dir, binName, root = '/') => {
root = resolve(root).toLowerCase()
const fileExists = async (file) => {
try {
const res = await fs.stat(file)
return res.isFile()
} catch {
return false
}
}

for (const path of walkUp(resolve(dir))) {
const localFileExists = async (dir, binName, root) => {
for (const path of walkUp(dir)) {
const binDir = resolve(path, 'node_modules', '.bin')

if (await fileExists(resolve(binDir, binName))) {
return binDir
}

if (path.toLowerCase() === root) {
if (path.toLowerCase() === resolve(root).toLowerCase()) {
return false
}
}
Expand Down
Loading

0 comments on commit ea44995

Please sign in to comment.