node/deps/npm/lib/commands/explore.js
npm team 6e1629786f deps: upgrade npm to 8.1.3
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>
2021-11-10 16:48:41 -08:00

88 lines
2.5 KiB
JavaScript

// npm explore <pkg>[@<version>]
// open a subshell to the package folder.
const rpj = require('read-package-json-fast')
const runScript = require('@npmcli/run-script')
const { join, resolve, relative } = require('path')
const completion = require('../utils/completion/installed-shallow.js')
const BaseCommand = require('../base-command.js')
class Explore extends BaseCommand {
static get description () {
return 'Browse an installed package'
}
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get name () {
return 'explore'
}
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get usage () {
return ['<pkg> [ -- <command>]']
}
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['shell']
}
/* istanbul ignore next - see test/lib/load-all-commands.js */
async completion (opts) {
return completion(this.npm, opts)
}
async exec (args) {
if (args.length < 1 || !args[0])
throw this.usageError()
const pkgname = args.shift()
// detect and prevent any .. shenanigans
const path = join(this.npm.dir, join('/', pkgname))
if (relative(path, this.npm.dir) === '')
throw this.usageError()
// run as if running a script named '_explore', which we set to either
// the set of arguments, or the shell config, and let @npmcli/run-script
// handle all the escaping and PATH setup stuff.
const pkg = await rpj(resolve(path, 'package.json')).catch(er => {
this.npm.log.error('explore', `It doesn't look like ${pkgname} is installed.`)
throw er
})
const { shell } = this.npm.flatOptions
pkg.scripts = {
...(pkg.scripts || {}),
_explore: args.join(' ').trim() || shell,
}
if (!args.length)
this.npm.output(`\nExploring ${path}\nType 'exit' or ^D when finished\n`)
this.npm.log.disableProgress()
try {
return await runScript({
...this.npm.flatOptions,
pkg,
banner: false,
path,
stdioString: true,
event: '_explore',
stdio: 'inherit',
}).catch(er => {
process.exitCode = typeof er.code === 'number' && er.code !== 0 ? er.code
: 1
// if it's not an exit error, or non-interactive, throw it
const isProcExit = er.message === 'command failed' &&
(typeof er.code === 'number' || /^SIG/.test(er.signal || ''))
if (args.length || !isProcExit)
throw er
})
} finally {
this.npm.log.enableProgress()
}
}
}
module.exports = Explore