Skip to main content
The Deno 2 Release Candidate is here
Learn more
Module

x/tsafe/tools/getProjectRoot.ts

🔩 The missing TypeScript utils
Go to Latest
File
const __dirname = (() => { const { url: urlStr } = import.meta; const url = new URL(urlStr); const __filename = (url.protocol === "file:" ? url.pathname : urlStr) .replace(/[/][^/]*$/, '');
const isWindows = (() => {
let NATIVE_OS: typeof Deno.build.os = "linux"; // eslint-disable-next-line @typescript-eslint/no-explicit-any const navigator = (globalThis as any).navigator; if (globalThis.Deno != null) { NATIVE_OS = Deno.build.os; } else if (navigator?.appVersion?.includes?.("Win") ?? false) { NATIVE_OS = "windows"; }
return NATIVE_OS == "windows";
})();
return isWindows ? __filename.split("/").join("\\").substring(1) : __filename;})();
import * as fs from "node:fs";import * as path from "node:path";
function getProjectRootRec(dirPath: string): string { if (fs.existsSync(path.join(dirPath, "package.json"))) { return dirPath; } return getProjectRootRec(path.join(dirPath, ".."));}
let result: string | undefined = undefined;
export function getProjectRoot(): string { if (result !== undefined) { return result; }
return (result = getProjectRootRec(__dirname));}