Skip to content

Commit 8177946

Browse files
committed
add smarter config search
1 parent 8d5c34b commit 8177946

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

.changeset/eighty-pugs-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bluecadet/launchpad-cli": patch
3+
---
4+
5+
Smarter/faster config search

packages/cli/src/utils/config.ts

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,44 @@ const DEFAULT_CONFIG_PATHS = [
1414
];
1515

1616
/**
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.
1919
*/
2020
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(", ")}`);
3024
}
31-
32-
return null;
25+
return configs.length > 0 ? configs[0] : null;
3326
}
3427

3528
/**
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.
3831
*/
39-
function findFirstFileRecursive(filePath: string) {
32+
function findAllConfigsRecursive() {
4033
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();
4936

5037
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+
}
5343
}
5444

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) {
6047
// Can't navigate any more levels up
6148
break;
6249
}
6350

64-
absPath = parentPath;
51+
currentDir = parentDir;
6552
}
6653

67-
return null;
54+
return foundConfigs;
6855
}
6956

7057
export async function loadConfigFromFile<T>(configPath: string): Promise<Partial<T>> {

0 commit comments

Comments
 (0)