From 360a63aaca190a49461908a61bd681c08ef3901e Mon Sep 17 00:00:00 2001 From: Yao Ding Date: Mon, 8 Feb 2021 18:39:18 -0500 Subject: [PATCH] fix: only display unknown settings warning for install command (#3130) --- .changeset/eighty-geese-fly.md | 5 +++++ .changeset/strange-llamas-refuse.md | 6 ++++++ packages/cli-utils/src/getConfig.ts | 2 ++ packages/config/src/index.ts | 25 ++++++++++++++----------- packages/config/test/index.ts | 11 +++++++++++ packages/pnpm/src/main.ts | 2 ++ 6 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 .changeset/eighty-geese-fly.md create mode 100644 .changeset/strange-llamas-refuse.md diff --git a/.changeset/eighty-geese-fly.md b/.changeset/eighty-geese-fly.md new file mode 100644 index 0000000000..04cdd199a7 --- /dev/null +++ b/.changeset/eighty-geese-fly.md @@ -0,0 +1,5 @@ +--- +"pnpm": patch +--- + +only display unknown settings for install command diff --git a/.changeset/strange-llamas-refuse.md b/.changeset/strange-llamas-refuse.md new file mode 100644 index 0000000000..5966978332 --- /dev/null +++ b/.changeset/strange-llamas-refuse.md @@ -0,0 +1,6 @@ +--- +"@pnpm/cli-utils": minor +"@pnpm/config": minor +--- + +add option to check unknown settings diff --git a/packages/cli-utils/src/getConfig.ts b/packages/cli-utils/src/getConfig.ts index d9bd4590b7..a4dceffcbb 100644 --- a/packages/cli-utils/src/getConfig.ts +++ b/packages/cli-utils/src/getConfig.ts @@ -9,6 +9,7 @@ export default async function ( globalDirShouldAllowWrite?: boolean rcOptionsTypes: Record workspaceDir: string | undefined + checkUnknownSetting?: boolean } ) { const { config, warnings } = await getConfig({ @@ -17,6 +18,7 @@ export default async function ( packageManager, rcOptionsTypes: opts.rcOptionsTypes, workspaceDir: opts.workspaceDir, + checkUnknownSetting: opts.checkUnknownSetting, }) config.cliOptions = cliOptions diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index 89f706b9b0..57fe4d08cf 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -109,6 +109,7 @@ export default async ( } rcOptionsTypes?: Record workspaceDir?: string | undefined + checkUnknownSetting?: boolean } ): Promise<{ config: Config, warnings: string[] }> => { const packageManager = opts.packageManager ?? { name: 'pnpm', version: 'undefined' } @@ -395,18 +396,20 @@ export default async ( } pnpmConfig.enablePnp = pnpmConfig['nodeLinker'] === 'pnp' - const settingKeys = Object.keys({ - ...npmConfig?.sources?.workspace?.data, - ...npmConfig?.sources?.project?.data, - }).filter(key => key.trim() !== '') - const unknownKeys = [] - for (const key of settingKeys) { - if (!rcOptions.includes(key) && !key.startsWith('//') && !(key.startsWith('@') && key.endsWith(':registry'))) { - unknownKeys.push(key) + if (opts.checkUnknownSetting) { + const settingKeys = Object.keys({ + ...npmConfig?.sources?.workspace?.data, + ...npmConfig?.sources?.project?.data, + }).filter(key => key.trim() !== '') + const unknownKeys = [] + for (const key of settingKeys) { + if (!rcOptions.includes(key) && !key.startsWith('//') && !(key.startsWith('@') && key.endsWith(':registry'))) { + unknownKeys.push(key) + } + } + if (unknownKeys.length) { + warnings.push(`Your .npmrc file contains unknown setting: ${unknownKeys.join(', ')}`) } - } - if (unknownKeys.length) { - warnings.push(`Your .npmrc file contains unknown setting: ${unknownKeys.join(', ')}`) } return { config: pnpmConfig, warnings } diff --git a/packages/config/test/index.ts b/packages/config/test/index.ts index 68d2bf4300..6216d854c9 100644 --- a/packages/config/test/index.ts +++ b/packages/config/test/index.ts @@ -647,9 +647,20 @@ test('warn user unknown settings in npmrc', async () => { name: 'pnpm', version: '1.0.0', }, + checkUnknownSetting: true, }) expect(warnings).toStrictEqual([ 'Your .npmrc file contains unknown setting: typo-setting, mistake-setting', ]) + + const { warnings: noWarnings } = await getConfig({ + cliOptions: {}, + packageManager: { + name: 'pnpm', + version: '1.0.0', + }, + }) + + expect(noWarnings).toStrictEqual([]) }) diff --git a/packages/pnpm/src/main.ts b/packages/pnpm/src/main.ts index 196ab201e9..b18db610cb 100644 --- a/packages/pnpm/src/main.ts +++ b/packages/pnpm/src/main.ts @@ -88,11 +88,13 @@ export default async function run (inputArgv: string[]) { // When we just want to print the location of the global bin directory, // we don't need the write permission to it. Related issue: #2700 const globalDirShouldAllowWrite = cmd !== 'root' + const checkUnknownSetting = cmd === 'install' config = await getConfig(cliOptions, { excludeReporter: false, globalDirShouldAllowWrite, rcOptionsTypes, workspaceDir, + checkUnknownSetting, }) as typeof config config.forceSharedLockfile = typeof config.workspaceDir === 'string' && config.sharedWorkspaceLockfile === true config.argv = argv