explore.js 1.7 KB

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