Skip to main content
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 "https://deno.land/std@0.85.0/node/fs.ts";import * as path from "https://deno.land/std@0.85.0/node/path.ts";
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));}