edit.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const { resolve } = require('node:path')
  2. const { lstat } = require('node:fs/promises')
  3. const cp = require('node:child_process')
  4. const completion = require('../utils/installed-shallow.js')
  5. const BaseCommand = require('../base-cmd.js')
  6. const splitPackageNames = (path) => path.split('/')
  7. // combine scoped parts
  8. .reduce((parts, part) => {
  9. if (parts.length === 0) {
  10. return [part]
  11. }
  12. const lastPart = parts[parts.length - 1]
  13. // check if previous part is the first part of a scoped package
  14. if (lastPart[0] === '@' && !lastPart.includes('/')) {
  15. parts[parts.length - 1] += '/' + part
  16. } else {
  17. parts.push(part)
  18. }
  19. return parts
  20. }, [])
  21. .join('/node_modules/')
  22. .replace(/(\/node_modules)+/, '/node_modules')
  23. // npm edit <pkg>
  24. // open the package folder in the $EDITOR
  25. class Edit extends BaseCommand {
  26. static description = 'Edit an installed package'
  27. static name = 'edit'
  28. static usage = ['<pkg>[/<subpkg>...]']
  29. static params = ['editor']
  30. static ignoreImplicitWorkspace = false
  31. static async completion (opts, npm) {
  32. return completion(npm, opts)
  33. }
  34. async exec (args) {
  35. if (args.length !== 1) {
  36. throw this.usageError()
  37. }
  38. const path = splitPackageNames(args[0])
  39. const dir = resolve(this.npm.dir, path)
  40. await lstat(dir)
  41. await new Promise((res, rej) => {
  42. const [bin, ...spawnArgs] = this.npm.config.get('editor').split(/\s+/)
  43. const editor = cp.spawn(bin, [...spawnArgs, dir], { stdio: 'inherit' })
  44. editor.on('exit', async (code) => {
  45. if (code) {
  46. return rej(new Error(`editor process exited with code: ${code}`))
  47. }
  48. await this.npm.exec('rebuild', [dir]).then(res).catch(rej)
  49. })
  50. })
  51. }
  52. }
  53. module.exports = Edit