UNPKG

1.67 kBJavaScriptView Raw
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 isWindowsShell = require('./utils/is-windows-shell.js')
13var escapeExecPath = require('./utils/escape-exec-path.js')
14var escapeArg = require('./utils/escape-arg.js')
15var output = require('./utils/output.js')
16
17function explore (args, cb) {
18 if (args.length < 1 || !args[0]) return cb(explore.usage)
19 var p = args.shift()
20
21 var cwd = path.resolve(npm.dir, p)
22 var opts = {cwd: cwd, stdio: 'inherit'}
23
24 var shellArgs = []
25 if (args) {
26 if (isWindowsShell) {
27 var execCmd = escapeExecPath(args.shift())
28 var execArgs = [execCmd].concat(args.map(escapeArg))
29 opts.windowsVerbatimArguments = true
30 shellArgs = ['/d', '/s', '/c'].concat(execArgs)
31 } else {
32 shellArgs.unshift('-c')
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 var shell = spawn(sh, shellArgs, opts)
53 shell.on('close', function (er) {
54 // only fail if non-interactive.
55 if (!shellArgs.length) return cb()
56 cb(er)
57 })
58 })
59}