All files explore.js

100% Statements 35/35
100% Branches 25/25
100% Functions 5/5
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72      2x 2x 2x 2x   18x   2x 2x   2x 2x   2x 18x 1x   17x     17x 17x 5x 5x             12x 1x 1x     11x 11x         11x 7x 11x 11x 11x                 7x     7x   7x 5x     11x       2x  
// npm explore <pkg>[@<version>]
// open a subshell to the package folder.
 
const usageUtil = require('./utils/usage.js')
const completion = require('./utils/completion/installed-shallow.js')
const usage = usageUtil('explore', 'npm explore <pkg> [ -- <command>]')
const rpj = require('read-package-json-fast')
 
const cmd = (args, cb) => explore(args).then(() => cb()).catch(cb)
 
const output = require('./utils/output.js')
const npm = require('./npm.js')
 
const runScript = require('@npmcli/run-script')
const { join, resolve, relative } = require('path')
 
const explore = async args => {
  if (args.length < 1 || !args[0])
    throw usage
 
  const pkgname = args.shift()
 
  // detect and prevent any .. shenanigans
  const path = join(npm.dir, join('/', pkgname))
  if (relative(path, npm.dir) === '') {
    console.error('NO PATH FOUND', path)
    throw usage
  }
 
  // 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 => {
    npm.log.error('explore', `It doesn't look like ${pkgname} is installed.`)
    throw er
  })
 
  const { shell } = npm.flatOptions
  pkg.scripts = {
    ...(pkg.scripts || {}),
    _explore: args.join(' ').trim() || shell,
  }
 
  if (!args.length)
    output(`\nExploring ${path}\nType 'exit' or ^D when finished\n`)
  npm.log.disableProgress()
  try {
    return await runScript({
      ...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 {
    npm.log.enableProgress()
  }
}
 
module.exports = Object.assign(cmd, { completion, usage })