UNPKG

zephyr-agent

Version:
33 lines (32 loc) • 1.16 kB
import "node:module"; import { accessSync } from "node:fs"; import { constants, readFile } from "node:fs/promises"; import { join, resolve } from "node:path"; import { ZeErrors, ZephyrError } from "../errors/index.mjs"; import { ze_log } from "../logging/debug.mjs"; const max_retry = 30; async function find_nearest_package_json(startPath) { if (!startPath) throw new ZephyrError(ZeErrors.ERR_PACKAGE_JSON_NOT_FOUND); let retry = 0; let dir = startPath; do { retry++; if (retry > max_retry) break; const packageJsonPath = join(dir, 'package.json'); try { accessSync(packageJsonPath, constants.F_OK); return { path: packageJsonPath, json: await readFile(packageJsonPath, 'utf8') }; } catch (e) { ze_log["package"](e); } const parentDir = resolve(dir, '..'); if (parentDir === dir) break; dir = parentDir; }while (startPath !== dir); throw new ZephyrError(ZeErrors.ERR_PACKAGE_JSON_NOT_FOUND); } export { find_nearest_package_json }; //# sourceMappingURL=find-nearest-package-json.mjs.map