edit.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // npm edit <pkg>
  2. // open the package folder in the $EDITOR
  3. module.exports = edit
  4. edit.usage = 'npm edit <pkg>[/<subpkg>...]'
  5. edit.completion = require('./utils/completion/installed-shallow.js')
  6. var npm = require('./npm.js')
  7. var path = require('path')
  8. var fs = require('graceful-fs')
  9. var editor = require('editor')
  10. var noProgressTillDone = require('./utils/no-progress-while-running').tillDone
  11. function edit (args, cb) {
  12. var p = args[0]
  13. if (args.length !== 1 || !p) return cb(edit.usage)
  14. var e = npm.config.get('editor')
  15. if (!e) {
  16. return cb(new Error(
  17. "No editor set. Set the 'editor' config, or $EDITOR environ."
  18. ))
  19. }
  20. p = p.split('/')
  21. // combine scoped parts
  22. .reduce(function (parts, part) {
  23. if (parts.length === 0) {
  24. return [part]
  25. }
  26. var lastPart = parts[parts.length - 1]
  27. // check if previous part is the first part of a scoped package
  28. if (lastPart[0] === '@' && !lastPart.includes('/')) {
  29. parts[parts.length - 1] += '/' + part
  30. } else {
  31. parts.push(part)
  32. }
  33. return parts
  34. }, [])
  35. .join('/node_modules/')
  36. .replace(/(\/node_modules)+/, '/node_modules')
  37. var f = path.resolve(npm.dir, p)
  38. fs.lstat(f, function (er) {
  39. if (er) return cb(er)
  40. editor(f, { editor: e }, noProgressTillDone(function (er) {
  41. if (er) return cb(er)
  42. npm.commands.rebuild(args, cb)
  43. }))
  44. })
  45. }