-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension_devman.js
82 lines (65 loc) · 2.67 KB
/
extension_devman.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
function activate(context) {
console.log('"sss" is now active!');
let disposable = vscode.commands.registerCommand('sss.generateReports', () => {
const rulePath = path.join(vscode.workspace.rootPath, '.vscode', 'devman.json');
const ruleContent = fs.readFileSync(rulePath, 'utf-8');
const ruleJson = JSON.parse(ruleContent);
const filesToScan = getFilesToScan(vscode.workspace.rootPath);
let violations = [];
filesToScan.forEach(file => {
const content = fs.readFileSync(file, 'utf-8');
const lines = content.split('\n');
for (let i = 0; i < lines.length; i++) {
for (const keyword of ruleJson.sensitiveKeywords) {
if (lines[i].includes(keyword)) {
violations.push({ file, lineNumber: i + 1, ruleMatched: keyword });
}
}
}
});
if (violations.length > 0) {
const resultFolderPath = path.join(vscode.workspace.rootPath, '.vscode', 'result');
const datetimeSuffix = new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').split('.')[0];
const resultFolderName = `result-${datetimeSuffix}`;
const resultFolderFullPath = path.join(resultFolderPath, resultFolderName);
if (!fs.existsSync(resultFolderPath)) {
fs.mkdirSync(resultFolderPath);
}
fs.mkdirSync(resultFolderFullPath);
const reportFilePath = path.join(resultFolderFullPath, 'devman_report.json');
fs.writeFileSync(reportFilePath, JSON.stringify(violations, null, 4));
vscode.window.showInformationMessage('DevMan scan report generated successfully!');
} else {
vscode.window.showInformationMessage('No violations found!');
}
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
function getFilesToScan(rootPath) {
const excludeFolders = ['node_modules', 'target'];
const files = [];
function traverseDirectory(dir) {
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
if (!excludeFolders.includes(file)) {
traverseDirectory(filePath);
}
} else {
files.push(filePath);
}
});
}
traverseDirectory(rootPath);
return files;
}
function deactivate() {}
module.exports = {
activate,
deactivate
};