1 |
|
2 |
|
3 |
|
4 | module.exports = explore
|
5 | explore.usage = 'npm explore <pkg> [ -- <command>]'
|
6 | explore.completion = require('./utils/completion/installed-shallow.js')
|
7 |
|
8 | var npm = require('./npm.js')
|
9 | var spawn = require('./utils/spawn')
|
10 | var path = require('path')
|
11 | var fs = require('graceful-fs')
|
12 | var isWindowsShell = require('./utils/is-windows-shell.js')
|
13 | var escapeExecPath = require('./utils/escape-exec-path.js')
|
14 | var escapeArg = require('./utils/escape-arg.js')
|
15 | var output = require('./utils/output.js')
|
16 |
|
17 | function 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 |
|
55 | if (!shellArgs.length) return cb()
|
56 | cb(er)
|
57 | })
|
58 | })
|
59 | }
|