@@ -3,25 +3,10 @@ import { Base64 } from 'js-base64';
3
3
import * as path from 'path' ;
4
4
import * as vscode from 'vscode' ;
5
5
6
+ import { Component , ComponentManager } from '../componentManager' ;
6
7
import { Config } from '../config' ;
7
8
import { FileSystemUtils } from '../filesystemUtils' ;
8
9
9
- class Component {
10
-
11
- constructor ( tsFilename : string , templateFilename : string , selector : string , subComponents : Component [ ] , isRoot : boolean ) {
12
- this . tsFilename = tsFilename ;
13
- this . templateFilename = templateFilename ;
14
- this . selector = selector ;
15
- this . subComponents = subComponents ;
16
- this . isRoot = isRoot ;
17
- }
18
- public tsFilename : string ;
19
- public templateFilename : string ;
20
- public selector : string ;
21
- public subComponents : Component [ ] ;
22
- public isRoot : boolean ;
23
- }
24
-
25
10
class Node {
26
11
constructor ( id : string , tsFilename : string , isRoot : boolean ) {
27
12
this . id = id ;
@@ -36,6 +21,7 @@ class Node {
36
21
return `{id: "${ this . id } ", label: "${ this . id } "}` ;
37
22
}
38
23
}
24
+
39
25
class Edge {
40
26
constructor ( id : string , source : string , target : string ) {
41
27
this . id = id ;
@@ -76,9 +62,7 @@ export class ShowComponentHierarchy {
76
62
) ;
77
63
78
64
var directoryPath : string = this . fsUtils . getWorkspaceFolder ( ) ;
79
- const componentFilenames = this . fsUtils . listFiles ( directoryPath , Config . excludeDirectories , this . isComponentFile ) ;
80
- const components = this . findComponents ( componentFilenames ) ;
81
- this . enrichComponentsFromComponentTemplates ( components ) ;
65
+ const components = ComponentManager . findComponents ( directoryPath ) ;
82
66
83
67
let nodes : Node [ ] = [ ] ;
84
68
const appendNodes = ( nodeList : Node [ ] ) => {
@@ -89,14 +73,14 @@ export class ShowComponentHierarchy {
89
73
} ) ;
90
74
} ;
91
75
let edges : Edge [ ] = [ ] ;
92
- const appendLinks = ( edgeList : Edge [ ] ) => {
76
+ const appendEdges = ( edgeList : Edge [ ] ) => {
93
77
edgeList . forEach ( newEdge => {
94
78
if ( ! edges . some ( edge => edge . source === newEdge . source && edge . target === newEdge . target ) ) {
95
79
edges = edges . concat ( newEdge ) ;
96
80
}
97
81
} ) ;
98
82
} ;
99
- this . addNodesAndLinks ( components , appendNodes , appendLinks ) ;
83
+ this . addNodesAndEdges ( components , appendNodes , appendEdges ) ;
100
84
101
85
const nodesJson = nodes
102
86
. map ( ( node , index , arr ) => { return node . toJsonString ( ) ; } )
@@ -127,77 +111,7 @@ export class ShowComponentHierarchy {
127
111
}
128
112
}
129
113
130
- private isComponentFile ( filename : string ) : boolean {
131
- return filename . endsWith ( '.component.ts' ) ;
132
- }
133
-
134
- private findComponents ( componentFilenames : string [ ] ) {
135
- const compHash : { [ selector : string ] : Component ; } = { } ;
136
- const componentRegex = / @ C o m p o n e n t \( { / ig;
137
- const templateUrlRegex = / .* t e m p l a t e U r l : .+ \/ ( .+ ) \' / i;
138
- const selectorRegex = / .* s e l e c t o r : .+ \' ( .+ ) \' / i;
139
- const endBracketRegex = / } \) / i;
140
- componentFilenames . forEach ( ( componentFilename ) => {
141
- let componentDefinitionFound = false ;
142
- let currentComponent = new Component ( componentFilename , "" , "" , [ ] , true ) ;
143
- const content = fs . readFileSync ( componentFilename , 'utf8' ) ;
144
- const lines : string [ ] = content . split ( '\n' ) ;
145
- for ( let i : number = 0 ; i < lines . length ; i ++ ) {
146
- let line = lines [ i ] ;
147
- let match = componentRegex . exec ( line ) ;
148
- if ( match ) {
149
- componentDefinitionFound = true ;
150
- }
151
- if ( componentDefinitionFound ) {
152
- match = templateUrlRegex . exec ( line ) ;
153
- if ( match ) {
154
- currentComponent . templateFilename = path . join ( path . dirname ( componentFilename ) , match [ 1 ] ) ;
155
- }
156
- match = selectorRegex . exec ( line ) ;
157
- if ( match ) {
158
- let currentSelector = match [ 1 ] ;
159
- currentSelector = currentSelector . replace ( "[" , "" ) ;
160
- currentSelector = currentSelector . replace ( "]" , "" ) ;
161
- currentComponent . selector = currentSelector ;
162
- }
163
- match = endBracketRegex . exec ( line ) ;
164
- if ( match ) {
165
- break ;
166
- }
167
- }
168
- }
169
- compHash [ currentComponent . selector ] = currentComponent ;
170
- } ) ;
171
- return compHash ;
172
- }
173
-
174
- private enrichComponentsFromComponentTemplates ( componentHash : { [ selector : string ] : Component ; } ) {
175
- for ( let selector1 in componentHash ) {
176
- if ( fs . existsSync ( componentHash [ selector1 ] . templateFilename ) ) {
177
- const template = fs . readFileSync ( componentHash [ selector1 ] . templateFilename ) ; // We read the entire template file
178
- for ( let selector2 in componentHash ) { // then we check if the template contains each of the selectors we found in the components
179
- let pattern = `</${ selector2 } >` ;
180
- let index = template . indexOf ( pattern ) ;
181
- if ( index >= 0 ) {
182
- componentHash [ selector1 ] . subComponents = componentHash [ selector1 ] . subComponents . concat ( componentHash [ selector2 ] ) ;
183
- // If selector2 has been found in a template then it is not root
184
- componentHash [ selector2 ] . isRoot = false ;
185
- }
186
- else {
187
- pattern = ` ${ selector2 } ` ;
188
- index = template . indexOf ( pattern ) ;
189
- if ( index >= 0 ) {
190
- componentHash [ selector1 ] . subComponents = componentHash [ selector1 ] . subComponents . concat ( componentHash [ selector2 ] ) ;
191
- // If selector2 has been found in a template then it is not root
192
- componentHash [ selector2 ] . isRoot = false ;
193
- }
194
- }
195
- }
196
- }
197
- }
198
- }
199
-
200
- private addNodesAndLinks ( componentHash : { [ selector : string ] : Component ; } , appendNodes : ( nodeList : Node [ ] ) => void , appendLinks : ( edgeList : Edge [ ] ) => void ) {
114
+ private addNodesAndEdges ( componentHash : { [ selector : string ] : Component ; } , appendNodes : ( nodeList : Node [ ] ) => void , appendLinks : ( edgeList : Edge [ ] ) => void ) {
201
115
for ( let selector in componentHash ) {
202
116
const component = componentHash [ selector ] ;
203
117
if ( component . isRoot ) {
@@ -284,7 +198,7 @@ export class ShowComponentHierarchy {
284
198
const newFilePath = path . join ( workspaceDirectory , Config . componentHierarchyFilename ) ;
285
199
this . fsUtils . writeFile ( newFilePath , u8arr , ( ) => { } ) ;
286
200
287
- vscode . window . showInformationMessage ( ' The file ComponentHierarchy.png has been created in the root of the workspace.' ) ;
201
+ vscode . window . showInformationMessage ( ` The file ${ Config . componentHierarchyFilename } has been created in the root of the workspace.` ) ;
288
202
}
289
203
}
290
204
}
0 commit comments