1
1
import * as fs from 'fs' ;
2
2
import path = require( 'path' ) ;
3
3
4
- import { Config , FileSystemUtils } from '@src' ;
5
-
6
- export class Component {
7
-
8
- constructor ( tsFilename : string , templateFilename : string , selector : string , subComponents : Component [ ] , isRoot : boolean ) {
9
- this . tsFilename = tsFilename ;
10
- this . templateFilename = templateFilename ;
11
- this . selector = selector ;
12
- this . subComponents = subComponents ;
13
- this . isRoot = isRoot ;
14
- }
15
-
16
- public tsFilename : string ;
17
- public templateFilename : string ;
18
- public selector : string ;
19
- public subComponents : Component [ ] ;
20
- public isRoot : boolean ;
21
- }
4
+ import { Config , FileSystemUtils } from '@src' ;
5
+ import { Component } from '@model' ;
22
6
23
7
export class ComponentManager {
8
+ private static componentRegex = / @ C o m p o n e n t \( { / ig;
9
+ private static templateUrlRegex = / .* t e m p l a t e U r l : .+ \/ ( .+ ) ' / i;
10
+ private static selectorRegex = / .* s e l e c t o r : .+ ' ( .+ ) ' / i;
11
+ private static endBracketRegex = / } \) / i;
12
+ private static routerOutletRegex = / < r o u t e r - o u t l e t .* ?> .* ?< \/ r o u t e r - o u t l e t > / ims;
24
13
25
14
public static findComponents ( directoryPath : string ) : { [ selector : string ] : Component ; } {
26
15
const fsUtils = new FileSystemUtils ( ) ;
@@ -37,34 +26,30 @@ export class ComponentManager {
37
26
38
27
private static scanWorkspaceForComponents ( componentFilenames : string [ ] ) : { [ selector : string ] : Component ; } {
39
28
const compHash : { [ selector : string ] : Component ; } = { } ;
40
- const componentRegex = / @ C o m p o n e n t \( { / ig;
41
- const templateUrlRegex = / .* t e m p l a t e U r l : .+ \/ ( .+ ) ' / i;
42
- const selectorRegex = / .* s e l e c t o r : .+ ' ( .+ ) ' / i;
43
- const endBracketRegex = / } \) / i;
44
29
componentFilenames . forEach ( ( componentFilename ) => {
45
30
let componentDefinitionFound = false ;
46
- let currentComponent = new Component ( componentFilename , "" , "" , [ ] , true ) ;
31
+ let currentComponent = new Component ( '' , componentFilename , "" , "" , [ ] , true ) ;
47
32
const content = fs . readFileSync ( componentFilename , 'utf8' ) ;
48
33
const lines : string [ ] = content . split ( '\n' ) ;
49
34
for ( let i : number = 0 ; i < lines . length ; i ++ ) {
50
35
let line = lines [ i ] ;
51
- let match = componentRegex . exec ( line ) ;
36
+ let match = this . componentRegex . exec ( line ) ;
52
37
if ( match ) {
53
38
componentDefinitionFound = true ;
54
39
}
55
40
if ( componentDefinitionFound ) {
56
- match = templateUrlRegex . exec ( line ) ;
41
+ match = this . templateUrlRegex . exec ( line ) ;
57
42
if ( match ) {
58
43
currentComponent . templateFilename = path . join ( path . dirname ( componentFilename ) , match [ 1 ] ) ;
59
44
}
60
- match = selectorRegex . exec ( line ) ;
45
+ match = this . selectorRegex . exec ( line ) ;
61
46
if ( match ) {
62
47
let currentSelector = match [ 1 ] ;
63
48
currentSelector = currentSelector . replace ( "[" , "" ) ;
64
49
currentSelector = currentSelector . replace ( "]" , "" ) ;
65
50
currentComponent . selector = currentSelector ;
66
51
}
67
- match = endBracketRegex . exec ( line ) ;
52
+ match = this . endBracketRegex . exec ( line ) ;
68
53
if ( match ) {
69
54
break ;
70
55
}
@@ -77,27 +62,33 @@ export class ComponentManager {
77
62
78
63
private static enrichComponentsFromComponentTemplates ( componentHash : { [ selector : string ] : Component ; } ) {
79
64
for ( let selector1 in componentHash ) {
80
- if ( fs . existsSync ( componentHash [ selector1 ] . templateFilename ) ) {
81
- const template = fs . readFileSync ( componentHash [ selector1 ] . templateFilename ) ; // We read the entire template file
65
+ const component = componentHash [ selector1 ] ;
66
+ if ( fs . existsSync ( component . templateFilename ) ) {
67
+ const template = fs . readFileSync ( component . templateFilename ) ; // We read the entire template file
68
+ component . isRouterOutlet = this . isComponentRouterOutlet ( template ) ;
82
69
for ( let selector2 in componentHash ) { // then we check if the template contains each of the selectors we found in the components
83
70
let pattern = `</${ selector2 } >` ;
84
71
let index = template . indexOf ( pattern ) ;
85
72
if ( index >= 0 ) {
86
- componentHash [ selector1 ] . subComponents = componentHash [ selector1 ] . subComponents . concat ( componentHash [ selector2 ] ) ;
73
+ component . subComponents = component . subComponents . concat ( componentHash [ selector2 ] ) ;
87
74
// If selector2 has been found in a template then it is not root
88
75
componentHash [ selector2 ] . isRoot = false ;
89
76
}
90
77
else {
91
78
pattern = ` ${ selector2 } ` ;
92
79
index = template . indexOf ( pattern ) ;
93
80
if ( index >= 0 ) {
94
- componentHash [ selector1 ] . subComponents = componentHash [ selector1 ] . subComponents . concat ( componentHash [ selector2 ] ) ;
81
+ component . subComponents = component . subComponents . concat ( componentHash [ selector2 ] ) ;
95
82
// If selector2 has been found in a template then it is not root
96
83
componentHash [ selector2 ] . isRoot = false ;
97
84
}
98
85
}
99
86
}
100
87
}
101
88
}
102
- }
89
+ }
90
+ private static isComponentRouterOutlet ( template : Buffer ) : boolean {
91
+ const match = this . routerOutletRegex . exec ( template . toString ( ) ) ;
92
+ return match !== null ;
93
+ }
103
94
}
0 commit comments