@@ -20,14 +20,17 @@ export type VersionMap = Record<string, string>;
20
20
export type PackageJsonInfo = {
21
21
content : any ;
22
22
directory : string ;
23
- }
23
+ } ;
24
24
25
25
const packageCache : Record < string , PackageJsonInfo [ ] > = { } ;
26
26
27
- export function findPackageJsonFiles ( project : string , workspace : string ) : string [ ] {
27
+ export function findPackageJsonFiles (
28
+ project : string ,
29
+ workspace : string
30
+ ) : string [ ] {
28
31
return expandFolders ( project , workspace )
29
- . map ( f => path . join ( f , 'package.json' ) )
30
- . filter ( f => fs . existsSync ( f ) ) ;
32
+ . map ( ( f ) => path . join ( f , 'package.json' ) )
33
+ . filter ( ( f ) => fs . existsSync ( f ) ) ;
31
34
}
32
35
33
36
export function expandFolders ( child : string , parent : string ) : string [ ] {
@@ -36,30 +39,30 @@ export function expandFolders(child: string, parent: string): string[] {
36
39
child = normalize ( child , true ) ;
37
40
38
41
if ( ! child . startsWith ( parent ) ) {
39
- throw new Error ( `Workspace folder ${ path } needs to be a parent of the project folder ${ child } ` ) ;
42
+ throw new Error (
43
+ `Workspace folder ${ path } needs to be a parent of the project folder ${ child } `
44
+ ) ;
40
45
}
41
46
42
47
let current = child ;
43
48
44
49
while ( current !== parent ) {
45
50
result . push ( current ) ;
46
-
51
+
47
52
const cand = normalize ( path . dirname ( current ) , true ) ;
48
53
if ( cand === current ) {
49
- break
50
- } ;
54
+ break ;
55
+ }
51
56
current = cand ;
52
57
}
53
58
result . push ( parent ) ;
54
59
return result ;
55
60
}
56
61
57
-
58
62
export function getPackageInfo (
59
63
packageName : string ,
60
64
workspaceRoot : string
61
65
) : PackageInfo | null {
62
-
63
66
workspaceRoot = normalize ( workspaceRoot , true ) ;
64
67
65
68
const packageJsonInfos = getPackageJsonFiles ( workspaceRoot , workspaceRoot ) ;
@@ -79,14 +82,19 @@ function getVersionMapCacheKey(project: string, workspace: string): string {
79
82
return `${ project } **${ workspace } ` ;
80
83
}
81
84
82
- export function getVersionMaps ( project : string , workspace : string ) : VersionMap [ ] {
83
- return getPackageJsonFiles ( project , workspace )
84
- . map ( json => ( {
85
- ...json . content [ 'dependencies' ]
86
- } ) ) ;
85
+ export function getVersionMaps (
86
+ project : string ,
87
+ workspace : string
88
+ ) : VersionMap [ ] {
89
+ return getPackageJsonFiles ( project , workspace ) . map ( ( json ) => ( {
90
+ ...json . content [ 'dependencies' ] ,
91
+ } ) ) ;
87
92
}
88
93
89
- export function getPackageJsonFiles ( project : string , workspace : string ) : PackageJsonInfo [ ] {
94
+ export function getPackageJsonFiles (
95
+ project : string ,
96
+ workspace : string
97
+ ) : PackageJsonInfo [ ] {
90
98
const cacheKey = getVersionMapCacheKey ( project , workspace ) ;
91
99
92
100
let maps = packageCache [ cacheKey ] ;
@@ -95,21 +103,24 @@ export function getPackageJsonFiles(project: string, workspace: string): Package
95
103
return maps ;
96
104
}
97
105
98
- maps = findPackageJsonFiles ( project , workspace )
99
- . map ( f => {
100
- const content = JSON . parse ( fs . readFileSync ( f , 'utf-8' ) ) ;
101
- const directory = normalize ( path . dirname ( f ) , true ) ;
102
- const result : PackageJsonInfo = {
103
- content , directory
104
- } ;
105
- return result ;
106
- } ) ;
106
+ maps = findPackageJsonFiles ( project , workspace ) . map ( ( f ) => {
107
+ const content = JSON . parse ( fs . readFileSync ( f , 'utf-8' ) ) ;
108
+ const directory = normalize ( path . dirname ( f ) , true ) ;
109
+ const result : PackageJsonInfo = {
110
+ content ,
111
+ directory,
112
+ } ;
113
+ return result ;
114
+ } ) ;
107
115
108
116
packageCache [ cacheKey ] = maps ;
109
117
return maps ;
110
118
}
111
119
112
- export function findDepPackageJson ( packageName : string , projectRoot : string ) : string | null {
120
+ export function findDepPackageJson (
121
+ packageName : string ,
122
+ projectRoot : string
123
+ ) : string | null {
113
124
const mainPkgName = getPkgFolder ( packageName ) ;
114
125
115
126
let mainPkgPath = path . join ( projectRoot , 'node_modules' , mainPkgName ) ;
@@ -118,7 +129,6 @@ export function findDepPackageJson(packageName: string, projectRoot: string): st
118
129
let directory = projectRoot ;
119
130
120
131
while ( path . dirname ( directory ) !== directory ) {
121
-
122
132
if ( fs . existsSync ( mainPkgJsonPath ) ) {
123
133
break ;
124
134
}
@@ -132,19 +142,19 @@ export function findDepPackageJson(packageName: string, projectRoot: string): st
132
142
if ( ! fs . existsSync ( mainPkgJsonPath ) ) {
133
143
// TODO: Add logger
134
144
// context.logger.warn('No package.json found for ' + packageName);
135
- logger . verbose ( 'No package.json found for ' + packageName + ' in ' + mainPkgPath ) ;
145
+ logger . verbose (
146
+ 'No package.json found for ' + packageName + ' in ' + mainPkgPath
147
+ ) ;
136
148
137
149
return null ;
138
150
}
139
151
return mainPkgJsonPath ;
140
-
141
152
}
142
153
143
154
export function _getPackageInfo (
144
155
packageName : string ,
145
- directory : string ,
156
+ directory : string
146
157
) : PackageInfo | null {
147
-
148
158
const mainPkgName = getPkgFolder ( packageName ) ;
149
159
const mainPkgJsonPath = findDepPackageJson ( packageName , directory ) ;
150
160
@@ -177,13 +187,13 @@ export function _getPackageInfo(
177
187
178
188
if ( typeof cand === 'string' ) {
179
189
return {
180
- entryPoint : path . join ( mainPkgPath , cand ) ,
181
- packageName,
182
- version,
183
- esm,
190
+ entryPoint : path . join ( mainPkgPath , cand ) ,
191
+ packageName,
192
+ version,
193
+ esm,
184
194
} ;
185
195
}
186
-
196
+
187
197
cand = mainPkgJson ?. exports ?. [ relSecondaryPath ] ?. import ;
188
198
189
199
if ( typeof cand === 'object' ) {
@@ -197,6 +207,16 @@ export function _getPackageInfo(
197
207
}
198
208
199
209
if ( cand ) {
210
+ if ( typeof cand === 'object' ) {
211
+ if ( cand . module ) {
212
+ cand = cand . module ;
213
+ } else if ( cand . default ) {
214
+ cand = cand . default ;
215
+ } else {
216
+ cand = null ;
217
+ }
218
+ }
219
+
200
220
return {
201
221
entryPoint : path . join ( mainPkgPath , cand ) ,
202
222
packageName,
@@ -228,7 +248,6 @@ export function _getPackageInfo(
228
248
229
249
cand = mainPkgJson ?. exports ?. [ relSecondaryPath ] ?. default ;
230
250
if ( cand ) {
231
-
232
251
if ( typeof cand === 'object' ) {
233
252
if ( cand . module ) {
234
253
cand = cand . module ;
@@ -238,7 +257,7 @@ export function _getPackageInfo(
238
257
cand = null ;
239
258
}
240
259
}
241
-
260
+
242
261
return {
243
262
entryPoint : path . join ( mainPkgPath , cand ) ,
244
263
packageName,
0 commit comments