dedupe.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // dedupe duplicated packages, or find them in the tree
  2. const Arborist = require('@npmcli/arborist')
  3. const reifyFinish = require('./utils/reify-finish.js')
  4. const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
  5. class Dedupe extends ArboristWorkspaceCmd {
  6. /* istanbul ignore next - see test/lib/load-all-commands.js */
  7. static get description () {
  8. return 'Reduce duplication in the package tree'
  9. }
  10. /* istanbul ignore next - see test/lib/load-all-commands.js */
  11. static get name () {
  12. return 'dedupe'
  13. }
  14. /* istanbul ignore next - see test/lib/load-all-commands.js */
  15. static get params () {
  16. return [
  17. 'global-style',
  18. 'legacy-bundling',
  19. 'strict-peer-deps',
  20. 'package-lock',
  21. 'omit',
  22. 'ignore-scripts',
  23. 'audit',
  24. 'bin-links',
  25. 'fund',
  26. 'dry-run',
  27. ...super.params,
  28. ]
  29. }
  30. exec (args, cb) {
  31. this.dedupe(args).then(() => cb()).catch(cb)
  32. }
  33. async dedupe (args) {
  34. if (this.npm.config.get('global')) {
  35. const er = new Error('`npm dedupe` does not work in global mode.')
  36. er.code = 'EDEDUPEGLOBAL'
  37. throw er
  38. }
  39. const dryRun = this.npm.config.get('dry-run')
  40. const where = this.npm.prefix
  41. const opts = {
  42. ...this.npm.flatOptions,
  43. log: this.npm.log,
  44. path: where,
  45. dryRun,
  46. workspaces: this.workspaceNames,
  47. }
  48. const arb = new Arborist(opts)
  49. await arb.dedupe(opts)
  50. await reifyFinish(this.npm, arb)
  51. }
  52. }
  53. module.exports = Dedupe