import { cache } from "./cache.mjs";
import { dirname, join } from "node:path";
import { existsSync } from "node:fs";

//#region ../config/dist/esm/utils/getPackageJsonPath.mjs
const isESModule = typeof import.meta.url === "string";
const MAX_LEVELS = 15;
const getPackageJsonPath = (startDir = process.cwd()) => {
	const checkedCache = cache.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)) {
			cache.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