fix: (some minor improvements to configs and getPackageJson return behaviour) (#12510)

Co-authored-by: Tommaso Sciortino <sciortino@gmail.com>
This commit is contained in:
grMLEqomlkkU5Eeinz4brIrOVCUCkJuN
2025-11-25 05:20:23 +08:00
committed by GitHub
parent f6ee025c46
commit d53a5c4fb5
3 changed files with 15 additions and 4 deletions
+15 -2
View File
@@ -15,13 +15,26 @@ export type PackageJson = BasePackageJson & {
};
};
/**
* Reads package.json from the current directory or any parent directory.
*
* @param cwd - The directory to start searching from (searches upward to filesystem root)
* @returns The package.json object if found, or `undefined` if no package.json exists
* in the directory hierarchy. This is expected behavior when called from
* directories outside of a Node.js project.
*
* @example
* ```ts
* const pkg = await getPackageJson(__dirname);
* const version = pkg?.version ?? 'unknown';
* ```
*/
export async function getPackageJson(
cwd: string,
): Promise<PackageJson | undefined> {
const result = await readPackageUp({ cwd });
if (!result) {
// TODO: Maybe bubble this up as an error.
return;
return undefined;
}
return result.packageJson;