@@ -2,6 +2,7 @@ import * as path from "path";
2
2
import { NODE_MODULES_FOLDER_NAME , PACKAGE_JSON_FILE_NAME } from "../../constants" ;
3
3
4
4
interface IDependencyDescription {
5
+ parent : IDependencyDescription ;
5
6
parentDir : string ;
6
7
name : string ;
7
8
depth : number ;
@@ -20,20 +21,22 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
20
21
21
22
const queue : IDependencyDescription [ ] = _ . keys ( dependencies )
22
23
. map ( dependencyName => ( {
24
+ parent : null ,
23
25
parentDir : projectPath ,
24
26
name : dependencyName ,
25
27
depth : 0
26
28
} ) ) ;
27
29
28
30
while ( queue . length ) {
29
31
const currentModule = queue . shift ( ) ;
30
- const resolvedDependency = this . findModule ( rootNodeModulesPath , currentModule . parentDir , currentModule . name , currentModule . depth , resolvedDependencies ) ;
32
+ const resolvedDependency = this . findModule ( rootNodeModulesPath , currentModule , resolvedDependencies ) ;
31
33
32
34
if ( resolvedDependency && ! _ . some ( resolvedDependencies , r => r . directory === resolvedDependency . directory ) ) {
33
35
_ . each ( resolvedDependency . dependencies , d => {
34
- const dependency : IDependencyDescription = { name : d , parentDir : resolvedDependency . directory , depth : resolvedDependency . depth + 1 } ;
36
+ const dependency : IDependencyDescription = { parent : currentModule , name : d , parentDir : resolvedDependency . directory , depth : resolvedDependency . depth + 1 } ;
35
37
36
38
const shouldAdd = ! _ . some ( queue , element =>
39
+ element . parent == dependency . parent &&
37
40
element . name === dependency . name &&
38
41
element . parentDir === dependency . parentDir &&
39
42
element . depth === dependency . depth ) ;
@@ -43,36 +46,47 @@ export class NodeModulesDependenciesBuilder implements INodeModulesDependenciesB
43
46
}
44
47
} ) ;
45
48
46
- _ . each ( this . getProductionDependencies ( resolvedDependency . directory ) , d => {
47
- resolvedDependencies . push ( d ) ;
48
- } ) ;
49
49
resolvedDependencies . push ( resolvedDependency ) ;
50
50
}
51
51
}
52
52
53
53
return resolvedDependencies ;
54
54
}
55
55
56
- private findModule ( rootNodeModulesPath : string , parentModulePath : string , name : string , depth : number , resolvedDependencies : IDependencyData [ ] ) : IDependencyData {
57
- let modulePath = path . join ( parentModulePath , NODE_MODULES_FOLDER_NAME , name ) ; // node_modules/parent/node_modules/<package>
58
- const rootModulesPath = path . join ( rootNodeModulesPath , name ) ;
59
- let depthInNodeModules = depth ;
56
+ private findModule ( rootNodeModulesPath : string , depDescription : IDependencyDescription , resolvedDependencies : IDependencyData [ ] ) : IDependencyData {
57
+ let modulePath = path . join ( depDescription . parentDir , NODE_MODULES_FOLDER_NAME , depDescription . name ) ; // node_modules/parent/node_modules/<package>
58
+ const rootModulesPath = path . join ( rootNodeModulesPath , depDescription . name ) ;
59
+ let depthInNodeModules = depDescription . depth ;
60
60
61
61
if ( ! this . moduleExists ( modulePath ) ) {
62
- modulePath = rootModulesPath ; // /node_modules/<package>
63
- if ( ! this . moduleExists ( modulePath ) ) {
64
- return null ;
62
+
63
+ let moduleExists = false ;
64
+ let parent = depDescription . parent ;
65
+
66
+ while ( parent && ! moduleExists ) {
67
+ modulePath = path . join ( depDescription . parent . parentDir , NODE_MODULES_FOLDER_NAME , depDescription . name ) ;
68
+ moduleExists = this . moduleExists ( modulePath ) ;
69
+ if ( ! moduleExists ) {
70
+ parent = parent . parent ;
71
+ }
65
72
}
66
73
74
+ if ( ! moduleExists ) {
75
+ modulePath = rootModulesPath ; // /node_modules/<package>
76
+ if ( ! this . moduleExists ( modulePath ) ) {
77
+ return null ;
78
+ }
79
+ }
80
+
67
81
depthInNodeModules = 0 ;
68
82
}
69
83
70
- if ( _ . some ( resolvedDependencies , r => r . name === name && r . directory === modulePath ) ) {
84
+ if ( _ . some ( resolvedDependencies , r => r . name === depDescription . name && r . directory === modulePath ) ) {
71
85
return null ;
72
86
73
87
}
74
88
75
- return this . getDependencyData ( name , modulePath , depthInNodeModules ) ;
89
+ return this . getDependencyData ( depDescription . name , modulePath , depthInNodeModules ) ;
76
90
}
77
91
78
92
private getDependencyData ( name : string , directory : string , depth : number ) : IDependencyData {
0 commit comments