1
- import * as fs from 'fs' ;
2
1
import * as vscode from 'vscode' ;
3
2
import * as path from 'path' ;
4
- import { ArrayUtils , Config , FileSystemUtils } from '@src' ;
3
+ import { ArrayUtils , Config , FileSystemUtils , NgModule } from '@src' ;
5
4
import { CommandBase } from '@commands' ;
6
-
7
- export interface INgModule {
8
- imports : string [ ] ;
9
- exports : string [ ] ;
10
- declarations : string [ ] ;
11
- entryComponents : string [ ] ;
12
- providers : string [ ] ;
13
- bootstrap : string [ ] ;
14
- }
15
-
16
- export class NgModule implements INgModule {
17
- public imports : string [ ] = [ ] ;
18
- public exports : string [ ] = [ ] ;
19
- public declarations : string [ ] = [ ] ;
20
- public entryComponents : string [ ] = [ ] ;
21
- public providers : string [ ] = [ ] ;
22
- public bootstrap : string [ ] = [ ] ;
23
- public filename : string = '' ;
24
- public moduleName : string = '' ;
25
- public moduleContents : string = '' ;
26
- public moduleStats ( ) : number [ ] {
27
- return [
28
- this . declarations === undefined ? 0 : ArrayUtils . arrayLength ( this . declarations ) ,
29
- this . imports === undefined ? 0 : ArrayUtils . arrayLength ( this . imports ) ,
30
- this . exports === undefined ? 0 : ArrayUtils . arrayLength ( this . exports ) ,
31
- this . bootstrap === undefined ? 0 : ArrayUtils . arrayLength ( this . bootstrap ) ,
32
- this . providers === undefined ? 0 : ArrayUtils . arrayLength ( this . providers ) ,
33
- this . entryComponents === undefined ? 0 : ArrayUtils . arrayLength ( this . entryComponents ) ,
34
- ] ;
35
- }
36
- }
5
+ import { ModuleManager } from '@src' ;
37
6
38
7
export class ModulesToMarkdown extends CommandBase {
39
8
private config = new Config ( ) ;
@@ -43,16 +12,9 @@ export class ModulesToMarkdown extends CommandBase {
43
12
this . checkForOpenWorkspace ( ) ;
44
13
const fsUtils = new FileSystemUtils ( ) ;
45
14
var workspaceDirectory : string = fsUtils . getWorkspaceFolder ( ) ;
46
- const filenames = fsUtils . listFiles ( workspaceDirectory , this . config . excludeDirectories , this . isTypescriptFile ) ;
47
15
let markdownContent = '# Modules\n\n' ;
48
16
const errors : string [ ] = [ ] ;
49
- const modules : NgModule [ ] = [ ] ;
50
- filenames . sort ( ArrayUtils . sortStrings ) . forEach ( filename => {
51
- const module = this . readModule ( filename , errors ) ;
52
- if ( module !== undefined ) {
53
- modules . push ( module ) ;
54
- }
55
- } ) ;
17
+ const modules : NgModule [ ] = ModuleManager . findModules ( workspaceDirectory , errors , this . isTypescriptFile ) ;
56
18
markdownContent = markdownContent +
57
19
'## Modules in workspace\n\n' +
58
20
'| Module | Declarations | Imports | Exports | Bootstrap | Providers | Entry points |\n' +
@@ -69,122 +31,6 @@ export class ModulesToMarkdown extends CommandBase {
69
31
fsUtils . writeFileAndOpen ( path . join ( workspaceDirectory , this . config . modulesToMarkdownFilename ) , markdownContent ) ;
70
32
}
71
33
72
- private readModule ( filename : string , errors : string [ ] ) : NgModule | undefined {
73
- const fileContents = fs . readFileSync ( filename ) ;
74
- 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;
75
- var match = regex . exec ( fileContents . toString ( ) ) ;
76
- if ( match !== null ) {
77
- const moduleName = match [ 2 ] ;
78
- const moduleContents = match [ 1 ] ;
79
- try {
80
- const module : NgModule = this . parseModuleContents ( moduleContents ) ;
81
- module . filename = filename ;
82
- module . moduleName = moduleName ;
83
- module . moduleContents = moduleContents ;
84
- return module ;
85
- } catch ( ex ) {
86
- errors . push ( `ModuleName: ${ moduleName } \nFilename: ${ filename } \nException: ${ ex } \n${ match [ 1 ] } \n` ) ;
87
- return undefined ;
88
- }
89
- }
90
- }
91
-
92
- private parseModuleContents ( moduleContents : string ) : NgModule {
93
- moduleContents = moduleContents . replace ( / \s * ?\/ \/ .* $ / igm, ( ) => '' ) ; // Remove comments
94
- const module = new NgModule ( ) ;
95
- let section = this . getSection ( moduleContents , 'imports' ) ;
96
- if ( section . length > 0 ) {
97
- module . imports = this . parseSection ( section ) ;
98
- }
99
- section = this . getSection ( moduleContents , 'exports' ) ;
100
- if ( section . length > 0 ) {
101
- module . exports = this . parseSection ( section ) ;
102
- }
103
- section = this . getSection ( moduleContents , 'declarations' ) ;
104
- if ( section . length > 0 ) {
105
- module . declarations = this . parseSection ( section ) ;
106
- }
107
- section = this . getSection ( moduleContents , 'entryComponents' ) ;
108
- if ( section . length > 0 ) {
109
- module . entryComponents = this . parseSection ( section ) ;
110
- }
111
- section = this . getSection ( moduleContents , 'providers' ) ;
112
- if ( section . length > 0 ) {
113
- module . providers = this . parseSection ( section ) ;
114
- }
115
- section = this . getSection ( moduleContents , 'bootstrap' ) ;
116
- if ( section . length > 0 ) {
117
- module . bootstrap = this . parseSection ( section ) ;
118
- }
119
- return module ;
120
- }
121
-
122
- private getSection ( moduleContents : string , sectionName : string ) : string {
123
- const regex = new RegExp ( "\\s*" + sectionName + ":\\s*\\[" , "igms" ) ;
124
- const match = regex . exec ( moduleContents ) ;
125
- let section = '' ;
126
- if ( match ) {
127
- let endSectionFound = false ;
128
- let inBrackets = 0 ;
129
- for ( let currentPos = match . index ; currentPos < moduleContents . length && ! endSectionFound ; currentPos ++ ) {
130
- let currentChar = moduleContents . charAt ( currentPos ) ;
131
- switch ( currentChar ) {
132
- case '[' :
133
- inBrackets ++ ;
134
- break ;
135
- case ']' :
136
- inBrackets -- ;
137
- if ( inBrackets === 0 ) {
138
- endSectionFound = true ;
139
- section = moduleContents . substr ( match . index + match [ 0 ] . length , currentPos - match . index - match [ 0 ] . length ) ;
140
- }
141
- }
142
- }
143
- }
144
- return section ;
145
- }
146
-
147
- private parseSection ( sectionContents : string ) : string [ ] {
148
- const result : string [ ] = [ ] ;
149
- let currentElement = '' ;
150
- let inBrackets = 0 ;
151
- for ( let currentPos = 0 ; currentPos < sectionContents . length ; currentPos ++ ) {
152
- let currentChar = sectionContents . charAt ( currentPos ) ;
153
- switch ( currentChar ) {
154
- case ',' :
155
- if ( inBrackets === 0 ) {
156
- currentElement = currentElement . replace ( / ^ \s + | \s + $ | [ \r \t \n | , ] / igms, '' ) ;
157
- if ( currentElement . length > 0 ) {
158
- result . push ( currentElement ) ;
159
- }
160
- currentElement = '' ;
161
- } else {
162
- currentElement += currentChar ;
163
- }
164
- break ;
165
- case '{' :
166
- case '(' :
167
- case '[' :
168
- inBrackets ++ ;
169
- currentElement += currentChar ;
170
- break ;
171
- case '}' :
172
- case ')' :
173
- case ']' :
174
- inBrackets -- ;
175
- currentElement += currentChar ;
176
- break ;
177
- default :
178
- currentElement += currentChar ;
179
- }
180
- }
181
- currentElement = currentElement . replace ( / ^ \s + | \s + $ | [ \r \t \n | , ] / igms, '' ) ;
182
- if ( currentElement . length > 0 ) {
183
- result . push ( currentElement ) ;
184
- }
185
- return result ;
186
- }
187
-
188
34
private generateModuleMarkdown ( module : NgModule ) : string {
189
35
let markdown = `## ${ module . moduleName } \n\n` ;
190
36
markdown = markdown +
0 commit comments