File tree Expand file tree Collapse file tree 4 files changed +34
-7
lines changed
libraries/node-core-library/src Expand file tree Collapse file tree 4 files changed +34
-7
lines changed Original file line number Diff line number Diff 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' ;
1919import {
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
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 55
66import * as path from 'path' ;
77import { JsonFile } from './JsonFile' ;
8- import { IPackageJson } from './IPackageJson' ;
8+ import { IPackageJson , IPackageJsonWithVersion } from './IPackageJson' ;
99import { FileConstants } from './Constants' ;
1010import { 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 ) {
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ export {
2020} from './Executable' ;
2121export {
2222 IPackageJson ,
23+ IPackageJsonWithVersion ,
2324 IPackageJsonDependencyTable ,
2425 IPackageJsonScriptTable ,
2526 IPackageJsonTsdocConfiguration
You can’t perform that action at this time.
0 commit comments