@@ -15,6 +15,15 @@ export interface PartialPackageJson {
15
15
main : string ;
16
16
}
17
17
18
+ export type VersionMap = Record < string , string > ;
19
+
20
+ export type PackageJsonInfo = {
21
+ content : any ;
22
+ directory : string ;
23
+ }
24
+
25
+ const packageCache : Record < string , PackageJsonInfo [ ] > = { } ;
26
+
18
27
export function findPackageJsonFiles ( project : string , workspace : string ) : string [ ] {
19
28
return expandFolders ( project , workspace )
20
29
. map ( f => path . join ( f , 'package.json' ) )
@@ -58,37 +67,62 @@ export function getPackageInfo(
58
67
throw new Error ( `Workspace folder ${ workspaceRoot } needs to be a parent of the project folder ${ projectRoot } ` ) ;
59
68
}
60
69
61
- let currentPath = projectRoot ;
70
+ const packageJsonInfos = getPackageJsonFiles ( projectRoot , workspaceRoot ) ;
62
71
63
- while ( workspaceRoot !== currentPath ) {
64
-
65
- const cand = _getPackageInfo ( packageName , currentPath ) ;
72
+ for ( const info of packageJsonInfos ) {
73
+ const cand = _getPackageInfo ( packageName , info ) ;
66
74
if ( cand ) {
67
75
return cand ;
68
76
}
69
-
70
- currentPath = normalize ( path . dirname ( currentPath ) , true ) ;
71
77
}
72
78
73
- const result = _getPackageInfo ( packageName , currentPath ) ;
79
+ logger . warn ( 'No meta data found for shared lib ' + packageName ) ;
80
+ return null ;
81
+ }
82
+
83
+ function getVersionMapCacheKey ( project : string , workspace : string ) : string {
84
+ return `${ project } **${ workspace } ` ;
85
+ }
74
86
75
- if ( ! result ) {
76
- logger . warn ( 'No meta data found for shared lib ' + packageName ) ;
77
- }
87
+ export function getVersionMaps ( project : string , workspace : string ) : VersionMap [ ] {
88
+ return getPackageJsonFiles ( project , workspace )
89
+ . map ( json => ( {
90
+ ...json . content [ 'dependencies' ]
91
+ } ) ) ;
92
+ }
78
93
79
- return result ;
94
+ export function getPackageJsonFiles ( project : string , workspace : string ) : PackageJsonInfo [ ] {
95
+ const cacheKey = getVersionMapCacheKey ( project , workspace ) ;
96
+
97
+ let maps = packageCache [ cacheKey ] ;
98
+
99
+ if ( maps ) {
100
+ return maps ;
101
+ }
80
102
103
+ maps = findPackageJsonFiles ( project , workspace )
104
+ . map ( f => {
105
+ const content = fs . readFileSync ( f , 'utf-8' ) ;
106
+ const directory = path . dirname ( f ) ;
107
+ const result : PackageJsonInfo = {
108
+ content, directory
109
+ } ;
110
+ return result ;
111
+ } ) ;
112
+
113
+ packageCache [ cacheKey ] = maps ;
114
+ return maps ;
81
115
}
82
116
83
117
export function _getPackageInfo (
84
118
packageName : string ,
85
- currentPath : string ,
119
+ packageJsonInfo : PackageJsonInfo ,
86
120
) : PackageInfo | null {
87
121
88
122
const mainPkgName = getPkgFolder ( packageName ) ;
89
123
90
- const mainPkgPath = path . join ( currentPath , 'node_modules' , mainPkgName ) ;
91
- const mainPkgJsonPath = path . join ( mainPkgPath , 'package.json' ) ;
124
+ const mainPkgPath = path . join ( packageJsonInfo . directory , 'node_modules' , mainPkgName ) ;
125
+ // const mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
92
126
93
127
if ( ! fs . existsSync ( mainPkgPath ) ) {
94
128
// TODO: Add logger
@@ -98,7 +132,8 @@ export function _getPackageInfo(
98
132
return null ;
99
133
}
100
134
101
- const mainPkgJson = readJson ( mainPkgJsonPath ) ;
135
+ //const mainPkgJson = readJson(mainPkgJsonPath);
136
+ const mainPkgJson = packageJsonInfo . content ;
102
137
103
138
const version = mainPkgJson [ 'version' ] as string ;
104
139
const esm = mainPkgJson [ 'type' ] === 'module' ;
@@ -171,7 +206,7 @@ export function _getPackageInfo(
171
206
} ;
172
207
}
173
208
174
- const secondaryPgkPath = path . join ( currentPath , 'node_modules' , packageName ) ;
209
+ const secondaryPgkPath = path . join ( packageJsonInfo . directory , 'node_modules' , packageName ) ;
175
210
const secondaryPgkJsonPath = path . join ( secondaryPgkPath , 'package.json' ) ;
176
211
let secondaryPgkJson : PartialPackageJson | null = null ;
177
212
if ( fs . existsSync ( secondaryPgkJsonPath ) ) {
0 commit comments