X Tutup
Skip to content

Commit 1446574

Browse files
committed
Introduce two new APIs loadNodePackageJson() and tryLoadNodePackageJsonFor() that return INodePackageJson
1 parent 066ad27 commit 1446574

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

common/reviews/api/node-core-library.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,12 @@ declare class PackageJsonLookup {
423423
// (undocumented)
424424
constructor(parameters?: IPackageJsonLookupParameters);
425425
clearCache(): void;
426+
loadNodePackageJson(jsonFilename: string): INodePackageJson;
426427
static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson;
427428
loadPackageJson(jsonFilename: string): IPackageJson;
428429
tryGetPackageFolderFor(fileOrFolderPath: string): string | undefined;
429430
tryGetPackageJsonFilePathFor(fileOrFolderPath: string): string | undefined;
431+
tryLoadNodePackageJsonFor(fileOrFolderPath: string): INodePackageJson | undefined;
430432
tryLoadPackageJsonFor(fileOrFolderPath: string): IPackageJson | undefined;
431433
}
432434

libraries/node-core-library/src/PackageJsonLookup.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as path from 'path';
77
import { JsonFile } from './JsonFile';
8-
import { IPackageJson } from './IPackageJson';
8+
import { IPackageJson, INodePackageJson } from './IPackageJson';
99
import { FileConstants } from './Constants';
1010
import { FileSystem } from './FileSystem';
1111

@@ -177,6 +177,18 @@ export class PackageJsonLookup {
177177
return this.loadPackageJson(packageJsonFilePath);
178178
}
179179

180+
/**
181+
* This function is similar to {@link tryLoadPackageJsonFor}, except that it does not report an error if the
182+
* `version` field is missing from the package.json file.
183+
*/
184+
public tryLoadNodePackageJsonFor(fileOrFolderPath: string): INodePackageJson | undefined {
185+
const packageJsonFilePath: string | undefined = this.tryGetPackageJsonFilePathFor(fileOrFolderPath);
186+
if (!packageJsonFilePath) {
187+
return undefined;
188+
}
189+
return this.loadNodePackageJson(packageJsonFilePath);
190+
}
191+
180192
/**
181193
* Loads the specified package.json file, if it is not already present in the cache.
182194
*
@@ -189,6 +201,21 @@ export class PackageJsonLookup {
189201
* @param jsonFilename - a relative or absolute path to a package.json file
190202
*/
191203
public loadPackageJson(jsonFilename: string): IPackageJson {
204+
const packageJson: INodePackageJson = this.loadNodePackageJson(jsonFilename);
205+
206+
if (!packageJson.version) {
207+
throw new Error(`Error reading "${jsonFilename}":\n `
208+
+ 'The required field "version" was not found');
209+
}
210+
211+
return packageJson as IPackageJson;
212+
}
213+
214+
/**
215+
* This function is similar to {@link loadPackageJson}, except that it does not report an error if the
216+
* `version` field is missing from the package.json file.
217+
*/
218+
public loadNodePackageJson(jsonFilename: string): INodePackageJson {
192219
if (!FileSystem.exists(jsonFilename)) {
193220
throw new Error(`Input file not found: ${jsonFilename}`);
194221
}

libraries/node-core-library/src/test/PackageJsonLookup.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as path from 'path';
55
import { PackageJsonLookup } from '../PackageJsonLookup';
6-
import { IPackageJson } from '../IPackageJson';
6+
import { IPackageJson, INodePackageJson } from '../IPackageJson';
77
import { FileConstants } from '../Constants';
88

99
describe('PackageJsonLookup', () => {
@@ -28,10 +28,10 @@ describe('PackageJsonLookup', () => {
2828
}
2929
});
3030

31-
test('tryLoadPackageJsonFor() test package with no version', () => {
31+
test('tryLoadNodePackageJsonFor() test package with no version', () => {
3232
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
3333
const sourceFilePath: string = path.join(__dirname, './test-data/example-package-no-version');
34-
const packageJson: IPackageJson | undefined = packageJsonLookup.tryLoadPackageJsonFor(sourceFilePath);
34+
const packageJson: INodePackageJson | undefined = packageJsonLookup.tryLoadNodePackageJsonFor(sourceFilePath);
3535
expect(packageJson).toBeDefined();
3636
if (packageJson) {
3737
expect(packageJson.name).toEqual('example-package');

0 commit comments

Comments
 (0)
X Tutup