1
1
import process from 'node:process'
2
+ import { dirname } from 'node:path'
2
3
import { loadConfig } from 'c12'
4
+ import escalade from 'escalade/sync'
3
5
import type { VersionBumpOptions } from './types/version-bump-options'
4
6
5
7
export const bumpConfigDefaults : VersionBumpOptions = {
@@ -18,25 +20,51 @@ export async function loadBumpConfig(
18
20
overrides ?: Partial < VersionBumpOptions > ,
19
21
cwd = process . cwd ( ) ,
20
22
) {
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 )
28
25
const { config } = await loadConfig < VersionBumpOptions > ( {
29
- name : 'bump' ,
26
+ name,
30
27
defaults : bumpConfigDefaults ,
31
28
overrides : {
32
- ...( bumppConfig ! ) ,
29
+ ...( overrides as VersionBumpOptions ) ,
33
30
} ,
34
- cwd,
31
+ cwd : configFile ? dirname ( configFile ) : cwd ,
35
32
} )
36
-
37
33
return config !
38
34
}
39
35
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
+
40
68
export function defineConfig ( config : Partial < VersionBumpOptions > ) {
41
69
return config
42
70
}
0 commit comments