@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
32 lines (30 loc) • 1.06 kB
JavaScript
import { cacheMemory } from "./cacheMemory.mjs";
import { existsSync } from "node:fs";
import { dirname, join } from "node:path";
//#region src/utils/getPackageJsonPath.ts
const MAX_LEVELS = 15;
const getPackageJsonPath = (startDir = process.cwd()) => {
const checkedCache = cacheMemory.get("packageJsonPath", startDir);
if (checkedCache) return checkedCache;
let currentDir = startDir;
for (let level = 0; level < MAX_LEVELS; level++) {
const packageJsonPath = join(currentDir, "package.json");
if (existsSync(packageJsonPath)) {
cacheMemory.set("packageJsonPath", startDir, {
packageJsonPath,
baseDir: currentDir
});
return {
packageJsonPath,
baseDir: currentDir
};
}
const parentDir = dirname(currentDir);
if (parentDir === currentDir) break;
currentDir = parentDir;
}
throw new Error(`Could not find package.json in current directory or any of the ${MAX_LEVELS} parent directories. Searched from: ${startDir}`);
};
//#endregion
export { getPackageJsonPath };
//# sourceMappingURL=getPackageJsonPath.mjs.map