uninstall.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const { resolve } = require('path')
  2. const Arborist = require('@npmcli/arborist')
  3. const rpj = require('read-package-json-fast')
  4. const reifyFinish = require('./utils/reify-finish.js')
  5. const completion = require('./utils/completion/installed-shallow.js')
  6. const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
  7. class Uninstall extends ArboristWorkspaceCmd {
  8. static get description () {
  9. return 'Remove a package'
  10. }
  11. /* istanbul ignore next - see test/lib/load-all-commands.js */
  12. static get name () {
  13. return 'uninstall'
  14. }
  15. /* istanbul ignore next - see test/lib/load-all-commands.js */
  16. static get params () {
  17. return ['save', ...super.params]
  18. }
  19. /* istanbul ignore next - see test/lib/load-all-commands.js */
  20. static get usage () {
  21. return ['[<@scope>/]<pkg>...']
  22. }
  23. /* istanbul ignore next - see test/lib/load-all-commands.js */
  24. async completion (opts) {
  25. return completion(this.npm, opts)
  26. }
  27. exec (args, cb) {
  28. this.uninstall(args).then(() => cb()).catch(cb)
  29. }
  30. async uninstall (args) {
  31. // the /path/to/node_modules/..
  32. const global = this.npm.config.get('global')
  33. const path = global
  34. ? resolve(this.npm.globalDir, '..')
  35. : this.npm.localPrefix
  36. if (!args.length) {
  37. if (!global)
  38. throw new Error('Must provide a package name to remove')
  39. else {
  40. let pkg
  41. try {
  42. pkg = await rpj(resolve(this.npm.localPrefix, 'package.json'))
  43. } catch (er) {
  44. if (er.code !== 'ENOENT' && er.code !== 'ENOTDIR')
  45. throw er
  46. else
  47. throw this.usage
  48. }
  49. args.push(pkg.name)
  50. }
  51. }
  52. const opts = {
  53. ...this.npm.flatOptions,
  54. path,
  55. log: this.npm.log,
  56. rm: args,
  57. workspaces: this.workspaceNames,
  58. }
  59. const arb = new Arborist(opts)
  60. await arb.reify(opts)
  61. await reifyFinish(this.npm, arb)
  62. }
  63. }
  64. module.exports = Uninstall