Skip to content

Commit a3b537b

Browse files
committed
Refactoring: All commands moved to seperate classes.
1 parent d8b6728 commit a3b537b

File tree

6 files changed

+282
-223
lines changed

6 files changed

+282
-223
lines changed

src/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './listAllImports';
2+
export * from './packageJsonToMarkdown';
3+
export * from './projectDirectoryStructure';

src/commands/listAllImports.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import { FileSystemUtils } from "../filesystemUtils";
4+
5+
export class ListAllImports {
6+
public static get commandName(): string { return 'listAllImports'; }
7+
8+
public execute() {
9+
const fsUtils = new FileSystemUtils();
10+
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+
this.writeResult(directoryPath, files);
14+
}
15+
16+
private isTypescriptFile(filename: string): boolean {
17+
return filename.endsWith('.ts') && !filename.endsWith('index.ts');
18+
}
19+
20+
private writeResult(workspaceDirectory: string, results: string[]) {
21+
const imports: { [module: string]: number } = {};
22+
if (!results) { return; }
23+
for (let i = 0; i < results.length; i++) {
24+
var file = results[i];
25+
const regex: RegExp = new RegExp('.*?\\s+from\\s+[\'"](.+)[\'"]', 'ig');
26+
const content = fs.readFileSync(file, 'utf8');
27+
const lines: string[] = content.split('\n');
28+
lines.forEach(line => {
29+
const match = regex.exec(line);
30+
if (match) {
31+
const key = match[1];
32+
if (imports.hasOwnProperty(key)) {
33+
imports[key] = imports[key] + 1;
34+
} else {
35+
imports[key] = 1;
36+
}
37+
}
38+
});
39+
}
40+
41+
const angularToolsOutput = vscode.window.createOutputChannel("Angular Tools");
42+
angularToolsOutput.clear();
43+
angularToolsOutput.appendLine(`Imports for files in workspace: ${workspaceDirectory}`);
44+
angularToolsOutput.appendLine('The number following each import in the list is the number of occurrences of the package import.\n');
45+
for (const key of Object.keys(imports).sort()) {
46+
angularToolsOutput.appendLine(`${key}: ${imports[key]}`);
47+
}
48+
angularToolsOutput.show();
49+
};
50+
51+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { FileSystemUtils } from '../filesystemUtils';
2+
import * as path from 'path';
3+
import * as fs from 'fs';
4+
const fetch = require('node-fetch');
5+
6+
export class PackageJsonToMarkdown {
7+
public static get commandName(): string { return 'packageJsonToMarkdown'; }
8+
9+
public execute() {
10+
const fsUtils = new FileSystemUtils();
11+
const directoryPath: string = fsUtils.getWorkspaceFolder();
12+
const excludeDirectories = ['bin', 'obj', 'node_modules', 'dist', 'packages', '.git', '.vs', '.github'];
13+
const isPackageJson = (filename: string): boolean => filename.toLowerCase().endsWith('package.json');
14+
const files = fsUtils.listFiles(directoryPath, excludeDirectories, isPackageJson);
15+
this.writeMarkdownFile(directoryPath, files);
16+
}
17+
18+
private writeMarkdownFile(workspaceDirectory: string, packageJsonFiles: string[]) {
19+
let devDependencies: string[] = [];
20+
let dependencies: string[] = [];
21+
let peerDependencies: string[] = [];
22+
packageJsonFiles.forEach(packageJsonFile => {
23+
const contents = fs.readFileSync(packageJsonFile).toString('utf8');
24+
const packageJson = JSON.parse(contents);
25+
if (packageJson.devDependencies) {
26+
devDependencies = [...new Set([...devDependencies, ...Object.keys(packageJson.devDependencies)])];
27+
}
28+
if (packageJson.dependencies) {
29+
dependencies = [...new Set([...dependencies, ...Object.keys(packageJson.dependencies)])];
30+
}
31+
if (packageJson.peerDependencies) {
32+
peerDependencies = [...new Set([...peerDependencies, ...Object.keys(packageJson.peerDependencies)])];
33+
}
34+
});
35+
36+
let dependenciesMarkdown = '';
37+
let devDependenciesMarkdown = '';
38+
let peerDependenciesMarkdown = '';
39+
const dependenciesRequests: Promise<{ name: string, description: string }>[] = [];
40+
dependencies.sort().forEach(pckName => {
41+
dependenciesRequests.push(this.makeRequest(pckName));
42+
});
43+
Promise.all(dependenciesRequests).then(responses => {
44+
responses.forEach(response => {
45+
if (response) {
46+
dependenciesMarkdown += `| ${response.name} | ${response.description} |\n`;
47+
}
48+
});
49+
}).then(() => {
50+
const devDependenciesRequests: Promise<{ name: string, description: string }>[] = [];
51+
devDependencies.sort().forEach(pckName => {
52+
devDependenciesRequests.push(this.makeRequest(pckName));
53+
});
54+
Promise.all(devDependenciesRequests).then(responses => {
55+
responses.forEach(response => {
56+
if (response) {
57+
devDependenciesMarkdown += `| ${response.name} | ${response.description} |\n`;
58+
}
59+
});
60+
}).then(() => {
61+
const peerDependenciesRequests: Promise<{ name: string, description: string }>[] = [];
62+
peerDependencies.sort().forEach(pckName => {
63+
peerDependenciesRequests.push(this.makeRequest(pckName));
64+
});
65+
Promise.all(peerDependenciesRequests).then(responses => {
66+
responses.forEach(response => {
67+
if (response) {
68+
peerDependenciesMarkdown += `| ${response.name} | ${response.description} |\n`;
69+
}
70+
});
71+
}).then(() => {
72+
const markdownContent =
73+
'# Package.json\n\n' +
74+
'## Dependencies\n\n' +
75+
'| Name | Description|\n' +
76+
'| ---- |:-----------|\n' +
77+
dependenciesMarkdown + '\n' +
78+
'## Dev dependencies\n\n' +
79+
'| Name | Description|\n' +
80+
'| ---- |:-----------|\n' +
81+
devDependenciesMarkdown + '\n' +
82+
'## Peer dependencies\n\n' +
83+
'| Name | Description|\n' +
84+
'| ---- |:-----------|\n' +
85+
peerDependenciesMarkdown;
86+
const fsUtils = new FileSystemUtils();
87+
fsUtils.writeFileAndOpen(path.join(workspaceDirectory, 'ReadMe-PackagesJson.md'), markdownContent);
88+
});
89+
});
90+
});
91+
}
92+
93+
private makeRequest(pckName: string): Promise<{ name: string, description: string }> {
94+
const uri = 'https://api.npms.io/v2/search?q=' + pckName + '%20not:deprecated,insecure,unstable';
95+
const request = fetch(uri).then((res: any) => res.json())
96+
.then((json: any) => {
97+
if (json.results[0] && json.results[0].package) {
98+
let packageName = json.results[0].package.name;
99+
packageName = packageName?.replace('|', '&#124;');
100+
let packageDescription = json.results[0].package.description;
101+
packageDescription = packageDescription?.replace('|', '&#124;');
102+
return { name: packageName, description: packageDescription };
103+
} else {
104+
console.log('Package not found: ' + pckName);
105+
}
106+
});
107+
return request;
108+
}
109+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as vscode from 'vscode';
2+
import { FileSystemUtils } from '../filesystemUtils';
3+
4+
export class ProjectDirectoryStructure {
5+
public static get commandName(): string { return 'projectDirectoryStructure'; }
6+
7+
public execute() {
8+
const fsUtils = new FileSystemUtils();
9+
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);
13+
}
14+
15+
private writeDirectoryStructure(workSpaceDirectory: string, filename: string, directories: string[]) {
16+
const angularToolsOutput = vscode.window.createOutputChannel("Angular Tools");
17+
angularToolsOutput.clear();
18+
angularToolsOutput.appendLine('Project Directory Structure');
19+
angularToolsOutput.appendLine(`Workspace directory: ${workSpaceDirectory}\n`);
20+
angularToolsOutput.appendLine('Directories:');
21+
directories?.forEach(directoryFullPath => {
22+
var directoryName = directoryFullPath.replace(workSpaceDirectory, '.');
23+
angularToolsOutput.appendLine(directoryName);
24+
});
25+
angularToolsOutput.show();
26+
}
27+
}

0 commit comments

Comments
 (0)