Skip to content

Commit fc712c3

Browse files
committed
Refactoring: Logic for scanning for modules moved to moduleManagaer
1 parent 567fcb5 commit fc712c3

File tree

3 files changed

+171
-158
lines changed

3 files changed

+171
-158
lines changed

src/commands/modulesToMarkdown.ts

Lines changed: 3 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,8 @@
1-
import * as fs from 'fs';
21
import * as vscode from 'vscode';
32
import * as path from 'path';
4-
import { ArrayUtils, Config, FileSystemUtils } from '@src';
3+
import { ArrayUtils, Config, FileSystemUtils, NgModule } from '@src';
54
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';
376

387
export class ModulesToMarkdown extends CommandBase {
398
private config = new Config();
@@ -43,16 +12,9 @@ export class ModulesToMarkdown extends CommandBase {
4312
this.checkForOpenWorkspace();
4413
const fsUtils = new FileSystemUtils();
4514
var workspaceDirectory: string = fsUtils.getWorkspaceFolder();
46-
const filenames = fsUtils.listFiles(workspaceDirectory, this.config.excludeDirectories, this.isTypescriptFile);
4715
let markdownContent = '# Modules\n\n';
4816
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);
5618
markdownContent = markdownContent +
5719
'## Modules in workspace\n\n' +
5820
'| Module | Declarations | Imports | Exports | Bootstrap | Providers | Entry points |\n' +
@@ -69,122 +31,6 @@ export class ModulesToMarkdown extends CommandBase {
6931
fsUtils.writeFileAndOpen(path.join(workspaceDirectory, this.config.modulesToMarkdownFilename), markdownContent);
7032
}
7133

72-
private readModule(filename: string, errors: string[]): NgModule | undefined {
73-
const fileContents = fs.readFileSync(filename);
74-
const regex: RegExp = /@NgModule\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\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-
18834
private generateModuleMarkdown(module: NgModule): string {
18935
let markdown = `## ${module.moduleName}\n\n`;
19036
markdown = markdown +

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './arrayUtils';
22
export * from './componentManager';
33
export * from './config';
4-
export * from './filesystemUtils';
4+
export * from './filesystemUtils';
5+
export * from './moduleManager';

src/moduleManager.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import * as fs from 'fs';
2+
import { ArrayUtils, Config, FileSystemUtils } from "@src";
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 {
14+
public imports: string[] = [];
15+
public exports: string[] = [];
16+
public declarations: string[] = [];
17+
public entryComponents: string[] = [];
18+
public providers: string[] = [];
19+
public bootstrap: string[] = [];
20+
public filename: string = '';
21+
public moduleName: string = '';
22+
public moduleContents: string = '';
23+
public moduleStats(): number[] {
24+
return [
25+
this.declarations === undefined ? 0 : ArrayUtils.arrayLength(this.declarations),
26+
this.imports === undefined ? 0 : ArrayUtils.arrayLength(this.imports),
27+
this.exports === undefined ? 0 : ArrayUtils.arrayLength(this.exports),
28+
this.bootstrap === undefined ? 0 : ArrayUtils.arrayLength(this.bootstrap),
29+
this.providers === undefined ? 0 : ArrayUtils.arrayLength(this.providers),
30+
this.entryComponents === undefined ? 0 : ArrayUtils.arrayLength(this.entryComponents),
31+
];
32+
}
33+
}
34+
35+
export class ModuleManager {
36+
37+
public static findModules(directoryPath: string, errors: string[], isTypescriptFile: (filename: string) => boolean): NgModule[] {
38+
const fsUtils = new FileSystemUtils();
39+
const config = new Config();
40+
const moduleFilenames = fsUtils.listFiles(directoryPath, config.excludeDirectories, isTypescriptFile);
41+
const modules: NgModule[] = [];
42+
moduleFilenames.sort(ArrayUtils.sortStrings).forEach(filename => {
43+
const module = this.readModule(filename, errors);
44+
if (module !== undefined) {
45+
modules.push(module);
46+
}
47+
});
48+
return modules;
49+
}
50+
51+
private static readModule(filename: string, errors: string[]): NgModule | undefined {
52+
const fileContents = fs.readFileSync(filename);
53+
const regex: RegExp = /@NgModule\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
54+
var match = regex.exec(fileContents.toString());
55+
if (match !== null) {
56+
const moduleName = match[2];
57+
const moduleContents = match[1];
58+
try {
59+
const module: NgModule = this.parseModuleContents(moduleContents);
60+
module.filename = filename;
61+
module.moduleName = moduleName;
62+
module.moduleContents = moduleContents;
63+
return module;
64+
} catch (ex) {
65+
errors.push(`ModuleName: ${moduleName}\nFilename: ${filename}\nException: ${ex}\n${match[1]}\n`);
66+
return undefined;
67+
}
68+
}
69+
}
70+
71+
private static parseModuleContents(moduleContents: string): NgModule {
72+
moduleContents = moduleContents.replace(/\s*?\/\/.*$/igm, () => ''); // Remove comments
73+
const module = new NgModule();
74+
let section = this.getSection(moduleContents, 'imports');
75+
if (section.length > 0) {
76+
module.imports = this.parseSection(section);
77+
}
78+
section = this.getSection(moduleContents, 'exports');
79+
if (section.length > 0) {
80+
module.exports = this.parseSection(section);
81+
}
82+
section = this.getSection(moduleContents, 'declarations');
83+
if (section.length > 0) {
84+
module.declarations = this.parseSection(section);
85+
}
86+
section = this.getSection(moduleContents, 'entryComponents');
87+
if (section.length > 0) {
88+
module.entryComponents = this.parseSection(section);
89+
}
90+
section = this.getSection(moduleContents, 'providers');
91+
if (section.length > 0) {
92+
module.providers = this.parseSection(section);
93+
}
94+
section = this.getSection(moduleContents, 'bootstrap');
95+
if (section.length > 0) {
96+
module.bootstrap = this.parseSection(section);
97+
}
98+
return module;
99+
}
100+
101+
private static getSection(moduleContents: string, sectionName: string): string {
102+
const regex = new RegExp("\\s*" + sectionName + ":\\s*\\[", "igms");
103+
const match = regex.exec(moduleContents);
104+
let section = '';
105+
if (match) {
106+
let endSectionFound = false;
107+
let inBrackets = 0;
108+
for (let currentPos = match.index; currentPos < moduleContents.length && !endSectionFound; currentPos++) {
109+
let currentChar = moduleContents.charAt(currentPos);
110+
switch (currentChar) {
111+
case '[':
112+
inBrackets++;
113+
break;
114+
case ']':
115+
inBrackets--;
116+
if (inBrackets === 0) {
117+
endSectionFound = true;
118+
section = moduleContents.substr(match.index + match[0].length, currentPos - match.index - match[0].length);
119+
}
120+
}
121+
}
122+
}
123+
return section;
124+
}
125+
126+
private static parseSection(sectionContents: string): string[] {
127+
const result: string[] = [];
128+
let currentElement = '';
129+
let inBrackets = 0;
130+
for (let currentPos = 0; currentPos < sectionContents.length; currentPos++) {
131+
let currentChar = sectionContents.charAt(currentPos);
132+
switch (currentChar) {
133+
case ',':
134+
if (inBrackets === 0) {
135+
currentElement = currentElement.replace(/^\s+|\s+$|[\r\t\n|,]/igms, '');
136+
if (currentElement.length > 0) {
137+
result.push(currentElement);
138+
}
139+
currentElement = '';
140+
} else {
141+
currentElement += currentChar;
142+
}
143+
break;
144+
case '{':
145+
case '(':
146+
case '[':
147+
inBrackets++;
148+
currentElement += currentChar;
149+
break;
150+
case '}':
151+
case ')':
152+
case ']':
153+
inBrackets--;
154+
currentElement += currentChar;
155+
break;
156+
default:
157+
currentElement += currentChar;
158+
}
159+
}
160+
currentElement = currentElement.replace(/^\s+|\s+$|[\r\t\n|,]/igms, '');
161+
if (currentElement.length > 0) {
162+
result.push(currentElement);
163+
}
164+
return result;
165+
}
166+
}

0 commit comments

Comments
 (0)