@@ -16,12 +16,17 @@ interface IPackageJson {
1616 * and retrieving the name of the package. The results are cached.
1717 */
1818export default class PackageJsonLookup {
19- private _tryFindPackagePathUpwardsCache : Map < string , string | undefined > ;
20- private _readPackageNameCache : Map < string , string > ;
19+ // Cached the return values for tryFindPackagePathUpwards():
20+ // sourceFilePath --> packageJsonFolder
21+ private _packageFolderCache : Map < string , string | undefined > ;
22+
23+ // Cached the return values for readPackageName():
24+ // packageJsonPath --> packageName
25+ private _packageNameCache : Map < string , string > ;
2126
2227 constructor ( ) {
23- this . _tryFindPackagePathUpwardsCache = new Map < string , string > ( ) ;
24- this . _readPackageNameCache = new Map < string , string > ( ) ;
28+ this . _packageFolderCache = new Map < string , string | undefined > ( ) ;
29+ this . _packageNameCache = new Map < string , string > ( ) ;
2530 }
2631
2732 /**
@@ -32,25 +37,25 @@ export default class PackageJsonLookup {
3237 * @param currentPath - a path (relative or absolute) of the current location
3338 * @returns a relative path to the package folder
3439 */
35- public tryFindPackagePathUpwards ( currentPath : string ) : string | undefined {
40+ public tryFindPackagePathUpwards ( sourceFilePath : string ) : string | undefined {
3641 // Two lookups are required, because get() cannot distinguish the undefined value
3742 // versus a missing key.
38- if ( this . _tryFindPackagePathUpwardsCache . has ( currentPath ) ) {
39- return this . _tryFindPackagePathUpwardsCache . get ( currentPath ) ;
43+ if ( this . _packageFolderCache . has ( sourceFilePath ) ) {
44+ return this . _packageFolderCache . get ( sourceFilePath ) ;
4045 }
4146
4247 let result : string | undefined ;
4348
44- const parentFolder : string = path . dirname ( currentPath ) ;
45- if ( ! parentFolder || parentFolder === currentPath ) {
49+ const parentFolder : string = path . dirname ( sourceFilePath ) ;
50+ if ( ! parentFolder || parentFolder === sourceFilePath ) {
4651 result = undefined ;
4752 } else if ( fsx . existsSync ( path . join ( parentFolder , 'package.json' ) ) ) {
4853 result = path . normalize ( parentFolder ) ;
4954 } else {
5055 result = this . tryFindPackagePathUpwards ( parentFolder ) ;
5156 }
5257
53- this . _tryFindPackagePathUpwardsCache . set ( currentPath , result ) ;
58+ this . _packageFolderCache . set ( sourceFilePath , result ) ;
5459 return result ;
5560 }
5661
@@ -62,15 +67,15 @@ export default class PackageJsonLookup {
6267 * @returns the name of the package (E.g. @microsoft/api-extractor)
6368 */
6469 public readPackageName ( packageJsonPath : string ) : string {
65- let result : string = this . _readPackageNameCache . get ( packageJsonPath ) ;
70+ let result : string = this . _packageNameCache . get ( packageJsonPath ) ;
6671 if ( result !== undefined ) {
6772 return result ;
6873 }
6974
7075 const packageJson : IPackageJson = JsonFile . loadJsonFile ( path . join ( packageJsonPath , 'package.json' ) ) as IPackageJson ;
7176 result = packageJson . name ;
7277
73- this . _readPackageNameCache . set ( packageJsonPath , result ) ;
78+ this . _packageNameCache . set ( packageJsonPath , result ) ;
7479 return result ;
7580 }
7681}
0 commit comments