blob: 57f92e4b342490ed01d070c1bd3c40bf0c50b461 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import fs from "fs";
import path from "path";
function searchUp({
target,
cwd,
contentCheck,
}: {
target: string;
cwd: string;
contentCheck?: (content: string) => boolean;
}): string | null {
const root = path.parse(cwd).root;
let found = false;
while (!found && cwd !== root) {
if (contentCheck) {
try {
const content = fs.readFileSync(path.join(cwd, target)).toString();
if (contentCheck(content)) {
found = true;
break;
}
} catch {
// keep looking
}
} else {
if (fs.existsSync(path.join(cwd, target))) {
found = true;
break;
}
}
cwd = path.dirname(cwd);
}
if (found) {
return cwd;
}
return null;
}
export default searchUp;
|