@augment-vir/node
Version:
A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.
29 lines (24 loc) • 1.01 kB
text/typescript
import {check} from '@augment-vir/assert';
import {join} from 'node:path';
import {type PackageJson} from 'type-fest';
import {readJsonFile} from '../fs/json.js';
/**
* Read the `package.json` file contained within the given directory.
*
* @category Node : Npm
* @category Package : @augment-vir/node
* @throws `TypeError` if the given directory has no `package.json` or the `package.json` is
* invalid.
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
*/
export async function readPackageJson(dirPath: string): Promise<PackageJson> {
const packageJsonPath = join(dirPath, 'package.json');
const packageJson = (await readJsonFile(packageJsonPath)) as PackageJson | undefined;
if (!packageJson) {
throw new TypeError(`package.json file does not exist in '${dirPath}'`);
}
if (!check.isObject(packageJson)) {
throw new TypeError(`Parsing package.json file did not return an object in '${dirPath}'`);
}
return packageJson;
}