|
| 1 | +/** |
| 2 | + * @file readPackageJson |
| 3 | + * @module mlly/utils/readPackageJson |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + ERR_INVALID_PACKAGE_CONFIG, |
| 8 | + type NodeError |
| 9 | +} from '@flex-development/errnode' |
| 10 | +import pathe from '@flex-development/pathe' |
| 11 | +import type { PackageJson } from '@flex-development/pkg-types' |
| 12 | +import type { Nullable } from '@flex-development/tutils' |
| 13 | +import fs from 'node:fs' |
| 14 | +import { URL, fileURLToPath } from 'node:url' |
| 15 | + |
| 16 | +/** |
| 17 | + * Reads a `package.json` file from the given directory. |
| 18 | + * |
| 19 | + * Returns `null` if a file is not found. |
| 20 | + * |
| 21 | + * Throws [`ERR_INVALID_PACKAGE_CONFIG`][1] if the file found is not valid JSON. |
| 22 | + * |
| 23 | + * [1]: https://nodejs.org/api/errors.html#err_invalid_package_config |
| 24 | + * |
| 25 | + * @param {URL | string} [dir='.'] - Id of directory containing `package.json` |
| 26 | + * @param {string?} [specifier] - Module specifier passed by user to initiate |
| 27 | + * reading of `package.json` file |
| 28 | + * @param {string?} [parent] - Id of module `specifier` is relative to |
| 29 | + * @return {?PackageJson} `package.json` object or `null` if file is not found |
| 30 | + * @throws {NodeError} If `package.json` file does not contain valid JSON |
| 31 | + */ |
| 32 | +const readPackageJson = ( |
| 33 | + dir: URL | string = '.', |
| 34 | + specifier?: string, |
| 35 | + parent?: string |
| 36 | +): Nullable<PackageJson> => { |
| 37 | + // ensure dir is a path |
| 38 | + if (dir instanceof URL) dir = dir.pathname |
| 39 | + else if (dir.startsWith('file:')) dir = fileURLToPath(dir) |
| 40 | + |
| 41 | + /** |
| 42 | + * Full path to `package.json` file. |
| 43 | + * |
| 44 | + * @const {string} path |
| 45 | + */ |
| 46 | + const path: string = pathe.toNamespacedPath(pathe.join(dir, 'package.json')) |
| 47 | + |
| 48 | + // return null if package.json file does not exist |
| 49 | + if (!fs.existsSync(path)) return null |
| 50 | + |
| 51 | + /** |
| 52 | + * Possible `package.json` object. |
| 53 | + * |
| 54 | + * @var {PackageJson} pkg |
| 55 | + */ |
| 56 | + let pkg: PackageJson |
| 57 | + |
| 58 | + // try parsing package.json file |
| 59 | + try { |
| 60 | + pkg = JSON.parse(fs.readFileSync(path, 'utf8')) |
| 61 | + } catch (e: unknown) { |
| 62 | + /** |
| 63 | + * String containing module specifier passed by user to initiate reading of |
| 64 | + * `package.json` file and the location the module specifier was imported |
| 65 | + * from. |
| 66 | + * |
| 67 | + * @var {string | undefined} base |
| 68 | + */ |
| 69 | + let base: string | undefined = specifier ? `'${specifier}'` : undefined |
| 70 | + |
| 71 | + // add specifier import location |
| 72 | + if (base && parent) base += ` from ${parent}` |
| 73 | + |
| 74 | + throw new ERR_INVALID_PACKAGE_CONFIG(path, base, (e as SyntaxError).message) |
| 75 | + } |
| 76 | + |
| 77 | + return pkg |
| 78 | +} |
| 79 | + |
| 80 | +export default readPackageJson |
0 commit comments