Skip to content

Commit

Permalink
fix: only display unknown settings warning for install command (#3130)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaodingyd committed Feb 8, 2021
1 parent a5e9d90 commit cb040ae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-geese-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pnpm": patch
---

only display unknown settings for install command
6 changes: 6 additions & 0 deletions .changeset/strange-llamas-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/cli-utils": minor
"@pnpm/config": minor
---

add option to check unknown settings
2 changes: 2 additions & 0 deletions packages/cli-utils/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default async function (
globalDirShouldAllowWrite?: boolean
rcOptionsTypes: Record<string, unknown>
workspaceDir: string | undefined
checkUnknownSetting?: boolean
}
) {
const { config, warnings } = await getConfig({
Expand All @@ -17,6 +18,7 @@ export default async function (
packageManager,
rcOptionsTypes: opts.rcOptionsTypes,
workspaceDir: opts.workspaceDir,
checkUnknownSetting: opts.checkUnknownSetting,
})
config.cliOptions = cliOptions

Expand Down
25 changes: 14 additions & 11 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions packages/config/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
})
2 changes: 2 additions & 0 deletions packages/pnpm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb040ae

Please sign in to comment.