| const pkgJson = require('@npmcli/package-json')
|
| const runScript = require('@npmcli/run-script')
|
| const { join, relative } = require('node:path')
|
| const { log, output } = require('proc-log')
|
| const completion = require('../utils/installed-shallow.js')
|
| const BaseCommand = require('../base-cmd.js')
|
|
|
|
|
|
|
| class Explore extends BaseCommand {
|
| static description = 'Browse an installed package'
|
| static name = 'explore'
|
| static usage = ['<pkg> [ -- <command>]']
|
| static params = ['shell']
|
| static ignoreImplicitWorkspace = false
|
|
|
| static async completion (opts, npm) {
|
| return completion(npm, opts)
|
| }
|
|
|
| async exec (args) {
|
| if (args.length < 1 || !args[0]) {
|
| throw this.usageError()
|
| }
|
|
|
| const pkgname = args.shift()
|
|
|
|
|
| const path = join(this.npm.dir, join('/', pkgname))
|
| if (relative(path, this.npm.dir) === '') {
|
| throw this.usageError()
|
| }
|
|
|
|
|
|
|
| const { content: pkg } = await pkgJson.normalize(path).catch(er => {
|
| 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) {
|
| output.standard(`\nExploring ${path}\nType 'exit' or ^D when finished\n`)
|
| }
|
|
|
| return runScript({
|
| ...this.npm.flatOptions,
|
| pkg,
|
| path,
|
| event: '_explore',
|
| stdio: 'inherit',
|
| }).catch(er => {
|
| process.exitCode = typeof er.code === 'number' && er.code !== 0 ? er.code
|
| : 1
|
|
|
| const isProcExit = er.message === 'command failed' &&
|
| (typeof er.code === 'number' || /^SIG/.test(er.signal || ''))
|
| if (args.length || !isProcExit) {
|
| throw er
|
| }
|
| })
|
| }
|
| }
|
|
|
| module.exports = Explore
|
|
|