fix: only display unknown settings warning for install command (#3130)

This commit is contained in:
Yao Ding
2021-02-08 18:39:18 -05:00
committed by GitHub
parent a5e9d903cb
commit cb040ae185
6 changed files with 40 additions and 11 deletions

View File

@@ -0,0 +1,5 @@
---
"pnpm": patch
---
only display unknown settings for install command

View File

@@ -0,0 +1,6 @@
---
"@pnpm/cli-utils": minor
"@pnpm/config": minor
---
add option to check unknown settings

View File

@@ -9,6 +9,7 @@ export default async function (
globalDirShouldAllowWrite?: boolean
rcOptionsTypes: Record<string, unknown>
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

View File

@@ -108,6 +108,7 @@ export default async (
}
rcOptionsTypes?: Record<string, unknown>
workspaceDir?: string | undefined
checkUnknownSetting?: boolean
}
): Promise<{ config: Config, warnings: string[] }> => {
const packageManager = opts.packageManager ?? { name: 'pnpm', version: 'undefined' }
@@ -393,18 +394,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 }

View File

@@ -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([])
})

View File

@@ -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