@launchql/env
Version:
LaunchQL environment management
24 lines (23 loc) • 790 B
JavaScript
import { existsSync } from 'fs';
import { dirname, resolve } from 'path';
/**
* Recursively walks up directories to find a specific file (sync version).
* @param startDir - Starting directory.
* @param filename - The target file to search for.
* @returns The directory path containing the file.
*/
export const walkUp = (startDir, filename) => {
let currentDir = resolve(startDir);
while (currentDir) {
const targetPath = resolve(currentDir, filename);
if (existsSync(targetPath)) {
return currentDir;
}
const parentDir = dirname(currentDir);
if (parentDir === currentDir) {
break;
}
currentDir = parentDir;
}
throw new Error(`File "${filename}" not found in any parent directories.`);
};