@@ -107,24 +107,19 @@ class Tree {
107
107
* @return {Object }
108
108
*/
109
109
generateTree ( files ) {
110
- const depTree = { } ;
111
- const visited = { } ;
110
+ const modules = { } ;
112
111
const nonExistent = [ ] ;
113
112
const npmPaths = { } ;
114
113
const pathCache = { } ;
115
114
116
115
files . forEach ( ( file ) => {
117
- if ( visited [ file ] ) {
118
- return ;
119
- }
120
-
121
- Object . assign ( depTree , dependencyTree ( {
116
+ dependencyTree ( {
122
117
filename : file ,
123
118
directory : this . baseDir ,
124
119
requireConfig : this . config . requireConfig ,
125
120
webpackConfig : this . config . webpackConfig ,
126
121
tsConfig : this . config . tsConfig ,
127
- visited : visited ,
122
+ visited : modules ,
128
123
filter : ( dependencyFilePath , traversedFilePath ) => {
129
124
let dependencyFilterRes = true ;
130
125
const isNpmPath = this . isNpmPath ( dependencyFilePath ) ;
@@ -145,10 +140,10 @@ class Tree {
145
140
} ,
146
141
detective : this . config . detectiveOptions ,
147
142
nonExistent : nonExistent
148
- } ) ) ;
143
+ } ) ;
149
144
} ) ;
150
145
151
- let tree = this . convertTree ( depTree , { } , pathCache , npmPaths ) ;
146
+ let tree = this . convertTree ( modules , files , this . config . depth ) ;
152
147
153
148
for ( const npmKey in npmPaths ) {
154
149
const id = this . processPath ( npmKey , pathCache ) ;
@@ -171,27 +166,57 @@ class Tree {
171
166
/**
172
167
* Convert deep tree produced by dependency-tree to a
173
168
* shallow (one level deep) tree used by madge.
174
- * @param {Object } depTree
169
+ * @param {Object } modules
175
170
* @param {Object } tree
176
- * @param {Object } pathCache
171
+ * @param {number? } depthLimit
177
172
* @return {Object }
178
173
*/
179
- convertTree ( depTree , tree , pathCache ) {
180
- for ( const key in depTree ) {
181
- const id = this . processPath ( key , pathCache ) ;
182
-
183
- if ( ! tree [ id ] ) {
184
- tree [ id ] = [ ] ;
185
-
186
- for ( const dep in depTree [ key ] ) {
187
- tree [ id ] . push ( this . processPath ( dep , pathCache ) ) ;
174
+ convertTree ( modules , tree , depthLimit ) {
175
+ const self = this ;
176
+ const depths = { } ;
177
+ const deepDependencies = { } ;
178
+
179
+ function calculateDepths ( tree , depth ) {
180
+ if ( depth <= depthLimit ) {
181
+ for ( let i = 0 ; i < tree . length ; i ++ ) {
182
+ const dependency = tree [ i ] ;
183
+ depths [ dependency ] = true ;
184
+ calculateDepths ( Object . keys ( modules [ dependency ] ) , depth + 1 ) ;
188
185
}
186
+ }
187
+ }
188
+
189
+ function getDeepDependencies ( dependency ) {
190
+ if ( deepDependencies [ dependency ] === null ) {
191
+ return [ ] ;
192
+ }
189
193
190
- this . convertTree ( depTree [ key ] , tree , pathCache ) ;
194
+ if ( ! ( dependency in deepDependencies ) ) {
195
+ deepDependencies [ dependency ] = null ;
196
+ deepDependencies [ dependency ] = [ ...new Set ( Object . keys ( modules [ dependency ] ) . flatMap (
197
+ ( dependency ) => dependency in depths ? [ dependency ] : getDeepDependencies ( dependency )
198
+ ) ) ] ;
191
199
}
200
+
201
+ return deepDependencies [ dependency ] ;
202
+ }
203
+
204
+ const pathCache = { } ;
205
+ const result = { } ;
206
+
207
+ if ( ! Number . isInteger ( depthLimit ) ) {
208
+ Object . entries ( modules ) . forEach ( ( [ module , dependencies ] ) => {
209
+ result [ self . processPath ( module , pathCache ) ] = Object . keys ( dependencies ) . map ( ( dependency ) => self . processPath ( dependency , pathCache ) ) ;
210
+ } ) ;
211
+ } else {
212
+ calculateDepths ( tree , 0 ) ;
213
+
214
+ Object . keys ( depths ) . forEach ( ( module ) => {
215
+ result [ self . processPath ( module , pathCache ) ] = getDeepDependencies ( module ) . map ( ( dependency ) => self . processPath ( dependency , pathCache ) ) ;
216
+ } ) ;
192
217
}
193
218
194
- return tree ;
219
+ return result ;
195
220
}
196
221
197
222
/**
0 commit comments