forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindUpDir.mjs
33 lines (28 loc) · 887 Bytes
/
findUpDir.mjs
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
import { existsSync } from "fs";
import {
dirname,
join,
resolve,
} from "path";
import url from "url";
const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = dirname(__filename);
// search directories upward to avoid hard-wired paths based on the
// build tree (same as src/harness/findUpDir.ts)
/**
* @param {string} name
* @returns {string}
*/
export function findUpFile(name) {
let dir = __dirname;
while (true) {
const fullPath = join(dir, name);
if (existsSync(fullPath)) return fullPath;
const up = resolve(dir, "..");
if (up === dir) return name; // it'll fail anyway
dir = up;
}
}
/** @type {string | undefined} */
let findUpRootCache;
export const findUpRoot = () => findUpRootCache || (findUpRootCache = dirname(findUpFile("Herebyfile.mjs")));