uninstall.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict'
  2. // remove a package.
  3. module.exports = uninstall
  4. const path = require('path')
  5. const validate = require('aproba')
  6. const readJson = require('read-package-json')
  7. const iferr = require('iferr')
  8. const npm = require('./npm.js')
  9. const Installer = require('./install.js').Installer
  10. const getSaveType = require('./install/save.js').getSaveType
  11. const removeDeps = require('./install/deps.js').removeDeps
  12. const log = require('npmlog')
  13. const usage = require('./utils/usage')
  14. uninstall.usage = usage(
  15. 'uninstall',
  16. 'npm uninstall [<@scope>/]<pkg>[@<version>]... [--save-prod|--save-dev|--save-optional] [--no-save]'
  17. )
  18. uninstall.completion = require('./utils/completion/installed-shallow.js')
  19. function uninstall (args, cb) {
  20. validate('AF', arguments)
  21. // the /path/to/node_modules/..
  22. const dryrun = !!npm.config.get('dry-run')
  23. if (args.length === 1 && args[0] === '.') args = []
  24. const where = npm.config.get('global') || !args.length
  25. ? path.resolve(npm.globalDir, '..')
  26. : npm.prefix
  27. args = args.filter(function (a) {
  28. return path.resolve(a) !== where
  29. })
  30. if (args.length) {
  31. new Uninstaller(where, dryrun, args).run(cb)
  32. } else {
  33. // remove this package from the global space, if it's installed there
  34. readJson(path.resolve(npm.localPrefix, 'package.json'), function (er, pkg) {
  35. if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
  36. if (er) return cb(uninstall.usage)
  37. new Uninstaller(where, dryrun, [pkg.name]).run(cb)
  38. })
  39. }
  40. }
  41. class Uninstaller extends Installer {
  42. constructor (where, dryrun, args) {
  43. super(where, dryrun, args)
  44. this.remove = []
  45. }
  46. loadArgMetadata (next) {
  47. this.args = this.args.map(function (arg) { return {name: arg} })
  48. next()
  49. }
  50. loadAllDepsIntoIdealTree (cb) {
  51. validate('F', arguments)
  52. this.remove = this.args
  53. this.args = []
  54. log.silly('uninstall', 'loadAllDepsIntoIdealTree')
  55. const saveDeps = getSaveType()
  56. super.loadAllDepsIntoIdealTree(iferr(cb, () => {
  57. removeDeps(this.remove, this.idealTree, saveDeps, cb)
  58. }))
  59. }
  60. // no top level lifecycles on rm
  61. runPreinstallTopLevelLifecycles (cb) { cb() }
  62. runPostinstallTopLevelLifecycles (cb) { cb() }
  63. }
  64. module.exports.Uninstaller = Uninstaller