@@ -28,6 +28,11 @@ export interface EntryPoint extends JsonObject {
28
28
name : string ;
29
29
/** The path to this entry point. */
30
30
path : AbsoluteFsPath ;
31
+ /**
32
+ * The name of the package that contains this entry-point (e.g. `@angular/core` or
33
+ * `@angular/common`).
34
+ */
35
+ packageName : string ;
31
36
/** The path to the package that contains this entry-point. */
32
37
packagePath : AbsoluteFsPath ;
33
38
/** The parsed package.json file for this entry-point. */
@@ -131,7 +136,8 @@ export function getEntryPointInfo(
131
136
const loadedEntryPointPackageJson = ( packagePackageJsonPath === entryPointPackageJsonPath ) ?
132
137
loadedPackagePackageJson :
133
138
loadPackageJson ( fs , entryPointPackageJsonPath ) ;
134
- const packageVersion = getPackageVersion ( loadedPackagePackageJson ) ;
139
+ const { packageName, packageVersion} = getPackageNameAndVersion (
140
+ fs , packagePath , loadedPackagePackageJson , loadedEntryPointPackageJson ) ;
135
141
const entryPointConfig =
136
142
config . getPackageConfig ( packagePath , packageVersion ) . entryPoints [ entryPointPath ] ;
137
143
let entryPointPackageJson : EntryPointPackageJson ;
@@ -173,6 +179,7 @@ export function getEntryPointInfo(
173
179
const entryPointInfo : EntryPoint = {
174
180
name : entryPointPackageJson . name ,
175
181
path : entryPointPath ,
182
+ packageName,
176
183
packagePath,
177
184
packageJson : entryPointPackageJson ,
178
185
typings : resolve ( entryPointPath , typings ) ,
@@ -304,15 +311,44 @@ function guessTypingsFromPackageJson(
304
311
}
305
312
306
313
/**
307
- * Find the version of the package at `packageJsonPath` .
314
+ * Find or infer the name and version of a package.
308
315
*
309
- * The version is read off of the `version` property of the package's `package.json` file (if
310
- * available).
316
+ * - The name is computed based on the `name` property of the package's or the entry-point's
317
+ * `package.json` file (if available) or inferred from the package's path.
318
+ * - The version is read off of the `version` property of the package's `package.json` file (if
319
+ * available).
311
320
*
312
- * @param packageJson the parsed `package.json` of the package (if available).
313
- * @returns the version string or `null` if the `pckage.json` file is missing or doesn't contain a
314
- * version.
321
+ * @param fs The `FileSystem` instance to use for parsing `packagePath` (if needed).
322
+ * @param packagePath the absolute path to the package.
323
+ * @param packagePackageJson the parsed `package.json` of the package (if available).
324
+ * @param entryPointPackageJson the parsed `package.json` of an entry-point (if available).
325
+ * @returns the computed name and version of the package.
315
326
*/
316
- function getPackageVersion ( packageJson : EntryPointPackageJson | null ) : string | null {
317
- return packageJson ?. version ?? null ;
327
+ function getPackageNameAndVersion (
328
+ fs : FileSystem , packagePath : AbsoluteFsPath , packagePackageJson : EntryPointPackageJson | null ,
329
+ entryPointPackageJson : EntryPointPackageJson |
330
+ null ) : { packageName : string , packageVersion : string | null } {
331
+ let packageName : string ;
332
+
333
+ if ( packagePackageJson !== null ) {
334
+ // We have a valid `package.json` for the package: Get the package name from that.
335
+ packageName = packagePackageJson . name ;
336
+ } else if ( entryPointPackageJson !== null ) {
337
+ // We have a valid `package.json` for the entry-point: Get the package name from that.
338
+ // This might be a secondary entry-point, so make sure we only keep the main package's name
339
+ // (e.g. only keep `@angular/common` from `@angular/common/http`).
340
+ packageName = / ^ (?: @ [ ^ / ] + \/ ) ? [ ^ / ] * / . exec ( entryPointPackageJson . name ) ! [ 0 ] ;
341
+ } else {
342
+ // We don't have a valid `package.json`: Infer the package name from the package's path.
343
+ const lastSegment = fs . basename ( packagePath ) ;
344
+ const secondLastSegment = fs . basename ( fs . dirname ( packagePath ) ) ;
345
+
346
+ packageName =
347
+ secondLastSegment . startsWith ( '@' ) ? `${ secondLastSegment } /${ lastSegment } ` : lastSegment ;
348
+ }
349
+
350
+ return {
351
+ packageName,
352
+ packageVersion : packagePackageJson ?. version ?? null ,
353
+ } ;
318
354
}
0 commit comments