MCPcopy Create free account
hub / github.com/freecodexyz/free-code / getConfig

Function getConfig

src/utils/config.ts:1441–1605  ·  view source on GitHub ↗
(
  file: string,
  createDefault: () => A,
  throwOnInvalid?: boolean,
)

Source from the content-addressed store, hash-verified

1439}
1440
1441function getConfig<A>(
1442 file: string,
1443 createDefault: () => A,
1444 throwOnInvalid?: boolean,
1445): A {
1446 // Log a warning if config is accessed before it's allowed
1447 if (!configReadingAllowed && process.env.NODE_ENV !== 'test') {
1448 throw new Error('Config accessed before allowed.')
1449 }
1450
1451 const fs = getFsImplementation()
1452
1453 try {
1454 const fileContent = fs.readFileSync(file, {
1455 encoding: 'utf-8',
1456 })
1457 try {
1458 // Strip BOM before parsing - PowerShell 5.x adds BOM to UTF-8 files
1459 const parsedConfig = jsonParse(stripBOM(fileContent))
1460 return {
1461 ...createDefault(),
1462 ...parsedConfig,
1463 }
1464 } catch (error) {
1465 // Throw a ConfigParseError with the file path and default config
1466 const errorMessage =
1467 error instanceof Error ? error.message : String(error)
1468 throw new ConfigParseError(errorMessage, file, createDefault())
1469 }
1470 } catch (error) {
1471 // Handle file not found - check for backup and return default
1472 const errCode = getErrnoCode(error)
1473 if (errCode === 'ENOENT') {
1474 const backupPath = findMostRecentBackup(file)
1475 if (backupPath) {
1476 process.stderr.write(
1477 `\nClaude configuration file not found at: ${file}\n` +
1478 `A backup file exists at: ${backupPath}\n` +
1479 `You can manually restore it by running: cp "${backupPath}" "${file}"\n\n`,
1480 )
1481 }
1482 return createDefault()
1483 }
1484
1485 // Re-throw ConfigParseError if throwOnInvalid is true
1486 if (error instanceof ConfigParseError && throwOnInvalid) {
1487 throw error
1488 }
1489
1490 // Log config parse errors so users know what happened
1491 if (error instanceof ConfigParseError) {
1492 logForDebugging(
1493 `Config file corrupted, resetting to defaults: ${error.message}`,
1494 { level: 'error' },
1495 )
1496
1497 // Guard: logEvent → shouldSampleEvent → getGlobalConfig → getConfig
1498 // causes infinite recursion when the config file is corrupted, because

Callers 5

saveGlobalConfigFunction · 0.70
getGlobalConfigFunction · 0.70
saveConfigWithLockFunction · 0.70
enableConfigsFunction · 0.70
saveCurrentProjectConfigFunction · 0.70

Calls 10

stripBOMFunction · 0.90
getFsImplementationFunction · 0.85
jsonParseFunction · 0.85
getErrnoCodeFunction · 0.85
findMostRecentBackupFunction · 0.85
logForDebuggingFunction · 0.85
logEventFunction · 0.85
getConfigBackupDirFunction · 0.85
logErrorFunction · 0.70
writeMethod · 0.45

Tested by

no test coverage detected