Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gar/libnpmexec fixes #5244

Merged
merged 6 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions DEPENDENCIES.md
Expand Up @@ -35,13 +35,15 @@ graph LR;
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
libnpmexec-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmexec-->npmcli-fs["@npmcli/fs"];
libnpmexec-->npmcli-run-script["@npmcli/run-script"];
libnpmexec-->npmcli-template-oss["@npmcli/template-oss"];
libnpmexec-->npmlog;
libnpmexec-->pacote;
libnpmexec-->proc-log;
libnpmexec-->read-package-json-fast;
libnpmexec-->read;
libnpmexec-->semver;
libnpmfund-->npmcli-arborist["@npmcli/arborist"];
libnpmfund-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmfund-->npmcli-template-oss["@npmcli/template-oss"];
Expand Down Expand Up @@ -342,13 +344,15 @@ graph LR;
libnpmexec-->npmcli-arborist["@npmcli/arborist"];
libnpmexec-->npmcli-ci-detect["@npmcli/ci-detect"];
libnpmexec-->npmcli-eslint-config["@npmcli/eslint-config"];
libnpmexec-->npmcli-fs["@npmcli/fs"];
libnpmexec-->npmcli-run-script["@npmcli/run-script"];
libnpmexec-->npmcli-template-oss["@npmcli/template-oss"];
libnpmexec-->npmlog;
libnpmexec-->pacote;
libnpmexec-->proc-log;
libnpmexec-->read-package-json-fast;
libnpmexec-->read;
libnpmexec-->semver;
libnpmexec-->tap;
libnpmexec-->walk-up-path;
libnpmfund-->npmcli-arborist["@npmcli/arborist"];
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-exec.md
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
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
36 changes: 6 additions & 30 deletions lib/commands/exec.js
@@ -1,31 +1,5 @@
const libexec = require('libnpmexec')
const BaseCommand = require('../base-command.js')
const getLocationMsg = require('../exec/get-workspace-location-msg.js')

// it's like this:
//
// npm x pkg@version <-- runs the bin named "pkg" or the only bin if only 1
//
// { name: 'pkg', bin: { pkg: 'pkg.js', foo: 'foo.js' }} <-- run pkg
// { name: 'pkg', bin: { foo: 'foo.js' }} <-- run foo?
//
// npm x -p pkg@version -- foo
//
// npm x -p pkg@version -- foo --registry=/dev/null
//
// const pkg = npm.config.get('package') || getPackageFrom(args[0])
// const cmd = getCommand(pkg, args[0])
// --> npm x -c 'cmd ...args.slice(1)'
//
// we've resolved cmd and args, and escaped them properly, and installed the
// relevant packages.
//
// Add the ${npx install prefix}/node_modules/.bin to PATH
//
// pkg = readPackageJson('./package.json')
// pkg.scripts.___npx = ${the -c arg}
// runScript({ pkg, event: 'npx', ... })
// process.env.npm_lifecycle_event = 'npx'

class Exec extends BaseCommand {
static description = 'Run a command from a local or remote npm package'
Expand All @@ -49,8 +23,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 All @@ -62,7 +38,7 @@ class Exec extends BaseCommand {
localBin,
globalBin,
} = this.npm
const output = (...outputArgs) => this.npm.output(...outputArgs)
const output = this.npm.output.bind(this.npm)
const scriptShell = this.npm.config.get('script-shell') || undefined
const packages = this.npm.config.get('package')
const yes = this.npm.config.get('yes')
Expand Down Expand Up @@ -92,10 +68,10 @@ class Exec extends BaseCommand {

async execWorkspaces (args, filters) {
await this.setWorkspaces(filters)
const color = this.npm.color

for (const path of this.workspacePaths) {
const locationMsg = await getLocationMsg({ color, path })
for (const [name, path] of this.workspaces) {
const locationMsg =
`in workspace ${this.npm.chalk.green(name)} at location:\n${this.npm.chalk.dim(path)}`
await this.exec(args, { locationMsg, runPath: path })
}
}
Expand Down
10 changes: 1 addition & 9 deletions lib/commands/init.js
Expand Up @@ -10,7 +10,6 @@ const PackageJson = require('@npmcli/package-json')
const log = require('../utils/log-shim.js')
const updateWorkspaces = require('../workspaces/update-workspaces.js')

const getLocationMsg = require('../exec/get-workspace-location-msg.js')
const BaseCommand = require('../base-command.js')

class Init extends BaseCommand {
Expand Down Expand Up @@ -119,13 +118,7 @@ class Init extends BaseCommand {
localBin,
globalBin,
} = this.npm
// this function is definitely called. But because of coverage map stuff
// it ends up both uncovered, and the coverage report doesn't even mention.
// the tests do assert that some output happens, so we know this line is
// being hit.
/* istanbul ignore next */
const output = (...outputArgs) => this.npm.output(...outputArgs)
const locationMsg = await getLocationMsg({ color, path })
const output = this.npm.output.bind(this.npm)
const runPath = path
const scriptShell = this.npm.config.get('script-shell') || undefined
const yes = this.npm.config.get('yes')
Expand All @@ -135,7 +128,6 @@ class Init extends BaseCommand {
args: newArgs,
color,
localBin,
locationMsg,
globalBin,
output,
path,
Expand Down
25 changes: 0 additions & 25 deletions lib/exec/get-workspace-location-msg.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/utils/config/definitions.js
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
17 changes: 0 additions & 17 deletions node_modules/@npmcli/fs/lib/common/file-url-to-path/index.js

This file was deleted.

121 changes: 0 additions & 121 deletions node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js

This file was deleted.

4 changes: 2 additions & 2 deletions node_modules/@npmcli/fs/lib/common/owner-sync.js
@@ -1,6 +1,6 @@
const { dirname, resolve } = require('path')
const url = require('url')

const fileURLToPath = require('./file-url-to-path/index.js')
const fs = require('../fs.js')

// given a path, find the owner of the nearest parent
Expand All @@ -13,7 +13,7 @@ const find = (path) => {
// fs methods accept URL objects with a scheme of file: so we need to unwrap
// those into an actual path string before we can resolve it
const resolved = path != null && path.href && path.origin
? resolve(fileURLToPath(path))
? resolve(url.fileURLToPath(path))
: resolve(path)

let stat
Expand Down
4 changes: 2 additions & 2 deletions node_modules/@npmcli/fs/lib/common/owner.js
@@ -1,6 +1,6 @@
const { dirname, resolve } = require('path')
const url = require('url')

const fileURLToPath = require('./file-url-to-path/index.js')
const fs = require('../fs.js')

// given a path, find the owner of the nearest parent
Expand All @@ -13,7 +13,7 @@ const find = async (path) => {
// fs methods accept URL objects with a scheme of file: so we need to unwrap
// those into an actual path string before we can resolve it
const resolved = path != null && path.href && path.origin
? resolve(fileURLToPath(path))
? resolve(url.fileURLToPath(path))
: resolve(path)

let stat
Expand Down
2 changes: 1 addition & 1 deletion node_modules/@npmcli/fs/lib/index.js
Expand Up @@ -2,7 +2,7 @@ module.exports = {
...require('./fs.js'),
copyFile: require('./copy-file.js'),
cp: require('./cp/index.js'),
mkdir: require('./mkdir/index.js'),
mkdir: require('./mkdir.js'),
mkdtemp: require('./mkdtemp.js'),
rm: require('./rm/index.js'),
withTempDir: require('./with-temp-dir.js'),
Expand Down
19 changes: 19 additions & 0 deletions node_modules/@npmcli/fs/lib/mkdir.js
@@ -0,0 +1,19 @@
const fs = require('./fs.js')
const getOptions = require('./common/get-options.js')
const withOwner = require('./with-owner.js')

// extends mkdir with the ability to specify an owner of the new dir
const mkdir = async (path, opts) => {
const options = getOptions(opts, {
copy: ['mode', 'recursive'],
wrap: 'mode',
})

return withOwner(
path,
() => fs.mkdir(path, options),
opts
)
}

module.exports = mkdir