Skip to content

Commit 87a5fd3

Browse files
committed
feat: implement home-directory dotfile config discovery
1 parent a5ecd3d commit 87a5fd3

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/bunfig/src/config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,30 @@ export async function loadConfig<T>({
266266
}
267267
}
268268

269+
// Also try dotfile configs in user's home directory root (e.g., ~/.<name>.config.*)
270+
if (name) {
271+
const homeDir = homedir()
272+
const homeRootPatterns = [`.${name}.config`]
273+
274+
if (alias)
275+
homeRootPatterns.push(`.${alias}.config`)
276+
277+
if (verbose)
278+
log.info(`Checking user home directory for dotfile configs: ${homeDir}`)
279+
280+
for (const configPath of homeRootPatterns) {
281+
for (const ext of extensions) {
282+
const fullPath = resolve(homeDir, `${configPath}${ext}`)
283+
const config = await tryLoadConfig(fullPath, configWithEnvVars, arrayStrategy)
284+
if (config !== null) {
285+
if (verbose)
286+
log.success(`Configuration loaded from user home directory: ${fullPath}`)
287+
return config
288+
}
289+
}
290+
}
291+
}
292+
269293
// Then try package.json (for both name and alias)
270294
try {
271295
const pkgPath = resolve(baseDir, 'package.json')

packages/bunfig/test/home-config-integration.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ describe('Home Config Directory Integration Tests', () => {
3333
rmSync(testHomeConfigDir, { recursive: true })
3434
}
3535

36+
// Helper to clean up root-level home dotfiles for a specific test name/alias
37+
const _cleanupHomeDotfiles = (testName: string, alias?: string) => {
38+
const home = homedir()
39+
const files = [`.${testName}.config.ts`, `.${testName}.config.js`, `.${testName}.config.mjs`, `.${testName}.config.cjs`, `.${testName}.config.json`]
40+
if (alias) {
41+
files.push(`.${alias}.config.ts`, `.${alias}.config.js`, `.${alias}.config.mjs`, `.${alias}.config.cjs`, `.${alias}.config.json`)
42+
}
43+
for (const f of files) {
44+
const p = resolve(home, f)
45+
if (existsSync(p))
46+
rmSync(p)
47+
}
48+
}
49+
3650
describe('Real home config loading', () => {
3751
it('should load config from real ~/.config/$name/config.ts', async () => {
3852
const testName = generateTestName()
@@ -62,7 +76,7 @@ describe('Home Config Directory Integration Tests', () => {
6276
})
6377
}
6478
finally {
65-
cleanupHomeConfig(testName)
79+
_cleanupHomeDotfiles(testName)
6680
}
6781
})
6882

0 commit comments

Comments
 (0)