Skip to content

Commit 8370c1c

Browse files
authored
chore(refactor): refactor findConfigFile (#416)
1 parent 05010e8 commit 8370c1c

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

packages/cli/src/config/loadConfig.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
2-
import path from 'path';
32
import type { RawOptions } from './types.js';
3+
import { findConfigFile } from '../core/helpers/findConfigFile.js';
44
import {
55
printConfigLoaded,
66
printConfigLoadError,
@@ -17,17 +17,7 @@ import {
1717
export function loadConfig(cliOptions: Partial<RawOptions>): RawOptions {
1818
const cwd = process.cwd();
1919

20-
// Recursive search upwards for dotenv-diff.config.json
21-
function findConfigFile(dir: string): string | null {
22-
const configPath = path.resolve(dir, 'dotenv-diff.config.json');
23-
if (fs.existsSync(configPath)) return configPath;
24-
25-
const parent = path.dirname(dir);
26-
if (parent !== dir) return findConfigFile(parent);
27-
return null;
28-
}
29-
30-
const foundPath = findConfigFile(cwd);
20+
const foundPath = findConfigFile(cwd, 'dotenv-diff.config.json');
3121

3222
let fileConfig: Partial<RawOptions> = {};
3323

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
/**
4+
* Recursive search upwards for a config file with the given name, starting from the specified directory.
5+
* fx: dotenv-diff.config.json or dotenv-diff.baseline.json
6+
* @param dir - Directory to start searching from
7+
* @param file - The name of the config file to search for
8+
* @returns The path to the config file if found, otherwise null
9+
*/
10+
export function findConfigFile(dir: string, file: string): string | null {
11+
const configPath = path.resolve(dir, file);
12+
if (fs.existsSync(configPath)) return configPath;
13+
14+
const parent = path.dirname(dir);
15+
if (parent !== dir) return findConfigFile(parent, file);
16+
return null;
17+
}

0 commit comments

Comments
 (0)