Skip to content

Commit 032bd7a

Browse files
aleclarsonantfu
andauthoredMar 4, 2024
feat: find config in parent directory (#8)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 0ef9933 commit 032bd7a

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"@jsdevtools/ez-spawn": "^3.0.4",
6363
"c12": "^1.9.0",
6464
"cac": "^6.7.14",
65+
"escalade": "^3.1.2",
6566
"fast-glob": "^3.3.2",
6667
"js-yaml": "^4.1.0",
6768
"prompts": "^2.4.2",

‎pnpm-lock.yaml

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/config.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import process from 'node:process'
2+
import { dirname } from 'node:path'
23
import { loadConfig } from 'c12'
4+
import escalade from 'escalade/sync'
35
import type { VersionBumpOptions } from './types/version-bump-options'
46

57
export const bumpConfigDefaults: VersionBumpOptions = {
@@ -18,25 +20,51 @@ export async function loadBumpConfig(
1820
overrides?: Partial<VersionBumpOptions>,
1921
cwd = process.cwd(),
2022
) {
21-
const { config: bumppConfig } = await loadConfig<VersionBumpOptions>({
22-
name: 'bumpp',
23-
overrides: {
24-
...(overrides as VersionBumpOptions),
25-
},
26-
cwd,
27-
})
23+
const name = 'bump'
24+
const configFile = findConfigFile(name, cwd)
2825
const { config } = await loadConfig<VersionBumpOptions>({
29-
name: 'bump',
26+
name,
3027
defaults: bumpConfigDefaults,
3128
overrides: {
32-
...(bumppConfig!),
29+
...(overrides as VersionBumpOptions),
3330
},
34-
cwd,
31+
cwd: configFile ? dirname(configFile) : cwd,
3532
})
36-
3733
return config!
3834
}
3935

36+
function findConfigFile(name: string, cwd: string) {
37+
let foundRepositoryRoot = false
38+
try {
39+
const candidates = ['js', 'mjs', 'ts', 'mts', 'json'].map(ext => `${name}.config.${ext}`)
40+
return escalade(cwd, (_dir, files) => {
41+
const match = files.find((file) => {
42+
if (candidates.includes(file))
43+
return true
44+
if (file === '.git')
45+
foundRepositoryRoot = true
46+
return false
47+
})
48+
49+
if (match)
50+
return match
51+
52+
// Stop at the repository root.
53+
if (foundRepositoryRoot) {
54+
// eslint-disable-next-line no-throw-literal
55+
throw null
56+
}
57+
58+
return false
59+
})
60+
}
61+
catch (error) {
62+
if (foundRepositoryRoot)
63+
return null
64+
throw error
65+
}
66+
}
67+
4068
export function defineConfig(config: Partial<VersionBumpOptions>) {
4169
return config
4270
}

0 commit comments

Comments
 (0)
Please sign in to comment.