@@ -14,57 +14,44 @@ const DEFAULT_CONFIG_PATHS = [
14
14
] ;
15
15
16
16
/**
17
- * Searches for a config file in the current and parent directories, up to a max depth of 64.
18
- * @returns {string | null } Absolute path to the config file or null if none can be found.
17
+ * Searches for all config files in the current and parent directories, up to a max depth of 64.
18
+ * @returns {string[] } Array of absolute paths to the config files found.
19
19
*/
20
20
export function findConfig ( ) {
21
- for ( const defaultPath of DEFAULT_CONFIG_PATHS ) {
22
- const resolved = findFirstFileRecursive ( defaultPath ) ;
23
-
24
- if ( resolved ) {
25
- console . log ( `Found config '${ chalk . white ( resolved ) } '` ) ;
26
- return resolved ;
27
- }
28
-
29
- console . warn ( `Could not find config with name '${ chalk . white ( defaultPath ) } '` ) ;
21
+ const configs = findAllConfigsRecursive ( ) ;
22
+ if ( configs . length > 0 ) {
23
+ console . log ( `Found configs: ${ configs . map ( ( c ) => chalk . white ( c ) ) . join ( ", " ) } ` ) ;
30
24
}
31
-
32
- return null ;
25
+ return configs . length > 0 ? configs [ 0 ] : null ;
33
26
}
34
27
35
28
/**
36
- * Searches for a file in the current and parent directories, up to a max depth of 64.
37
- * @returns The absolute path to the file or null if it doesn't exist .
29
+ * Searches for all config files in the current and parent directories, up to a max depth of 64.
30
+ * @returns { string[] } Array of absolute paths to the config files found .
38
31
*/
39
- function findFirstFileRecursive ( filePath : string ) {
32
+ function findAllConfigsRecursive ( ) {
40
33
const maxDepth = 64 ;
41
-
42
- let absPath = filePath ;
43
-
44
- if ( process . env . INIT_CWD ) {
45
- absPath = path . resolve ( process . env . INIT_CWD , filePath ) ;
46
- } else {
47
- absPath = path . resolve ( filePath ) ;
48
- }
34
+ const foundConfigs = [ ] ;
35
+ let currentDir = process . env . INIT_CWD ? path . resolve ( process . env . INIT_CWD ) : process . cwd ( ) ;
49
36
50
37
for ( let i = 0 ; i < maxDepth ; i ++ ) {
51
- if ( fs . existsSync ( absPath ) ) {
52
- return absPath ;
38
+ for ( const defaultPath of DEFAULT_CONFIG_PATHS ) {
39
+ const candidatePath = path . join ( currentDir , defaultPath ) ;
40
+ if ( fs . existsSync ( candidatePath ) ) {
41
+ foundConfigs . push ( candidatePath ) ;
42
+ }
53
43
}
54
44
55
- const dirPath = path . dirname ( absPath ) ;
56
- const filePath = path . basename ( absPath ) ;
57
- const parentPath = path . resolve ( dirPath , ".." , filePath ) ;
58
-
59
- if ( absPath === parentPath ) {
45
+ const parentDir = path . resolve ( currentDir , ".." ) ;
46
+ if ( currentDir === parentDir ) {
60
47
// Can't navigate any more levels up
61
48
break ;
62
49
}
63
50
64
- absPath = parentPath ;
51
+ currentDir = parentDir ;
65
52
}
66
53
67
- return null ;
54
+ return foundConfigs ;
68
55
}
69
56
70
57
export async function loadConfigFromFile < T > ( configPath : string ) : Promise < Partial < T > > {
0 commit comments