vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
26 lines (25 loc) • 940 B
JavaScript
export { findFile };
import path from 'node:path';
import fs from 'node:fs';
import { isArray } from './isArray.js';
import { assertPosixPath } from './path.js';
// We need to be able to crawl the filesystem, regardless of Vike's `$ git ls-files` command call, because we need to fallback if the user didn't setup Git (e.g. we cannot remove the tinyglobby fallback).
function findFile(arg, cwd) {
assertPosixPath(cwd);
const filenames = isArray(arg) ? arg : [arg];
let dir = cwd;
while (true) {
for (const filename of filenames) {
const configFilePath = path.posix.join(dir, `./${filename}`);
if (fs.existsSync(configFilePath)) {
assertPosixPath(configFilePath);
return configFilePath;
}
}
const dirPrevious = dir;
dir = path.posix.dirname(dir);
if (dir === dirPrevious) {
return null;
}
}
}