-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanningOperations.js
124 lines (97 loc) · 5.22 KB
/
scanningOperations.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// scanningOperations.js
const fs = require('fs');
const vscode = require('vscode');
const path = require('path'); // Import the path module
const { downloadFiles } = require('./fileDownloader');
const { readFile, createDirectoryIfNotExists } = require('./fileSystemOperations');
const { checkDevManViolations } = require('./devManViolations');
const { checkDockerfileViolations } = require('./dockerfileViolations');
const { checkCTCViolations } = require('./ctcViolations');
const { generateReport, generateHtmlReport } = require('./reportGeneration');
async function getFilesToScan(rootPath) {
const excludeFolders = ['.vscode', 'node_modules', 'target'];
const files = [];
async function traverseDirectory(dir) {
const fileEntries = await vscode.workspace.fs.readDirectory(vscode.Uri.file(dir));
for (const [name, type] of fileEntries) {
const filePath = path.join(dir, name);
if (type === vscode.FileType.Directory) {
if (!excludeFolders.includes(name)) {
await traverseDirectory(filePath);
}
} else {
files.push(filePath);
}
}
}
await traverseDirectory(rootPath);
return files;
}
async function activate(context) {
console.log('"sss" is now active!');
let disposable = vscode.commands.registerCommand('sss.generateReports', async () => {
try {
// Download files before running rule validation
await downloadFiles();
// Load DevMan rules
const devmanRulePath = path.join(vscode.workspace.rootPath, '.vscode', 'devman.json');
const devmanRuleContent = await readFile(devmanRulePath);
const devmanRuleJson = JSON.parse(devmanRuleContent);
// Load CTC rules
const ctcRulePath = path.join(vscode.workspace.rootPath, '.vscode', 'ctcrule.json');
const ctcRuleContent = await readFile(ctcRulePath);
const ctcRuleJson = JSON.parse(ctcRuleContent);
// Load Dockerfile rules
const ruleFilePath = path.join(vscode.workspace.rootPath, '.vscode', 'docker.json');
// Load package.json
const packageJsonPath = path.join(vscode.workspace.rootPath, 'package.json');
const packageJsonContent = await readFile(packageJsonPath);
const packageJson = JSON.parse(packageJsonContent);
const filesToScan = await getFilesToScan(vscode.workspace.rootPath);
let devmanViolations = [];
let ctcViolations = [];
let dockerViolations = [];
for (const file of filesToScan) {
const content = await readFile(file);
// Check for DevMan violations
if (path.basename(file).toLowerCase() !== 'dockerfile') {
// DevMan violations are not checked in Dockerfile
devmanViolations = devmanViolations.concat(checkDevManViolations(content, devmanRuleJson, file));
}
// Check for CTC violations in package.json
if (path.basename(file).toLowerCase() === 'package.json') {
ctcViolations = ctcViolations.concat(checkCTCViolations(content, ctcRuleJson, file));
}
// Check for Dockerfile violations
if (path.basename(file).toLowerCase() === 'dockerfile') {
dockerViolations = dockerViolations.concat(await checkDockerfileViolations(content, ruleFilePath, file));
}
}
// Generate reports and show success message
const resultFolderPath = path.join(vscode.workspace.rootPath, '.vscode', 'result');
const datetimeSuffix = new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').split('.')[0];
const resultFolderName = `result-${datetimeSuffix}`;
createDirectoryIfNotExists(resultFolderPath);
const resultFolderFullPath = path.join(resultFolderPath, resultFolderName);
createDirectoryIfNotExists(resultFolderFullPath);
const devmanReportFilePath = path.join(resultFolderFullPath, 'devman_report.json');
const ctcReportFilePath = path.join(resultFolderFullPath, 'ctcscan_report.json');
const dockerReportFilePath = path.join(resultFolderFullPath, 'docker_scan_report.json');
await generateReport(devmanViolations, devmanReportFilePath);
await generateReport(ctcViolations, ctcReportFilePath);
await generateReport(dockerViolations, dockerReportFilePath);
// Generate HTML report
const htmlReportFilePath = path.join(resultFolderFullPath, 'report.html');
generateHtmlReport(devmanViolations, ctcViolations, dockerViolations, packageJson, datetimeSuffix, htmlReportFilePath);
vscode.window.showInformationMessage('Scan reports generated successfully!');
} catch (error) {
console.error('Error generating reports:', error);
vscode.window.showErrorMessage('Error generating reports. See the console for more information.');
}
});
context.subscriptions.push(disposable);
}
module.exports = {
activate,
getFilesToScan
};