• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// npm explore <pkg>[@<version>]
2// open a subshell to the package folder.
3
4module.exports = explore
5explore.usage = 'npm explore <pkg> [ -- <command>]'
6explore.completion = require('./utils/completion/installed-shallow.js')
7
8var npm = require('./npm.js')
9var spawn = require('./utils/spawn')
10var path = require('path')
11var fs = require('graceful-fs')
12var isWindows = require('./utils/is-windows.js')
13var escapeExecPath = require('./utils/escape-exec-path.js')
14var escapeArg = require('./utils/escape-arg.js')
15var output = require('./utils/output.js')
16var log = require('npmlog')
17
18function explore (args, cb) {
19  if (args.length < 1 || !args[0]) return cb(explore.usage)
20  var p = args.shift()
21
22  var cwd = path.resolve(npm.dir, p)
23  var opts = {cwd: cwd, stdio: 'inherit'}
24
25  var shellArgs = []
26  if (args.length) {
27    if (isWindows) {
28      var execCmd = escapeExecPath(args.shift())
29      var execArgs = [execCmd].concat(args.map(escapeArg))
30      opts.windowsVerbatimArguments = true
31      shellArgs = ['/d', '/s', '/c'].concat(execArgs)
32    } else {
33      shellArgs = ['-c', args.map(escapeArg).join(' ').trim()]
34    }
35  }
36
37  var sh = npm.config.get('shell')
38  fs.stat(cwd, function (er, s) {
39    if (er || !s.isDirectory()) {
40      return cb(new Error(
41        "It doesn't look like " + p + ' is installed.'
42      ))
43    }
44
45    if (!shellArgs.length) {
46      output(
47        '\nExploring ' + cwd + '\n' +
48          "Type 'exit' or ^D when finished\n"
49      )
50    }
51
52    log.silly('explore', {sh, shellArgs, opts})
53    var shell = spawn(sh, shellArgs, opts)
54    shell.on('close', function (er) {
55      // only fail if non-interactive.
56      if (!shellArgs.length) return cb()
57      cb(er)
58    })
59  })
60}
61