@codemod-utils/json
Version:
Utilities for handling JSON
49 lines (48 loc) • 1.23 kB
JavaScript
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
/**
* Reads `package.json` and returns the parsed JSON.
*
* @param options
*
* An object with `projectRoot`.
*
* @return
*
* A JSON that represents `package.json`.
*
* @example
*
* Check if the project, against which the codemod is run,
* has `typescript` as a dependency.
*
* ```ts
* const { dependencies, devDependencies } = readPackageJson({
* projectRoot,
* });
*
* const projectDependencies = new Map([
* ...Object.entries(dependencies ?? {}),
* ...Object.entries(devDependencies ?? {}),
* ]);
*
* const hasTypeScript = projectDependencies.has('typescript');
* ```
*/
export function readPackageJson(options) {
const { projectRoot } = options;
const filePath = join(projectRoot, 'package.json');
if (!existsSync(filePath)) {
throw new SyntaxError(`ERROR: package.json is missing.\n`);
}
try {
const file = readFileSync(filePath, 'utf8');
return JSON.parse(file);
}
catch (error) {
if (error instanceof Error) {
throw new SyntaxError(`ERROR: package.json is not valid. (${error.message})\n`);
}
throw error;
}
}