@@ -7,6 +7,7 @@ import R from 'ramda';
7
7
import generateTree from './generate-tree-madge' ;
8
8
import PackageJson from '../package-json/package-json' ;
9
9
import partition from 'lodash.partition' ;
10
+ import lset from 'lodash.set' ;
10
11
import { DEFAULT_BINDINGS_PREFIX } from '../constants' ;
11
12
12
13
/**
@@ -233,34 +234,42 @@ function groupMissing(missing, cwd, consumerPath, bindingPrefix) {
233
234
if ( item . startsWith ( `${ bindingPrefix } /` ) || item . startsWith ( `${ DEFAULT_BINDINGS_PREFIX } /` ) ) return 'bits' ;
234
235
return item . startsWith ( '.' ) ? 'files' : 'packages' ;
235
236
} ) ;
236
- const groups = byPathType ( missing , bindingPrefix ) ;
237
- const packages = groups . packages ? groups . packages . map ( resolvePackageNameByPath ) : [ ] ;
237
+ const groups = Object . keys ( missing ) . map ( key => Object . assign ( { originFile : path . relative ( cwd , key ) } , byPathType ( missing [ key ] , bindingPrefix ) ) ) ;
238
+ groups . forEach ( ( group ) => {
239
+ if ( group . packages ) group . packages = group . packages . map ( resolvePackageNameByPath ) ;
240
+ } ) ;
238
241
// This is a hack to solve problems that madge has with packages for type script files
239
242
// It see them as missing even if they are exists
240
243
const foundPackages = { } ;
241
- const missingPackages = [ ] ;
242
- packages . forEach ( ( packageName ) => {
243
- // Don't add the same package twice
244
- if ( R . contains ( packageName , missingPackages ) ) return ;
245
- const resolvedPath = resolveModulePath ( packageName , cwd , consumerPath ) ;
246
- if ( ! resolvedPath ) {
247
- return missingPackages . push ( packageName ) ;
248
- }
249
- const packageWithVersion = resolveNodePackage ( cwd , resolvedPath ) ;
244
+ const packageJson = PackageJson . findPackage ( cwd ) ;
245
+
246
+ groups . forEach ( ( group ) => {
247
+ const missingPackages = [ ] ;
248
+ if ( group . packages ) {
249
+ group . packages . forEach ( ( packageName ) => {
250
+ // Don't add the same package twice
251
+ if ( R . contains ( packageName , missingPackages ) ) return ;
252
+ const resolvedPath = resolveModulePath ( packageName , cwd , consumerPath ) ;
253
+ if ( ! resolvedPath ) {
254
+ return missingPackages . push ( packageName ) ;
255
+ }
256
+ const packageWithVersion = resolveNodePackage ( cwd , resolvedPath ) ;
250
257
251
- return packageWithVersion ? Object . assign ( foundPackages , packageWithVersion ) :
252
- missingPackages . push ( packageWithVersion ) ;
258
+ return packageWithVersion ? Object . assign ( foundPackages , packageWithVersion ) :
259
+ missingPackages . push ( packageWithVersion ) ;
260
+ } ) ;
261
+ }
262
+ if ( packageJson ) {
263
+ const result = findPackagesInPackageJson ( packageJson , missingPackages ) ;
264
+ groups . packages = result . missingPackages ;
265
+ Object . assign ( foundPackages , result . foundPackages ) ;
266
+ }
253
267
} ) ;
254
- groups . packages = missingPackages ;
255
268
256
269
// temporarily disable this functionality since it cause this bugs:
257
270
// https://github.com/teambit/bit/issues/635
258
271
// https://github.com/teambit/bit/issues/690
259
- // if (packageJson) {
260
- // const result = findPackagesInPackageJson(packageJson, missingPackages);
261
- // groups.packages = result.missingPackages;
262
- // Object.assign(foundPackages, result.foundPackages)
263
- // }
272
+
264
273
265
274
return { groups, foundPackages } ;
266
275
}
@@ -362,23 +371,23 @@ function updateTreeWithLinkFilesAndImportSpecifiers(tree: Tree, pathMap: PathMap
362
371
* @param bindingPrefix
363
372
* @return {Promise<{missing, tree}> }
364
373
*/
365
- export default async function getDependencyTree ( baseDir : string , consumerPath : string , filePaths : string [ ] , bindingPrefix : string ) :
366
- Promise < { missing: Object , tree : Tree } > {
374
+ export default async function getDependencyTree ( baseDir : string , consumerPath : string , filePaths : string [ ] , bindingPrefix : string ) : Promise < { missing: Object , tree : Tree } > {
367
375
const config = { baseDir, includeNpm : true , requireConfig : null , webpackConfig : null , visited : { } , nonExistent : [ ] } ;
368
376
const result = generateTree ( filePaths , config ) ;
369
377
const { groups, foundPackages } = groupMissing ( result . skipped , baseDir , consumerPath , bindingPrefix ) ;
370
378
const tree : Tree = groupDependencyTree ( result . tree , baseDir , bindingPrefix ) ;
371
- const relativeFilePaths = filePaths . map ( filePath => path . relative ( baseDir , filePath ) ) ;
379
+ // const relativeFilePaths = filePaths.map(filePath => path.relative(baseDir, filePath));
372
380
// Merge manually found packages with madge founded packages
373
381
if ( foundPackages && ! R . isEmpty ( foundPackages ) ) {
374
382
// Madge found packages so we need to merge them with the manual
375
- relativeFilePaths . forEach ( ( relativeFilePath ) => {
376
- if ( tree [ relativeFilePath ] . packages ) {
377
- Object . assign ( tree [ relativeFilePath ] . packages , foundPackages ) ;
378
- // There is only manually found packages
379
- } else {
380
- tree [ relativeFilePath ] . packages = foundPackages ;
381
- }
383
+ Object . keys ( foundPackages ) . forEach ( ( pkg ) => {
384
+ // locate package in groups(contains missing)
385
+ groups . forEach ( ( fileDep ) => {
386
+ if ( fileDep . packages && fileDep . packages . includes ( pkg ) ) {
387
+ fileDep . packages = fileDep . packages . filter ( packageName => packageName !== pkg ) ;
388
+ lset ( tree [ fileDep [ 'originFile' ] ] , `packages.${ pkg } ` , foundPackages [ pkg ] ) ;
389
+ }
390
+ } ) ;
382
391
} ) ;
383
392
}
384
393
0 commit comments