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Content":["import { readFileSync } from 'node:fs'\nimport { dirname, resolve, sep } from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nconst NM = `${sep}node_modules${sep}`\nconst STORE = `.store${sep}`\nconst PKG = `${sep}package${sep}`\nconst DIST = `${sep}dist${sep}`\n\n/**\n * Find the package.json file, either from a TypeScript file somewhere not\n * in a 'dist' folder, or a built and/or installed 'dist' folder.\n *\n * Note: this *only* works if you build your code into `'./dist'`, and that the\n * source path does not also contain `'dist'`! If you don't build into\n * `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will\n * not work properly!\n *\n * The default `pathFromSrc` option assumes that the calling code lives one\n * folder below the root of the package. Otherwise, it must be specified.\n *\n * Example:\n *\n * ```ts\n * // src/index.ts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(import.meta.url)\n * console.log(`package.json found at ${pj}`)\n * ```\n *\n * If the caller is deeper within the project source, then you must provide\n * the appropriate fallback path:\n *\n * ```ts\n * // src/components/something.ts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(import.meta.url, '../../package.json')\n * console.log(`package.json found at ${pj}`)\n * ```\n *\n * When running from CommmonJS, use `__filename` instead of `import.meta.url`\n *\n * ```ts\n * // src/index.cts\n * import { findPackageJson } from 'package-json-from-dist'\n *\n * const pj = findPackageJson(__filename)\n * console.log(`package.json found at ${pj}`)\n * ```\n */\nexport const findPackageJson = (\n from: string | URL,\n pathFromSrc: string = '../package.json',\n): string => {\n const f =\n typeof from === 'object' || from.startsWith('file://') ?\n fileURLToPath(from)\n : from\n const __dirname = dirname(f)\n\n const nms = __dirname.lastIndexOf(NM)\n if (nms !== -1) {\n // inside of node_modules. find the dist directly under package name.\n const nm = __dirname.substring(0, nms + NM.length)\n const pkgDir = __dirname.substring(nms + NM.length)\n // affordance for yarn berry, which puts package contents in\n // '.../node_modules/.store/${id}-${hash}/package/...'\n if (pkgDir.startsWith(STORE)) {\n const pkg = pkgDir.indexOf(PKG, STORE.length)\n if (pkg) {\n return resolve(\n nm,\n pkgDir.substring(0, pkg + PKG.length),\n 'package.json',\n )\n }\n }\n const pkgName =\n pkgDir.startsWith('@') ?\n pkgDir.split(sep, 2).join(sep)\n : String(pkgDir.split(sep)[0])\n return resolve(nm, pkgName, 'package.json')\n } else {\n // see if we are in a dist folder.\n const d = __dirname.lastIndexOf(DIST)\n if (d !== -1) {\n return resolve(__dirname.substring(0, d), 'package.json')\n } else {\n return resolve(__dirname, pathFromSrc)\n }\n }\n}\n\n/**\n * Load the package.json file, either from a TypeScript file somewhere not\n * in a 'dist' folder, or a built and/or installed 'dist' folder.\n *\n * Note: this *only* works if you build your code into `'./dist'`, and that the\n * source path does not also contain `'dist'`! If you don't build into\n * `'./dist'`, or if you have files at `./src/dist/dist.ts`, then this will\n * not work properly!\n *\n * The default `pathFromSrc` option assumes that the calling code lives one\n * folder below the root of the package. Otherwise, it must be specified.\n *\n * Example:\n *\n * ```ts\n * // src/index.ts\n * import { loadPackageJson } from 'package-json-from-dist'\n *\n * const pj = loadPackageJson(import.meta.url)\n * console.log(`Hello from ${pj.name}@${pj.version}`)\n * ```\n *\n * If the caller is deeper within the project source, then you must provide\n * the appropriate fallback path:\n *\n * ```ts\n * // src/components/something.ts\n * import { loadPackageJson } from 'package-json-from-dist'\n *\n * const pj = loadPackageJson(import.meta.url, '../../package.json')\n * console.log(`Hello from ${pj.name}@${pj.version}`)\n * ```\n *\n * When running from CommmonJS, use `__filename` instead of `import.meta.url`\n *\n * ```ts\n * // src/index.cts\n * import { loadPackageJson } from 'package-json-from-dist'\n *\n * const pj = loadPackageJson(__filename)\n * console.log(`Hello from ${pj.name}@${pj.version}`)\n * ```\n */\nexport const loadPackageJson = (\n from: string | URL,\n pathFromSrc: string = '../package.json',\n) =>\n JSON.parse(readFileSync(findPackageJson(from, pathFromSrc), 'utf8'))\n"]}