Skip to content

Commit

Permalink
fix: fix exec tests and clean up workspace-location-msg
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wraithgar authored and nlf committed Aug 2, 2022
1 parent 19f1497 commit 3b30af2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1,585 deletions.
34 changes: 4 additions & 30 deletions lib/commands/exec.js
Original file line number Diff line number Diff line change
@@ -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 Down Expand Up @@ -64,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 @@ -94,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
Original file line number Diff line number Diff line change
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.

Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,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
Loading

0 comments on commit 3b30af2

Please sign in to comment.