Skip to content

Commit 053d31b

Browse files
committed
Introduce an interface IPackageJsonWithVersion to avoid constantly having to check for an undefined "version" in all the places where the version is guaranteed to exist
1 parent f2920db commit 053d31b

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

apps/api-extractor/src/api/Extractor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
JsonSchema,
1313
Path,
1414
FileSystem,
15-
IPackageJson,
1615
NewlineKind,
17-
PackageJsonLookup
16+
PackageJsonLookup,
17+
IPackageJsonWithVersion
1818
} from '@microsoft/node-core-library';
1919
import {
2020
IExtractorConfig,
@@ -99,7 +99,7 @@ export class Extractor {
9999
/**
100100
* Returns the version number of the API Extractor NPM package.
101101
*/
102-
public static get version(): string | undefined {
102+
public static get version(): string {
103103
return Extractor._getPackageJson().version;
104104
}
105105

@@ -110,7 +110,7 @@ export class Extractor {
110110
return Extractor._getPackageJson().name;
111111
}
112112

113-
private static _getPackageJson(): IPackageJson {
113+
private static _getPackageJson(): IPackageJsonWithVersion {
114114
return PackageJsonLookup.loadOwnPackageJson(__dirname);
115115
}
116116

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,22 @@ export interface IPackageJson {
148148
*/
149149
scripts?: IPackageJsonScriptTable;
150150
}
151+
152+
/**
153+
* Describes a `IPackageJson` object whose `version` field is guaranteed to be defined.
154+
*
155+
* @remarks
156+
* The `IPackageJsonWithVersion` interface can be used with package.json files that were
157+
* obtained from an NPM registry, or installed in the `node_modules` folder.
158+
* According to the {@link https://docs.npmjs.com/files/package.json | NPM documentation},
159+
* the `version` field will always be defined for published NPM packages.
160+
*
161+
* But this is not true in general. For example, the
162+
* {@link https://nodejs.org/dist/latest-v10.x/docs/api/modules.html#modules_folders_as_modules
163+
* | NodeJS documentation} does not require the `version` field when using the `require()` API
164+
* to import folders as modules.
165+
*/
166+
export interface IPackageJsonWithVersion extends IPackageJson {
167+
/** {@inheritDoc IPackageJson.version} */
168+
version: string;
169+
}

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

Lines changed: 10 additions & 3 deletions
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, IPackageJsonWithVersion } from './IPackageJson';
99
import { FileConstants } from './Constants';
1010
import { FileSystem } from './FileSystem';
1111

@@ -66,7 +66,7 @@ export class PackageJsonLookup {
6666
* @returns This function always returns a valid `IPackageJson` object. If any problems are encountered during
6767
* loading, an exception will be thrown instead.
6868
*/
69-
public static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson {
69+
public static loadOwnPackageJson(dirnameOfCaller: string): IPackageJsonWithVersion {
7070
const packageJson: IPackageJson | undefined = PackageJsonLookup._loadOwnPackageJsonLookup
7171
.tryLoadPackageJsonFor(dirnameOfCaller);
7272

@@ -75,7 +75,14 @@ export class PackageJsonLookup {
7575
+ ` The __dirname was: ${dirnameOfCaller}`);
7676
}
7777

78-
return packageJson;
78+
if (packageJson.version !== undefined) {
79+
return packageJson as IPackageJsonWithVersion;
80+
}
81+
82+
const errorPath: string = PackageJsonLookup._loadOwnPackageJsonLookup.tryGetPackageJsonFilePathFor(dirnameOfCaller)
83+
|| 'package.json';
84+
throw new Error(`PackageJsonLookup.loadOwnPackageJson() failed because the "version" field is missing in`
85+
+ ` ${errorPath}`);
7986
}
8087

8188
constructor(parameters?: IPackageJsonLookupParameters) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export {
2020
} from './Executable';
2121
export {
2222
IPackageJson,
23+
IPackageJsonWithVersion,
2324
IPackageJsonDependencyTable,
2425
IPackageJsonScriptTable,
2526
IPackageJsonTsdocConfiguration

0 commit comments

Comments
 (0)