Skip to content

Commit 3b30af2

Browse files
wraithgarnlf
authored andcommitted
fix: fix exec tests and clean up workspace-location-msg
The workspace-location-msg file was being called improperly by `npm init` and not even tested, and when digging in it probably shouldn't be used at all from there. It's not always a workspace in this context.
1 parent 19f1497 commit 3b30af2

File tree

5 files changed

+76
-1585
lines changed

5 files changed

+76
-1585
lines changed

lib/commands/exec.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
11
const libexec = require('libnpmexec')
22
const BaseCommand = require('../base-command.js')
3-
const getLocationMsg = require('../exec/get-workspace-location-msg.js')
4-
5-
// it's like this:
6-
//
7-
// npm x pkg@version <-- runs the bin named "pkg" or the only bin if only 1
8-
//
9-
// { name: 'pkg', bin: { pkg: 'pkg.js', foo: 'foo.js' }} <-- run pkg
10-
// { name: 'pkg', bin: { foo: 'foo.js' }} <-- run foo?
11-
//
12-
// npm x -p pkg@version -- foo
13-
//
14-
// npm x -p pkg@version -- foo --registry=/dev/null
15-
//
16-
// const pkg = npm.config.get('package') || getPackageFrom(args[0])
17-
// const cmd = getCommand(pkg, args[0])
18-
// --> npm x -c 'cmd ...args.slice(1)'
19-
//
20-
// we've resolved cmd and args, and escaped them properly, and installed the
21-
// relevant packages.
22-
//
23-
// Add the ${npx install prefix}/node_modules/.bin to PATH
24-
//
25-
// pkg = readPackageJson('./package.json')
26-
// pkg.scripts.___npx = ${the -c arg}
27-
// runScript({ pkg, event: 'npx', ... })
28-
// process.env.npm_lifecycle_event = 'npx'
293

304
class Exec extends BaseCommand {
315
static description = 'Run a command from a local or remote npm package'
@@ -64,7 +38,7 @@ class Exec extends BaseCommand {
6438
localBin,
6539
globalBin,
6640
} = this.npm
67-
const output = (...outputArgs) => this.npm.output(...outputArgs)
41+
const output = this.npm.output.bind(this.npm)
6842
const scriptShell = this.npm.config.get('script-shell') || undefined
6943
const packages = this.npm.config.get('package')
7044
const yes = this.npm.config.get('yes')
@@ -94,10 +68,10 @@ class Exec extends BaseCommand {
9468

9569
async execWorkspaces (args, filters) {
9670
await this.setWorkspaces(filters)
97-
const color = this.npm.color
9871

99-
for (const path of this.workspacePaths) {
100-
const locationMsg = await getLocationMsg({ color, path })
72+
for (const [name, path] of this.workspaces) {
73+
const locationMsg =
74+
`in workspace ${this.npm.chalk.green(name)} at location:\n${this.npm.chalk.dim(path)}`
10175
await this.exec(args, { locationMsg, runPath: path })
10276
}
10377
}

lib/commands/init.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const PackageJson = require('@npmcli/package-json')
1010
const log = require('../utils/log-shim.js')
1111
const updateWorkspaces = require('../workspaces/update-workspaces.js')
1212

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

1615
class Init extends BaseCommand {
@@ -119,13 +118,7 @@ class Init extends BaseCommand {
119118
localBin,
120119
globalBin,
121120
} = this.npm
122-
// this function is definitely called. But because of coverage map stuff
123-
// it ends up both uncovered, and the coverage report doesn't even mention.
124-
// the tests do assert that some output happens, so we know this line is
125-
// being hit.
126-
/* istanbul ignore next */
127-
const output = (...outputArgs) => this.npm.output(...outputArgs)
128-
const locationMsg = await getLocationMsg({ color, path })
121+
const output = this.npm.output.bind(this.npm)
129122
const runPath = path
130123
const scriptShell = this.npm.config.get('script-shell') || undefined
131124
const yes = this.npm.config.get('yes')
@@ -135,7 +128,6 @@ class Init extends BaseCommand {
135128
args: newArgs,
136129
color,
137130
localBin,
138-
locationMsg,
139131
globalBin,
140132
output,
141133
path,

lib/exec/get-workspace-location-msg.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ Directory in which \`npm pack\` will save tarballs.
11171117
* Default:
11181118
* Type: String (can be set multiple times)
11191119
1120-
The package to install for [\`npm exec\`](/commands/npm-exec)
1120+
The package or packages to install for [\`npm exec\`](/commands/npm-exec)
11211121
11221122
<!-- automatically generated, do not edit manually -->
11231123
<!-- see lib/utils/config/definitions.js -->

0 commit comments

Comments
 (0)