Files
pnpm/config/commands/test/utils/index.ts
Zoltan Kochan b5d93c6ba9 refactor(config): remove rawLocalConfig and force* hoist flags (#11199)
rawLocalConfig detected whether hoist settings were explicitly
set. In v11, config values are always authoritative.

- Remove rawLocalConfig from ConfigContext, config reader,
  inheritPickedConfig, UniversalOptions
- Remove forceHoistPattern, forcePublicHoistPattern,
  forceShamefullyHoist — validateModules always checks now
- Simplify save-workspace-protocol check
- Remove dead rawLocalConfig overrides in deploy/patchCommit
2026-04-05 11:36:36 +02:00

111 lines
3.5 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import type { Config, ConfigContext } from '@pnpm/config.reader'
import { readIniFileSync } from 'read-ini-file'
import { readYamlFileSync } from 'read-yaml-file'
import { writeIniFileSync } from 'write-ini-file'
import { writeYamlFileSync } from 'write-yaml-file'
import type { ConfigCommandOptions } from '../../src/ConfigCommandOptions.js'
import type { config } from '../../src/index.js'
/**
* Build a {@link ConfigCommandOptions} object for tests.
*
* Accepts the flat shape that tests already use (settings like `storeDir`,
* `authConfig`, etc. mixed into a single object) and builds the `config`
* and `context` properties that the refactored config commands now expect.
*/
export function createConfigCommandOpts (
opts: Record<string, unknown> & {
dir: string
configDir: string
cliOptions: Record<string, unknown>
authConfig: Record<string, unknown>
global?: boolean
json?: boolean
location?: 'global' | 'project'
}
): ConfigCommandOptions {
return {
...opts,
_config: opts as unknown as Config,
_context: {
cliOptions: opts.cliOptions ?? {},
explicitlySetKeys: new Set(Object.keys(opts)),
rootProjectManifestDir: opts.dir,
packageManager: { name: 'pnpm', version: '0.0.0' },
} as ConfigContext,
} as ConfigCommandOptions
}
export function getOutputString (result: config.ConfigHandlerResult): string {
if (result == null) throw new Error('output is null or undefined')
if (typeof result === 'string') return result
if (typeof result === 'object') return result.output
const _typeGuard: never = result
throw new Error('unreachable')
}
export interface ConfigFilesData {
globalRc: Record<string, unknown> | undefined
globalYaml: Record<string, unknown> | undefined
localRc: Record<string, unknown> | undefined
localYaml: Record<string, unknown> | undefined
}
export function readConfigFiles (globalConfigDir: string | undefined, localDir: string | undefined): ConfigFilesData {
function tryRead<Return> (reader: () => Return): Return | undefined {
try {
return reader()
} catch (error) {
if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT') {
return undefined
}
throw error
}
}
return {
globalRc: globalConfigDir
? tryRead(() => readIniFileSync(path.join(globalConfigDir, 'auth.ini')) as Record<string, unknown>)
: undefined,
globalYaml: globalConfigDir
? tryRead(() => readYamlFileSync(path.join(globalConfigDir, 'config.yaml')))
: undefined,
localRc: localDir
? tryRead(() => readIniFileSync(path.join(localDir, '.npmrc')) as Record<string, unknown>)
: undefined,
localYaml: localDir
? tryRead(() => readYamlFileSync(path.join(localDir, 'pnpm-workspace.yaml')))
: undefined,
}
}
export function writeConfigFiles (globalConfigDir: string | undefined, localDir: string | undefined, data: ConfigFilesData): void {
if (globalConfigDir) {
fs.mkdirSync(globalConfigDir, { recursive: true })
if (data.globalRc) {
writeIniFileSync(path.join(globalConfigDir, 'auth.ini'), data.globalRc)
}
if (data.globalYaml) {
writeYamlFileSync(path.join(globalConfigDir, 'config.yaml'), data.globalYaml)
}
}
if (localDir) {
fs.mkdirSync(localDir, { recursive: true })
if (data.localRc) {
writeIniFileSync(path.join(localDir, '.npmrc'), data.localRc)
}
if (data.localYaml) {
writeYamlFileSync(path.join(localDir, 'pnpm-workspace.yaml'), data.localYaml)
}
}
}