1
1
import * as fs from 'fs' ;
2
2
import { ArrayUtils , Config , FileSystemUtils } from "@src" ;
3
3
4
- export interface INgModule {
5
- imports : string [ ] ;
6
- exports : string [ ] ;
7
- declarations : string [ ] ;
8
- entryComponents : string [ ] ;
9
- providers : string [ ] ;
10
- bootstrap : string [ ] ;
11
- }
12
-
13
- export class NgModule implements INgModule {
4
+ export class NgModule {
14
5
public imports : string [ ] = [ ] ;
15
6
public exports : string [ ] = [ ] ;
16
7
public declarations : string [ ] = [ ] ;
@@ -19,7 +10,6 @@ export class NgModule implements INgModule {
19
10
public bootstrap : string [ ] = [ ] ;
20
11
public filename : string = '' ;
21
12
public moduleName : string = '' ;
22
- public moduleContents : string = '' ;
23
13
public moduleStats ( ) : number [ ] {
24
14
return [
25
15
this . declarations === undefined ? 0 : ArrayUtils . arrayLength ( this . declarations ) ,
@@ -32,25 +22,50 @@ export class NgModule implements INgModule {
32
22
}
33
23
}
34
24
25
+ export class NamedEntity {
26
+ public name : string = '' ;
27
+ public constructor ( name : string ) {
28
+ this . name = name ;
29
+ }
30
+ }
31
+ class Directive extends NamedEntity { }
32
+ class Pipe extends NamedEntity { }
33
+ class Component extends NamedEntity { }
34
+ export class Project {
35
+ public modules : NgModule [ ] = [ ] ;
36
+ public components : string [ ] = [ ] ;
37
+ public pipes : string [ ] = [ ] ;
38
+ public directives : string [ ] = [ ] ;
39
+ }
40
+
35
41
export class ModuleManager {
36
42
37
- public static findModules ( directoryPath : string , errors : string [ ] , isTypescriptFile : ( filename : string ) => boolean ) : NgModule [ ] {
43
+ public static scanProject ( directoryPath : string , errors : string [ ] , isTypescriptFile : ( filename : string ) => boolean ) : Project {
38
44
const fsUtils = new FileSystemUtils ( ) ;
39
45
const config = new Config ( ) ;
40
46
const moduleFilenames = fsUtils . listFiles ( directoryPath , config . excludeDirectories , isTypescriptFile ) ;
41
- const modules : NgModule [ ] = [ ] ;
47
+ const project = new Project ( ) ;
42
48
moduleFilenames . sort ( ArrayUtils . sortStrings ) . forEach ( filename => {
43
- const module = this . readModule ( filename , errors ) ;
44
- if ( module !== undefined ) {
45
- modules . push ( module ) ;
49
+ const file = this . readTypescriptFile ( filename , errors ) ;
50
+ if ( file instanceof NgModule ) {
51
+ project . modules . push ( file as NgModule ) ;
52
+ }
53
+ else if ( file instanceof Component ) {
54
+ project . components . push ( file . name ) ;
55
+ }
56
+ else if ( file instanceof Pipe ) {
57
+ project . pipes . push ( file . name ) ;
58
+ }
59
+ else if ( file instanceof Directive ) {
60
+ project . directives . push ( file . name ) ;
46
61
}
47
62
} ) ;
48
- return modules ;
63
+ return project ;
49
64
}
50
65
51
- private static readModule ( filename : string , errors : string [ ] ) : NgModule | undefined {
66
+ private static readTypescriptFile ( filename : string , errors : string [ ] ) : NgModule | Component | Directive | Pipe | undefined {
52
67
const fileContents = fs . readFileSync ( filename ) ;
53
- const regex : RegExp = / @ N g M o d u l e \s * \( \s * ( \{ .+ ?\} ) \s * \) \s * e x p o r t \s + c l a s s \s + ( \w + ) \s + / ims;
68
+ let regex : RegExp = / @ N g M o d u l e \s * \( \s * ( \{ .+ ?\} ) \s * \) \s * e x p o r t \s + c l a s s \s + ( \w + ) \s + / ims;
54
69
var match = regex . exec ( fileContents . toString ( ) ) ;
55
70
if ( match !== null ) {
56
71
const moduleName = match [ 2 ] ;
@@ -59,13 +74,27 @@ export class ModuleManager {
59
74
const module : NgModule = this . parseModuleContents ( moduleContents ) ;
60
75
module . filename = filename ;
61
76
module . moduleName = moduleName ;
62
- module . moduleContents = moduleContents ;
63
77
return module ;
64
78
} catch ( ex ) {
65
79
errors . push ( `ModuleName: ${ moduleName } \nFilename: ${ filename } \nException: ${ ex } \n${ match [ 1 ] } \n` ) ;
66
80
return undefined ;
67
81
}
68
82
}
83
+ regex = / @ C o m p o n e n t \s * \( \s * ( \{ .+ ?\} ) \s * \) \s * e x p o r t \s + c l a s s \s + ( \w + ) \s + / ims;
84
+ var match = regex . exec ( fileContents . toString ( ) ) ;
85
+ if ( match !== null ) {
86
+ return new Component ( match [ 2 ] ) ;
87
+ }
88
+ regex = / @ D i r e c t i v e \s * \( \s * ( \{ .+ ?\} ) \s * \) \s * e x p o r t \s + c l a s s \s + ( \w + ) \s + / ims;
89
+ var match = regex . exec ( fileContents . toString ( ) ) ;
90
+ if ( match !== null ) {
91
+ return new Directive ( match [ 2 ] ) ;
92
+ }
93
+ regex = / @ P i p e \s * \( \s * ( \{ .+ ?\} ) \s * \) \s * e x p o r t \s + c l a s s \s + ( \w + ) \s + / ims;
94
+ var match = regex . exec ( fileContents . toString ( ) ) ;
95
+ if ( match !== null ) {
96
+ return new Pipe ( match [ 2 ] ) ;
97
+ }
69
98
}
70
99
71
100
private static parseModuleContents ( moduleContents : string ) : NgModule {
0 commit comments