dedupe.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const reifyFinish = require('../utils/reify-finish.js')
  2. const ArboristWorkspaceCmd = require('../arborist-cmd.js')
  3. // dedupe duplicated packages, or find them in the tree
  4. class Dedupe extends ArboristWorkspaceCmd {
  5. static description = 'Reduce duplication in the package tree'
  6. static name = 'dedupe'
  7. static params = [
  8. 'install-strategy',
  9. 'legacy-bundling',
  10. 'global-style',
  11. 'strict-peer-deps',
  12. 'package-lock',
  13. 'omit',
  14. 'include',
  15. 'ignore-scripts',
  16. 'allow-git',
  17. 'audit',
  18. 'bin-links',
  19. 'fund',
  20. 'dry-run',
  21. ...super.params,
  22. ]
  23. async exec () {
  24. if (this.npm.global) {
  25. const er = new Error('`npm dedupe` does not work in global mode.')
  26. er.code = 'EDEDUPEGLOBAL'
  27. throw er
  28. }
  29. const dryRun = this.npm.config.get('dry-run')
  30. const where = this.npm.prefix
  31. const Arborist = require('@npmcli/arborist')
  32. const opts = {
  33. ...this.npm.flatOptions,
  34. path: where,
  35. dryRun,
  36. // Saving during dedupe would only update if one of your direct dependencies was also duplicated somewhere in your tree.
  37. // It would be confusing if running this were to also update your package.json.
  38. // In order to reduce potential confusion we set this to false.
  39. save: false,
  40. workspaces: this.workspaceNames,
  41. }
  42. const arb = new Arborist(opts)
  43. await arb.dedupe(opts)
  44. await reifyFinish(this.npm, arb)
  45. }
  46. }
  47. module.exports = Dedupe