@@ -161,7 +161,7 @@ function groupDependencyTree(tree, cwd, bindingPrefix) {
161
161
}
162
162
163
163
/**
164
- * Get an import statement path to node package and return the package name
164
+ * return the package name by the import statement path to node package
165
165
*
166
166
* @param {string } packagePath import statement path
167
167
* @returns {string } name of the package
@@ -220,6 +220,10 @@ function findPackagesInPackageJson(packageJson: Record<string, any>, packagesNam
220
220
}
221
221
return { foundPackages : { } , missingPackages : packagesNames } ;
222
222
}
223
+
224
+ type Missing = { [ absolutePath : string ] : string [ ] } ; // e.g. { '/tmp/workspace': ['lodash', 'ramda'] };
225
+ type MissingGroupItem = { originFile : string ; packages ?: string [ ] ; bits ?: string [ ] ; files ?: string [ ] } ;
226
+ type FoundPackages = { [ packageName : string ] : string } ;
223
227
/**
224
228
* Run over each entry in the missing array and transform the missing from list of paths
225
229
* to object with missing types
@@ -230,7 +234,12 @@ function findPackagesInPackageJson(packageJson: Record<string, any>, packagesNam
230
234
* @param {string } bindingPrefix
231
235
* @returns new object with grouped missing
232
236
*/
233
- function groupMissing ( missing , cwd , consumerPath , bindingPrefix ) {
237
+ function groupMissing (
238
+ missing : Missing ,
239
+ cwd ,
240
+ consumerPath ,
241
+ bindingPrefix
242
+ ) : { missingGroups : MissingGroupItem [ ] ; foundPackages : FoundPackages } {
234
243
// temporarily disable this functionality since it cause few bugs: explanation below (on using the packageJson)
235
244
// const packageJson = PackageJson.findPackage(cwd);
236
245
@@ -243,27 +252,26 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
243
252
if ( item . startsWith ( `${ bindingPrefix } /` ) || item . startsWith ( `${ DEFAULT_BINDINGS_PREFIX } /` ) ) return 'bits' ;
244
253
return item . startsWith ( '.' ) ? 'files' : 'packages' ;
245
254
} ) ;
246
- const groups = Object . keys ( missing ) . map ( key =>
255
+ const groups : MissingGroupItem [ ] = Object . keys ( missing ) . map ( key =>
247
256
Object . assign ( { originFile : processPath ( key , { } , cwd ) } , byPathType ( missing [ key ] , bindingPrefix ) )
248
257
) ;
249
- groups . forEach ( group => {
258
+ groups . forEach ( ( group : MissingGroupItem ) => {
250
259
if ( group . packages ) group . packages = group . packages . map ( resolvePackageNameByPath ) ;
260
+ if ( group . bits ) group . bits = group . bits . map ( resolvePackageNameByPath ) ;
251
261
} ) ;
252
262
// This is a hack to solve problems that madge has with packages for type script files
253
263
// It see them as missing even if they are exists
254
264
const foundPackages = { } ;
255
265
// @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
256
266
const packageJson = PackageJson . findPackage ( cwd ) ;
257
-
258
- groups . forEach ( group => {
259
- const missingPackages = [ ] ;
267
+ groups . forEach ( ( group : MissingGroupItem ) => {
268
+ const missingPackages : string [ ] = [ ] ;
260
269
if ( group . packages ) {
261
270
group . packages . forEach ( packageName => {
262
- // Don't add the same package twice
271
+ // Don't try to resolve the same package twice
263
272
if ( R . contains ( packageName , missingPackages ) ) return ;
264
273
const resolvedPath = resolveModulePath ( packageName , cwd , consumerPath ) ;
265
274
if ( ! resolvedPath ) {
266
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
267
275
missingPackages . push ( packageName ) ;
268
276
return ;
269
277
}
@@ -279,6 +287,13 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
279
287
// @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
280
288
groups . packages = result . missingPackages ;
281
289
Object . assign ( foundPackages , result . foundPackages ) ;
290
+
291
+ if ( group . bits ) {
292
+ const foundBits = findPackagesInPackageJson ( packageJson , group . bits ) ;
293
+ // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
294
+ groups . bits = foundBits . missingPackages ;
295
+ Object . assign ( foundPackages , foundBits . foundPackages ) ;
296
+ }
282
297
}
283
298
} ) ;
284
299
@@ -358,16 +373,23 @@ function getResolveConfigAbsolute(
358
373
return resolveConfigAbsolute ;
359
374
}
360
375
361
- function mergeManuallyFoundPackagesToTree ( foundPackages , groups , tree : Tree ) {
376
+ function mergeManuallyFoundPackagesToTree ( foundPackages : FoundPackages , missingGroups : MissingGroupItem [ ] , tree : Tree ) {
362
377
if ( R . isEmpty ( foundPackages ) ) return ;
363
378
// Merge manually found packages (by groupMissing()) with the packages found by Madge (generate-tree-madge)
364
379
Object . keys ( foundPackages ) . forEach ( pkg => {
365
380
// locate package in groups(contains missing)
366
- groups . forEach ( fileDep => {
381
+ missingGroups . forEach ( ( fileDep : MissingGroupItem ) => {
367
382
if ( fileDep . packages && fileDep . packages . includes ( pkg ) ) {
368
383
fileDep . packages = fileDep . packages . filter ( packageName => packageName !== pkg ) ;
369
384
lset ( tree [ fileDep . originFile ] , [ 'packages' , pkg ] , foundPackages [ pkg ] ) ;
370
385
}
386
+ if ( fileDep . bits && fileDep . bits . includes ( pkg ) ) {
387
+ fileDep . bits = fileDep . bits . filter ( packageName => packageName !== pkg ) ;
388
+ if ( ! tree [ fileDep . originFile ] ) tree [ fileDep . originFile ] = { } ;
389
+ if ( ! tree [ fileDep . originFile ] . bits ) tree [ fileDep . originFile ] . bits = [ ] ;
390
+ // @ts -ignore
391
+ tree [ fileDep . originFile ] . bits . push ( pkg ) ;
392
+ }
371
393
} ) ;
372
394
} ) ;
373
395
}
0 commit comments