feat: accept install command line options as dedupe options (#7473)

* refactor: reuse OPTIONS.ignoreScripts in dedupe help

* feat: accept install command line options as dedupe options

* test: add test to ensure .npmrc is used by pnpm dedupe

* test: add test for "pnpm dedupe --store-dir=..."

* test: ensure pnpm dedupe does not accidentally inherit --frozen-lockfile

* revert: remove newly added e2e tests in dedupe

* test: add unit test to check contents of dedupe cliOptionTypes
This commit is contained in:
Brandon Cheng
2024-01-01 18:47:46 -05:00
committed by GitHub
parent a2f5f5c990
commit 064aeb681e
3 changed files with 33 additions and 10 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-installation": minor
pnpm: minor
---
The `pnpm dedupe` command now accepts more command line options that the `pnpm install` command also accepts. Example: `pnpm dedupe --store-dir=local-store-dir`

View File

@@ -1,14 +1,15 @@
import { docsUrl } from '@pnpm/cli-utils'
import { UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { OPTIONS, UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { dedupeDiffCheck } from '@pnpm/dedupe.check'
import renderHelp from 'render-help'
import { type InstallCommandOptions } from './install'
import { type InstallCommandOptions, rcOptionsTypes as installCommandRcOptionsTypes } from './install'
import { installDeps } from './installDeps'
import { types as allTypes } from '@pnpm/config'
import pick from 'ramda/src/pick'
import omit from 'ramda/src/omit'
// In general, the "pnpm dedupe" command should use .npmrc options that "pnpm install" would also accept.
export function rcOptionsTypes () {
return pick(['ignore-scripts'], allTypes)
// Some options on pnpm install (like --frozen-lockfile) don't make sense on pnpm dedupe.
return omit(['frozen-lockfile'], installCommandRcOptionsTypes())
}
export function cliOptionsTypes () {
@@ -32,10 +33,12 @@ export function help () {
description: 'Check if running dedupe would result in changes without installing packages or editing the lockfile. Exits with a non-zero status code if changes are possible.',
name: '--check',
},
{
description: "Don't run lifecycle scripts",
name: '--ignore-scripts',
},
OPTIONS.ignoreScripts,
OPTIONS.offline,
OPTIONS.preferOffline,
OPTIONS.storeDir,
OPTIONS.virtualStoreDir,
OPTIONS.globalDir,
],
},
],
@@ -57,7 +60,6 @@ export async function handler (opts: DedupeCommandOptions) {
return installDeps({
...opts,
dedupe: true,
ignoreScripts: opts.ignoreScripts ?? false,
include,
includeDirect: include,
lockfileCheck: opts.check ? dedupeDiffCheck : undefined,

View File

@@ -107,6 +107,21 @@ describe('pnpm dedupe', () => {
expect(fs.existsSync('package.json')).toBeTruthy()
expect(server.getLines()).toStrictEqual([])
})
describe('cliOptionsTypes', () => {
test('trivially contains command line arguments from install command', () => {
// Using --store-dir and --registry as a gut check to ensure the "pnpm
// dedupe" command accepts most CLI options that "pnpm install" accepts.
expect(dedupe.cliOptionsTypes()).toHaveProperty('store-dir')
expect(dedupe.cliOptionsTypes()).toHaveProperty('registry')
})
test('does not accept --frozen-lockfile', () => {
// This option doesn't make sense on pnpm dedupe. Ensure it's not
// accidentally inherited from the install command after future refactors.
expect(dedupe.cliOptionsTypes()).not.toHaveProperty('--frozen-lockfile')
})
})
})
const noColor = (str: string) => str