11cb0ef41Sopenharmony_ci// dedupe duplicated packages, or find them in the tree
21cb0ef41Sopenharmony_ciconst reifyFinish = require('../utils/reify-finish.js')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst ArboristWorkspaceCmd = require('../arborist-cmd.js')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciclass Dedupe extends ArboristWorkspaceCmd {
71cb0ef41Sopenharmony_ci  static description = 'Reduce duplication in the package tree'
81cb0ef41Sopenharmony_ci  static name = 'dedupe'
91cb0ef41Sopenharmony_ci  static params = [
101cb0ef41Sopenharmony_ci    'install-strategy',
111cb0ef41Sopenharmony_ci    'legacy-bundling',
121cb0ef41Sopenharmony_ci    'global-style',
131cb0ef41Sopenharmony_ci    'strict-peer-deps',
141cb0ef41Sopenharmony_ci    'package-lock',
151cb0ef41Sopenharmony_ci    'omit',
161cb0ef41Sopenharmony_ci    'include',
171cb0ef41Sopenharmony_ci    'ignore-scripts',
181cb0ef41Sopenharmony_ci    'audit',
191cb0ef41Sopenharmony_ci    'bin-links',
201cb0ef41Sopenharmony_ci    'fund',
211cb0ef41Sopenharmony_ci    'dry-run',
221cb0ef41Sopenharmony_ci    ...super.params,
231cb0ef41Sopenharmony_ci  ]
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  async exec (args) {
261cb0ef41Sopenharmony_ci    if (this.npm.global) {
271cb0ef41Sopenharmony_ci      const er = new Error('`npm dedupe` does not work in global mode.')
281cb0ef41Sopenharmony_ci      er.code = 'EDEDUPEGLOBAL'
291cb0ef41Sopenharmony_ci      throw er
301cb0ef41Sopenharmony_ci    }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    const dryRun = this.npm.config.get('dry-run')
331cb0ef41Sopenharmony_ci    const where = this.npm.prefix
341cb0ef41Sopenharmony_ci    const Arborist = require('@npmcli/arborist')
351cb0ef41Sopenharmony_ci    const opts = {
361cb0ef41Sopenharmony_ci      ...this.npm.flatOptions,
371cb0ef41Sopenharmony_ci      path: where,
381cb0ef41Sopenharmony_ci      dryRun,
391cb0ef41Sopenharmony_ci      // Saving during dedupe would only update if one of your direct
401cb0ef41Sopenharmony_ci      // dependencies was also duplicated somewhere in your tree. It would be
411cb0ef41Sopenharmony_ci      // confusing if running this were to also update your package.json.  In
421cb0ef41Sopenharmony_ci      // order to reduce potential confusion we set this to false.
431cb0ef41Sopenharmony_ci      save: false,
441cb0ef41Sopenharmony_ci      workspaces: this.workspaceNames,
451cb0ef41Sopenharmony_ci    }
461cb0ef41Sopenharmony_ci    const arb = new Arborist(opts)
471cb0ef41Sopenharmony_ci    await arb.dedupe(opts)
481cb0ef41Sopenharmony_ci    await reifyFinish(this.npm, arb)
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cimodule.exports = Dedupe
53