mirror of
https://github.com/nodejs/node.git
synced 2025-05-16 02:01:52 +00:00

PR-URL: https://github.com/nodejs/node/pull/40726 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Voltrex <mohammadkeyvanzade94@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
114 lines
2.9 KiB
JavaScript
114 lines
2.9 KiB
JavaScript
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 {
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
static get description () {
|
|
return 'Run a command from a local or remote npm package'
|
|
}
|
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
static get params () {
|
|
return [
|
|
'package',
|
|
'call',
|
|
'workspace',
|
|
'workspaces',
|
|
'include-workspace-root',
|
|
]
|
|
}
|
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
static get name () {
|
|
return 'exec'
|
|
}
|
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
static get usage () {
|
|
return [
|
|
'-- <pkg>[@<version>] [args...]',
|
|
'--package=<pkg>[@<version>] -- <cmd> [args...]',
|
|
'-c \'<cmd> [args...]\'',
|
|
'--package=foo -c \'<cmd> [args...]\'',
|
|
]
|
|
}
|
|
|
|
async exec (_args, { locationMsg, path, runPath } = {}) {
|
|
if (!path)
|
|
path = this.npm.localPrefix
|
|
|
|
if (!runPath)
|
|
runPath = process.cwd()
|
|
|
|
const args = [..._args]
|
|
const call = this.npm.config.get('call')
|
|
const {
|
|
flatOptions,
|
|
localBin,
|
|
log,
|
|
globalBin,
|
|
} = this.npm
|
|
const output = (...outputArgs) => this.npm.output(...outputArgs)
|
|
const scriptShell = this.npm.config.get('script-shell') || undefined
|
|
const packages = this.npm.config.get('package')
|
|
const yes = this.npm.config.get('yes')
|
|
|
|
if (call && _args.length)
|
|
throw this.usageError()
|
|
|
|
return libexec({
|
|
...flatOptions,
|
|
args,
|
|
call,
|
|
localBin,
|
|
locationMsg,
|
|
log,
|
|
globalBin,
|
|
output,
|
|
packages,
|
|
path,
|
|
runPath,
|
|
scriptShell,
|
|
yes,
|
|
})
|
|
}
|
|
|
|
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 })
|
|
await this.exec(args, { locationMsg, path, runPath: path })
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Exec
|