UNPKG

vlt

Version:
143 lines (140 loc) 3.75 kB
var global = globalThis; import {Buffer} from "node:buffer"; import {setTimeout,clearTimeout,setImmediate,clearImmediate,setInterval,clearInterval} from "node:timers"; import {createRequire as _vlt_createRequire} from "node:module"; var require = _vlt_createRequire(import.meta.filename); import { parse, stringify, walkUp } from "./chunk-SGEQHKFC.js"; import { asManifest, longDependencyTypes, normalizeManifest } from "./chunk-X4RDKJKD.js"; import { error } from "./chunk-RV3EHS4P.js"; // ../../src/package-json/src/index.ts import { readFileSync, writeFileSync, lstatSync } from "node:fs"; import { resolve } from "node:path"; import { homedir } from "node:os"; var exists = (path) => { try { lstatSync(path); return true; } catch { return false; } }; var PackageJson = class { /** * cache of `package.json` loads */ #cache = /* @__PURE__ */ new Map(); /** * cache of `package.json` paths by manifest */ #pathCache = /* @__PURE__ */ new Map(); /** * cache of load errors */ #errCache = /* @__PURE__ */ new Map(); /** * Reads and parses contents of a `package.json` file at a directory `dir`. * `reload` will optionally skip reading from the cache when set to `true`. */ read(dir, { reload } = {}) { const cachedPackageJson = !reload && this.#cache.get(dir); if (cachedPackageJson) { return cachedPackageJson; } const filename = dir.endsWith("package.json") ? resolve(dir) : resolve(dir, "package.json"); const fail = (err) => error("Could not read package.json file", err, this.read); const cachedError = !reload && this.#errCache.get(dir); if (cachedError) { throw fail(cachedError); } try { const res = normalizeManifest( asManifest( parse(readFileSync(filename, { encoding: "utf8" })) ) ); this.#cache.set(dir, res); this.#pathCache.set(res, dir); return res; } catch (err) { const ec = { path: filename, cause: err }; this.#errCache.set(dir, ec); throw fail(ec); } } write(dir, manifest, indent) { const filename = dir.endsWith("package.json") ? resolve(dir) : resolve(dir, "package.json"); this.fix(manifest); try { writeFileSync(filename, stringify(manifest, void 0, indent)); this.#cache.set(dir, manifest); this.#pathCache.set(manifest, dir); } catch (err) { this.#cache.delete(dir); this.#pathCache.delete(manifest); throw error( "Could not write package.json file", { path: filename, cause: err }, this.write ); } } save(manifest) { const dir = this.#pathCache.get(manifest); if (!dir) { throw error( "Could not save manifest", { manifest }, this.save ); } this.write(dir, manifest); } fix(manifest) { for (const depType of longDependencyTypes) { const deps = manifest[depType]; if (deps) { manifest[depType] = Object.fromEntries( Object.entries(deps).sort( ([a], [b]) => a.localeCompare(b, "en") ) ); } } } /** * Walks up the directory tree from the current working directory * and returns the path to the first `package.json` file found. * Returns undefined if no package.json is found. */ find(cwd = process.cwd(), home = homedir()) { for (const dir of walkUp(cwd)) { if (dir === home) break; const packageJsonPath = resolve(dir, "package.json"); if (exists(packageJsonPath)) { return packageJsonPath; } } } }; export { PackageJson }; //# sourceMappingURL=chunk-YWPMIBJS.js.map