Skip to content

Commit e451978

Browse files
committed
Refactoring ModuleManager so it will now identify the type of the typescript files it is scanning so the information can be used in the commands.
1 parent 3aa0933 commit e451978

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

src/commands/modulesToMarkdown.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
3-
import { ArrayUtils, Config, FileSystemUtils, NgModule } from '@src';
3+
import { ArrayUtils, Config, FileSystemUtils, NgModule, Project } from '@src';
44
import { CommandBase } from '@commands';
55
import { ModuleManager } from '@src';
66

@@ -14,13 +14,13 @@ export class ModulesToMarkdown extends CommandBase {
1414
var workspaceDirectory: string = fsUtils.getWorkspaceFolder();
1515
let markdownContent = '# Modules\n\n';
1616
const errors: string[] = [];
17-
const modules: NgModule[] = ModuleManager.findModules(workspaceDirectory, errors, this.isTypescriptFile);
17+
const project: Project = ModuleManager.scanProject(workspaceDirectory, errors, this.isTypescriptFile);
1818
markdownContent = markdownContent +
1919
'## Modules in workspace\n\n' +
2020
'| Module | Declarations | Imports | Exports | Bootstrap | Providers | Entry points |\n' +
2121
'| ---| --- | --- | --- | --- | --- | --- |\n';
2222
let modulesMarkdown: string = '';
23-
modules.forEach(module => {
23+
project.modules.forEach(module => {
2424
markdownContent = markdownContent + '| ' + module.moduleName + ' | ' + module.moduleStats().join(' | ') + ' |\n';
2525
modulesMarkdown = modulesMarkdown + this.generateModuleMarkdown(module);
2626
});

src/moduleManager.ts

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import * as fs from 'fs';
22
import { ArrayUtils, Config, FileSystemUtils } from "@src";
33

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 {
145
public imports: string[] = [];
156
public exports: string[] = [];
167
public declarations: string[] = [];
@@ -19,7 +10,6 @@ export class NgModule implements INgModule {
1910
public bootstrap: string[] = [];
2011
public filename: string = '';
2112
public moduleName: string = '';
22-
public moduleContents: string = '';
2313
public moduleStats(): number[] {
2414
return [
2515
this.declarations === undefined ? 0 : ArrayUtils.arrayLength(this.declarations),
@@ -32,25 +22,50 @@ export class NgModule implements INgModule {
3222
}
3323
}
3424

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+
3541
export class ModuleManager {
3642

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 {
3844
const fsUtils = new FileSystemUtils();
3945
const config = new Config();
4046
const moduleFilenames = fsUtils.listFiles(directoryPath, config.excludeDirectories, isTypescriptFile);
41-
const modules: NgModule[] = [];
47+
const project = new Project();
4248
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);
4661
}
4762
});
48-
return modules;
63+
return project;
4964
}
5065

51-
private static readModule(filename: string, errors: string[]): NgModule | undefined {
66+
private static readTypescriptFile(filename: string, errors: string[]): NgModule | Component | Directive | Pipe | undefined {
5267
const fileContents = fs.readFileSync(filename);
53-
const regex: RegExp = /@NgModule\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
68+
let regex: RegExp = /@NgModule\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
5469
var match = regex.exec(fileContents.toString());
5570
if (match !== null) {
5671
const moduleName = match[2];
@@ -59,13 +74,27 @@ export class ModuleManager {
5974
const module: NgModule = this.parseModuleContents(moduleContents);
6075
module.filename = filename;
6176
module.moduleName = moduleName;
62-
module.moduleContents = moduleContents;
6377
return module;
6478
} catch (ex) {
6579
errors.push(`ModuleName: ${moduleName}\nFilename: ${filename}\nException: ${ex}\n${match[1]}\n`);
6680
return undefined;
6781
}
6882
}
83+
regex = /@Component\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
84+
var match = regex.exec(fileContents.toString());
85+
if (match !== null) {
86+
return new Component(match[2]);
87+
}
88+
regex = /@Directive\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
89+
var match = regex.exec(fileContents.toString());
90+
if (match !== null) {
91+
return new Directive(match[2]);
92+
}
93+
regex = /@Pipe\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
94+
var match = regex.exec(fileContents.toString());
95+
if (match !== null) {
96+
return new Pipe(match[2]);
97+
}
6998
}
7099

71100
private static parseModuleContents(moduleContents: string): NgModule {

0 commit comments

Comments
 (0)