Skip to content

Commit 6fd1a1d

Browse files
committed
Static values and constants moved to Config.ts
1 parent 67ac2a7 commit 6fd1a1d

File tree

7 files changed

+91
-50
lines changed

7 files changed

+91
-50
lines changed

src/commands/componentHierarchyDgml.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import * as vscode from 'vscode';
21
import * as fs from 'fs';
32
import * as path from 'path';
4-
import { FileSystemUtils } from "../filesystemUtils";
3+
import * as vscode from 'vscode';
54
import * as xmldom from 'xmldom';
5+
6+
import { Config } from '../config';
7+
import { FileSystemUtils } from '../filesystemUtils';
8+
69
const prettifyXml = require('prettify-xml');
710
const xmlSerializer = require('xmlserializer');
811

@@ -29,22 +32,20 @@ export class ComponentHierarchyDgml {
2932
public execute() {
3033
const fsUtils = new FileSystemUtils();
3134
var directoryPath: string = fsUtils.getWorkspaceFolder();
32-
const excludeDirectories = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
33-
const componentFilenames = fsUtils.listFiles(directoryPath, excludeDirectories, this.isComponentFile);
35+
const componentFilenames = fsUtils.listFiles(directoryPath, Config.excludeDirectories, this.isComponentFile);
3436
const components = this.findComponents(componentFilenames);
3537
this.scanComponentTemplates(components);
3638

37-
const graphFilename = 'ReadMe-ProjectStructure.dgml';
3839
const domImpl = new xmldom.DOMImplementation();
3940
const documentParser = new xmldom.DOMParser();
4041
let xmlDocument: Document;
4142
let root: Element;
4243

4344
try {
4445
// if the graph file already exists, then read it and parse it into a xml document object
45-
if (fs.existsSync(graphFilename)) {
46+
if (fs.existsSync(Config.dgmlGraphFilename)) {
4647
try {
47-
const content = fs.readFileSync(graphFilename).toString();
48+
const content = fs.readFileSync(Config.dgmlGraphFilename).toString();
4849
xmlDocument = documentParser.parseFromString(content, 'text/xml');
4950
} catch {
5051
xmlDocument = this.createNewDirectedGraph(domImpl);
@@ -64,7 +65,7 @@ export class ComponentHierarchyDgml {
6465

6566
// Write the prettified xml string to the ReadMe-ProjectStructure.dgml file.
6667
const fsUtils = new FileSystemUtils();
67-
fsUtils.writeFile(path.join(directoryPath, graphFilename), fileContent, () => {
68+
fsUtils.writeFile(path.join(directoryPath, Config.dgmlGraphFilename), fileContent, () => {
6869
vscode.window.showInformationMessage('The project structure has been analyzed and a Directed Graph Markup Language (dgml) file has been created\nThe ReadMe-ProjectStructure.dgml file can now be viewed in Visual Studio');
6970
});
7071
} catch (ex) {
@@ -145,9 +146,9 @@ export class ComponentHierarchyDgml {
145146
private createNewDirectedGraph(domImpl: DOMImplementation) {
146147
let xmlDoc: Document = domImpl.createDocument('', null, null);
147148
const root = xmlDoc.createElement("DirectedGraph");
148-
root.setAttribute("GraphDirection", "LeftToRight");
149-
root.setAttribute("Layout", "Sugiyama");
150-
root.setAttribute("ZoomLevel", "-1");
149+
root.setAttribute("GraphDirection", Config.dgmlGraphDirection);
150+
root.setAttribute("Layout", Config.dgmlGraphLayout);
151+
root.setAttribute("ZoomLevel", Config.dgmlZooLevel);
151152
root.setAttribute("xmlns", "http://schemas.microsoft.com/vs/2009/dgml");
152153
xmlDoc.appendChild(root);
153154
return xmlDoc;
@@ -254,7 +255,7 @@ export class ComponentHierarchyDgml {
254255
const categoryElement = xmlDoc.createElement("Category");
255256
categoryElement.setAttribute("Id", "RootComponent");
256257
categoryElement.setAttribute("Label", "Root component");
257-
categoryElement.setAttribute("Background", "#FF00AA00");
258+
categoryElement.setAttribute("Background", Config.rootNodeBackgroundColor);
258259
categoryElement.setAttribute("IsTag", "True");
259260
this.addNode(categoriesElement, categoryElement);
260261
}

src/commands/listAllImports.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import * as vscode from 'vscode';
21
import * as fs from 'fs';
3-
import { FileSystemUtils } from "../filesystemUtils";
2+
import * as vscode from 'vscode';
3+
4+
import { Config } from '../config';
5+
import { FileSystemUtils } from '../filesystemUtils';
46

57
export class ListAllImports {
68
public static get commandName(): string { return 'listAllImports'; }
79

810
public execute() {
911
const fsUtils = new FileSystemUtils();
1012
var directoryPath: string = fsUtils.getWorkspaceFolder();
11-
const excludeDirectories = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
12-
const files = fsUtils.listFiles(directoryPath, excludeDirectories, this.isTypescriptFile);
13+
const files = fsUtils.listFiles(directoryPath, Config.excludeDirectories, this.isTypescriptFile);
1314
this.writeResult(directoryPath, files);
1415
}
1516

@@ -38,7 +39,7 @@ export class ListAllImports {
3839
});
3940
}
4041

41-
const angularToolsOutput = vscode.window.createOutputChannel("Angular Tools");
42+
const angularToolsOutput = vscode.window.createOutputChannel(Config.angularToolsOutputChannel);
4243
angularToolsOutput.clear();
4344
angularToolsOutput.appendLine(`Imports for files in workspace: ${workspaceDirectory}`);
4445
angularToolsOutput.appendLine('The number following each import in the list is the number of occurrences of the package import.\n');

src/commands/packageJsonToMarkdown.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { FileSystemUtils } from '../filesystemUtils';
2-
import * as path from 'path';
31
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
import { Config } from '../config';
5+
import { FileSystemUtils } from '../filesystemUtils';
6+
47
const fetch = require('node-fetch');
58

69
export class PackageJsonToMarkdown {
@@ -9,9 +12,8 @@ export class PackageJsonToMarkdown {
912
public execute() {
1013
const fsUtils = new FileSystemUtils();
1114
const directoryPath: string = fsUtils.getWorkspaceFolder();
12-
const excludeDirectories = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
1315
const isPackageJson = (filename: string): boolean => filename.toLowerCase().endsWith('package.json');
14-
const files = fsUtils.listFiles(directoryPath, excludeDirectories, isPackageJson);
16+
const files = fsUtils.listFiles(directoryPath, Config.excludeDirectories, isPackageJson);
1517
this.writeMarkdownFile(directoryPath, files);
1618
}
1719

@@ -84,7 +86,7 @@ export class PackageJsonToMarkdown {
8486
'| ---- |:-----------|\n' +
8587
peerDependenciesMarkdown;
8688
const fsUtils = new FileSystemUtils();
87-
fsUtils.writeFileAndOpen(path.join(workspaceDirectory, 'ReadMe-PackagesJson.md'), markdownContent);
89+
fsUtils.writeFileAndOpen(path.join(workspaceDirectory, Config.packageJsonMarkdownFilename), markdownContent);
8890
});
8991
});
9092
});

src/commands/projectDirectoryStructure.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as vscode from 'vscode';
2+
3+
import { Config } from '../config';
24
import { FileSystemUtils } from '../filesystemUtils';
35

46
export class ProjectDirectoryStructure {
@@ -7,13 +9,12 @@ export class ProjectDirectoryStructure {
79
public execute() {
810
const fsUtils = new FileSystemUtils();
911
var workspaceDirectory: string = fsUtils.getWorkspaceFolder();
10-
const excludeDirectories = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
11-
const directories: string[] = fsUtils.listDirectories(workspaceDirectory, excludeDirectories);
12-
this.writeDirectoryStructure(workspaceDirectory, 'ReadMe-ProjectDirectoryStructure.md', directories);
12+
const directories: string[] = fsUtils.listDirectories(workspaceDirectory, Config.excludeDirectories);
13+
this.writeDirectoryStructure(workspaceDirectory, Config.projectDirectoryStructureMarkdownFilename, directories);
1314
}
1415

1516
private writeDirectoryStructure(workSpaceDirectory: string, filename: string, directories: string[]) {
16-
const angularToolsOutput = vscode.window.createOutputChannel("Angular Tools");
17+
const angularToolsOutput = vscode.window.createOutputChannel(Config.angularToolsOutputChannel);
1718
angularToolsOutput.clear();
1819
angularToolsOutput.appendLine('Project Directory Structure');
1920
angularToolsOutput.appendLine(`Workspace directory: ${workSpaceDirectory}\n`);

src/commands/showComponentHierarchy.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import * as vscode from 'vscode';
21
import * as fs from 'fs';
3-
import * as path from 'path';
4-
import { FileSystemUtils } from "../filesystemUtils";
52
import { Base64 } from 'js-base64';
3+
import * as path from 'path';
4+
import * as vscode from 'vscode';
5+
6+
import { Config } from '../config';
7+
import { FileSystemUtils } from '../filesystemUtils';
68

79
class Component {
810

@@ -53,10 +55,11 @@ export class ShowComponentHierarchy {
5355

5456
private extensionContext: vscode.ExtensionContext;
5557
private fsUtils = new FileSystemUtils();
58+
private static readonly Name = 'showComponentHierarchy';
5659
constructor(context: vscode.ExtensionContext) {
5760
this.extensionContext = context;
5861
}
59-
public static get commandName(): string { return 'showComponentHierarchy'; }
62+
public static get commandName(): string { return ShowComponentHierarchy.Name; }
6063

6164
public execute(webview: vscode.Webview) {
6265

@@ -73,8 +76,7 @@ export class ShowComponentHierarchy {
7376
);
7477

7578
var directoryPath: string = this.fsUtils.getWorkspaceFolder();
76-
const excludeDirectories = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
77-
const componentFilenames = this.fsUtils.listFiles(directoryPath, excludeDirectories, this.isComponentFile);
79+
const componentFilenames = this.fsUtils.listFiles(directoryPath, Config.excludeDirectories, this.isComponentFile);
7880
const components = this.findComponents(componentFilenames);
7981
this.enrichComponentsFromComponentTemplates(components);
8082

@@ -109,10 +111,10 @@ export class ShowComponentHierarchy {
109111

110112
try {
111113
const jsContent = this.generateJavascriptContent(nodesJson, rootNodesJson, edgesJson);
112-
const outputJsFilename = 'showComponentHierarchy.js';
114+
const outputJsFilename = ShowComponentHierarchy.Name + '.js';
113115
let htmlContent = this.generateHtmlContent(webview, outputJsFilename);
114116

115-
this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('out', 'showComponentHierarchy.html')), htmlContent, () => { }); // For debugging
117+
this.fsUtils.writeFile(this.extensionContext?.asAbsolutePath(path.join('out', ShowComponentHierarchy.Name + '.html')), htmlContent, () => { }); // For debugging
116118
this.fsUtils.writeFile(
117119
this.extensionContext?.asAbsolutePath(path.join('out', outputJsFilename)),
118120
jsContent,
@@ -199,29 +201,29 @@ export class ShowComponentHierarchy {
199201
for (let selector in componentHash) {
200202
const component = componentHash[selector];
201203
if (component.isRoot) {
202-
this.generateDirectedGraphNodesXml(component.subComponents, component, true, appendNodes);
203-
this.generateDirectedGraphLinksXml(component.subComponents, selector, "", appendLinks);
204+
this.generateDirectedGraphNodes(component.subComponents, component, true, appendNodes);
205+
this.generateDirectedGraphEdges(component.subComponents, selector, "", appendLinks);
204206
}
205207
}
206208
}
207209

208-
private generateDirectedGraphNodesXml(components: Component[], component: Component, isRoot: boolean, appendNodes: (nodeList: Node[]) => void) {
210+
private generateDirectedGraphNodes(components: Component[], component: Component, isRoot: boolean, appendNodes: (nodeList: Node[]) => void) {
209211
appendNodes([new Node(component.selector, component.templateFilename, isRoot)]);
210212
if (components.length > 0) {
211213
components.forEach((subComponent) => {
212-
this.generateDirectedGraphNodesXml(subComponent.subComponents, subComponent, subComponent.isRoot, appendNodes);
214+
this.generateDirectedGraphNodes(subComponent.subComponents, subComponent, subComponent.isRoot, appendNodes);
213215
});
214216
}
215217
}
216218

217-
private generateDirectedGraphLinksXml(subComponents: Component[], selector: string, parentSelector: string, appendLinks: (edgeList: Edge[]) => void) {
219+
private generateDirectedGraphEdges(subComponents: Component[], selector: string, parentSelector: string, appendLinks: (edgeList: Edge[]) => void) {
218220
if (parentSelector.length > 0) {
219221
const id = Math.random() * 100000;
220222
appendLinks([new Edge(id.toString(), parentSelector, selector)]);
221223
}
222224
if (subComponents.length > 0) {
223225
subComponents.forEach((subComponent) => {
224-
this.generateDirectedGraphLinksXml(subComponent.subComponents, subComponent.selector, selector, appendLinks);
226+
this.generateDirectedGraphEdges(subComponent.subComponents, subComponent.selector, selector, appendLinks);
225227
});
226228
}
227229
}
@@ -236,25 +238,31 @@ export class ShowComponentHierarchy {
236238
}
237239

238240
private generateJavascriptContent(nodesJson: string, rootNodesJson: string, edgesJson: string): string {
239-
const templateJsFilename = 'showComponentHierarchy_Template.js';
241+
const templateJsFilename = ShowComponentHierarchy.Name + '_Template.js';
240242
let template = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', templateJsFilename)), 'utf8');
241243
let jsContent = template.replace('var nodes = new vis.DataSet([]);', `var nodes = new vis.DataSet([${nodesJson}]);`);
242244
jsContent = jsContent.replace('var rootNodes = [];', `var rootNodes = [${rootNodesJson}];`);
243245
jsContent = jsContent.replace('var edges = new vis.DataSet([]);', `var edges = new vis.DataSet([${edgesJson}]);`);
246+
jsContent = jsContent.replace('background: "#00FF00" // rootNode background color', `background: "${Config.visRootNodeBackgroundColor}" // rootNode background color`);
247+
jsContent = jsContent.replace('type: "triangle" // edge arrow to type', `type: "${Config.visEdgeArrowToType}" // edge arrow to type`);
248+
jsContent = jsContent.replace('ctx.strokeStyle = \'blue\'; // graph selection guideline color', `ctx.strokeStyle = '${Config.graphSelectionGuidelineColor}'; // graph selection guideline color`);
249+
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${Config.graphSelectionGuidelineWidth}; // graph selection guideline width`);
250+
jsContent = jsContent.replace('selectionCanvasContext.strokeStyle = \'red\';', `selectionCanvasContext.strokeStyle = '${Config.graphSelectionColor}';`);
251+
jsContent = jsContent.replace('selectionCanvasContext.lineWidth = 2;', `selectionCanvasContext.lineWidth = ${Config.graphSelectionWidth};`);
244252
return jsContent;
245253
}
246254

247255
private generateHtmlContent(webview: vscode.Webview, outputJsFilename: string): string {
248-
const templateHtmlFilename = 'showComponentHierarchy_Template.html';
256+
const templateHtmlFilename = ShowComponentHierarchy.Name + '_Template.html';
249257
let htmlContent = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', templateHtmlFilename)), 'utf8');
250258

251259
const visPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'javascript', 'vis-network.min.js');
252260
const visUri = webview.asWebviewUri(visPath);
253261
htmlContent = htmlContent.replace('vis-network.min.js', visUri.toString());
254262

255-
const cssPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'stylesheet', 'showComponentHierarchy.css');
263+
const cssPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'stylesheet', ShowComponentHierarchy.Name + '.css');
256264
const cssUri = webview.asWebviewUri(cssPath);
257-
htmlContent = htmlContent.replace('showComponentHierarchy.css', cssUri.toString());
265+
htmlContent = htmlContent.replace(ShowComponentHierarchy.Name + '.css', cssUri.toString());
258266

259267
const nonce = this.getNonce();
260268
htmlContent = htmlContent.replace('nonce-nonce', `nonce-${nonce}`);
@@ -263,7 +271,7 @@ export class ShowComponentHierarchy {
263271

264272
const jsPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'out', outputJsFilename);
265273
const jsUri = webview.asWebviewUri(jsPath);
266-
htmlContent = htmlContent.replace('showComponentHierarchy.js', jsUri.toString());
274+
htmlContent = htmlContent.replace(ShowComponentHierarchy.Name + '.js', jsUri.toString());
267275
return htmlContent;
268276
}
269277

@@ -273,7 +281,7 @@ export class ShowComponentHierarchy {
273281
const u8arr = Base64.toUint8Array(dataUrl[1]);
274282

275283
const workspaceDirectory = this.fsUtils.getWorkspaceFolder();
276-
const newFilePath = path.join(workspaceDirectory, 'ComponentHierarchy.png');
284+
const newFilePath = path.join(workspaceDirectory, Config.componentHierarchyFilename);
277285
this.fsUtils.writeFile(newFilePath, u8arr, () => {});
278286

279287
vscode.window.showInformationMessage('The file ComponentHierarchy.png has been created in the root of the workspace.');

src/config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
export class Config {
3+
public static readonly excludeDirectories: string[] = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
4+
5+
public static readonly angularToolsOutputChannel = 'Angular Tools';
6+
7+
// ComponentHierarchyDgml
8+
public static readonly dgmlGraphFilename = 'ReadMe-ProjectStructure.dgml';
9+
public static readonly dgmlGraphLayout = 'Sugiyama';
10+
public static readonly dgmlGraphDirection = 'LeftToRight';
11+
public static readonly dgmlZooLevel = '-1';
12+
public static readonly rootNodeBackgroundColor = '#FF00AA00';
13+
14+
// PackageJsonToMarkdown
15+
public static readonly packageJsonMarkdownFilename ='ReadMe-PackagesJson.md';
16+
17+
// ProjectDirectoryStructure
18+
public static readonly projectDirectoryStructureMarkdownFilename ='ReadMe-ProjectDirectoryStructure.md';
19+
20+
// ShowComponentHierarchy
21+
public static readonly visRootNodeBackgroundColor = '#00FF00';
22+
public static readonly visEdgeArrowToType = 'triangle';
23+
public static readonly graphSelectionGuidelineColor = 'blue';
24+
public static readonly graphSelectionGuidelineWidth = 1;
25+
public static readonly graphSelectionColor = 'red';
26+
public static readonly graphSelectionWidth = 2;
27+
public static readonly componentHierarchyFilename = 'ComponentHierarchy.png';
28+
}

templates/showComponentHierarchy_Template.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
var rootNodes = [];
55
rootNodes.forEach(nodeId => {
66
nodes.get(nodeId).color = {
7-
background: "#00FF00"
7+
background: "#00FF00" // rootNode background color
88
};
99
});
1010

1111
var arrowAttr = {
1212
to: {
1313
enabled: true,
14-
type: "triangle"
14+
type: "triangle" // edge arrow to type
1515
}
1616
};
1717
var edges = new vis.DataSet([]);
@@ -91,8 +91,8 @@
9191
ctx.moveTo(0, mouseY);
9292
ctx.lineTo(selectionCanvas.width, mouseY);
9393
}
94-
ctx.strokeStyle = 'blue';
95-
ctx.lineWidth = 1;
94+
ctx.strokeStyle = 'blue'; // graph selection guideline color
95+
ctx.lineWidth = 1; // graph selection guideline width
9696
ctx.stroke();
9797
}
9898

0 commit comments

Comments
 (0)