uninstall.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const { resolve } = require('node:path')
  2. const pkgJson = require('@npmcli/package-json')
  3. const reifyFinish = require('../utils/reify-finish.js')
  4. const completion = require('../utils/installed-shallow.js')
  5. const ArboristWorkspaceCmd = require('../arborist-cmd.js')
  6. class Uninstall extends ArboristWorkspaceCmd {
  7. static description = 'Remove a package'
  8. static name = 'uninstall'
  9. static params = ['save', 'global', ...super.params]
  10. static usage = ['[<@scope>/]<pkg>...']
  11. static ignoreImplicitWorkspace = false
  12. static async completion (opts, npm) {
  13. return completion(npm, opts)
  14. }
  15. async exec (args) {
  16. if (!args.length) {
  17. if (!this.npm.global) {
  18. throw new Error('Must provide a package name to remove')
  19. } else {
  20. try {
  21. const { content: pkg } = await pkgJson.normalize(this.npm.localPrefix)
  22. args.push(pkg.name)
  23. } catch (er) {
  24. if (er.code !== 'ENOENT' && er.code !== 'ENOTDIR') {
  25. throw er
  26. } else {
  27. throw this.usageError()
  28. }
  29. }
  30. }
  31. }
  32. // the /path/to/node_modules/..
  33. const path = this.npm.global
  34. ? resolve(this.npm.globalDir, '..')
  35. : this.npm.localPrefix
  36. const Arborist = require('@npmcli/arborist')
  37. const opts = {
  38. ...this.npm.flatOptions,
  39. path,
  40. rm: args,
  41. workspaces: this.workspaceNames,
  42. }
  43. const arb = new Arborist(opts)
  44. await arb.reify(opts)
  45. await reifyFinish(this.npm, arb)
  46. }
  47. }
  48. module.exports = Uninstall