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