From 8080e44a74ac231fd53b73fda7f01742ea3bda05 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 2 Oct 2024 04:21:06 +0800 Subject: [PATCH] fix: `pnpm cache view` command should specify package name (#8582) --- .changeset/tricky-games-give.md | 5 +++++ cache/commands/package.json | 1 + cache/commands/src/cache.cmd.ts | 7 +++++++ cache/commands/test/cacheView.cmd.test.ts | 22 ++++++++++++++++++++++ cache/commands/tsconfig.json | 3 +++ pnpm-lock.yaml | 3 +++ 6 files changed, 41 insertions(+) create mode 100644 .changeset/tricky-games-give.md diff --git a/.changeset/tricky-games-give.md b/.changeset/tricky-games-give.md new file mode 100644 index 0000000000..ec2a6172d2 --- /dev/null +++ b/.changeset/tricky-games-give.md @@ -0,0 +1,5 @@ +--- +"@pnpm/cache.commands": patch +--- + +`pnpm cache view` should specify package name diff --git a/cache/commands/package.json b/cache/commands/package.json index b7bc69521e..1d87124568 100644 --- a/cache/commands/package.json +++ b/cache/commands/package.json @@ -43,6 +43,7 @@ "@pnpm/cli-utils": "workspace:*", "@pnpm/config": "workspace:*", "@pnpm/constants": "workspace:*", + "@pnpm/error": "workspace:*", "@pnpm/store-path": "workspace:*", "ramda": "catalog:", "render-help": "catalog:" diff --git a/cache/commands/src/cache.cmd.ts b/cache/commands/src/cache.cmd.ts index 19096058c3..22b41d04ed 100644 --- a/cache/commands/src/cache.cmd.ts +++ b/cache/commands/src/cache.cmd.ts @@ -11,6 +11,7 @@ import { cacheDelete, cacheListRegistries, } from '@pnpm/cache.api' +import { PnpmError } from '@pnpm/error' export const rcOptionsTypes = cliOptionsTypes @@ -81,6 +82,12 @@ export async function handler (opts: CacheCommandOptions, params: string[]): Pro registry: opts.cliOptions['registry'], }, params.slice(1)) case 'view': { + if (!params[1]) { + throw new PnpmError('MISSING_PACKAGE_NAME', '`pnpm cache view` requires the package name') + } + if (params.length > 2) { + throw new PnpmError('TOO_MANY_PARAMS', '`pnpm cache view` only accepts one package name') + } const storeDir = await getStorePath({ pkgRoot: process.cwd(), storePath: opts.storeDir, diff --git a/cache/commands/test/cacheView.cmd.test.ts b/cache/commands/test/cacheView.cmd.test.ts index 94b5d9af80..0cc12040de 100644 --- a/cache/commands/test/cacheView.cmd.test.ts +++ b/cache/commands/test/cacheView.cmd.test.ts @@ -90,4 +90,26 @@ describe('cache view', () => { }), })) }) + + test('lists all metadata for requested package should specify a package name', async () => { + await expect( + cache.handler({ + cacheDir, + cliOptions: {}, + pnpmHomeDir: process.cwd(), + storeDir, + }, ['view']) + ).rejects.toThrow('`pnpm cache view` requires the package name') + }) + + test('lists all metadata for requested package should not accept more than one package name', async () => { + await expect( + cache.handler({ + cacheDir, + cliOptions: {}, + pnpmHomeDir: process.cwd(), + storeDir, + }, ['view', 'is-negative', 'is-positive']) + ).rejects.toThrow('`pnpm cache view` only accepts one package name') + }) }) diff --git a/cache/commands/tsconfig.json b/cache/commands/tsconfig.json index 8020b4a0dd..59d0d92bcd 100644 --- a/cache/commands/tsconfig.json +++ b/cache/commands/tsconfig.json @@ -21,6 +21,9 @@ { "path": "../../packages/constants" }, + { + "path": "../../packages/error" + }, { "path": "../../packages/logger" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5884db1c1e..81de079e06 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1080,6 +1080,9 @@ importers: '@pnpm/constants': specifier: workspace:* version: link:../../packages/constants + '@pnpm/error': + specifier: workspace:* + version: link:../../packages/error '@pnpm/store-path': specifier: workspace:* version: link:../../store/store-path