Repository navigation
Expand file tree
/
Copy pathreporting.js
More file actions
125 lines (111 loc) · 2.84 KB
/
Copy pathreporting.js
File metadata and controls
125 lines (111 loc) · 2.84 KB
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
125
/**
* Reporting utilities for audit scripts
* Provides structured logging, JSON serialization, and report formatting
*/
import fs from 'fs';
import path from 'path';
export class Reporter {
constructor(options = {}) {
this.verbose = options.verbose || false;
this.outputDir = options.outputDir || './reports';
this.format = options.format || 'json';
this.reports = new Map();
}
/**
* Log a message with optional severity level
*/
log(message, level = 'info') {
const timestamp = new Date().toISOString();
const prefix = `[${timestamp}] [${level.toUpperCase()}]`;
console.log(`${prefix} ${message}`);
}
/**
* Log debug information (only if verbose mode enabled)
*/
debug(message) {
if (this.verbose) {
this.log(message, 'debug');
}
}
/**
* Create a new report section
*/
startReport(name, description = '') {
this.reports.set(name, {
name,
description,
startTime: new Date(),
entries: [],
stats: {},
});
}
/**
* Add an entry to the current report
*/
addEntry(reportName, entry) {
if (!this.reports.has(reportName)) {
this.startReport(reportName);
}
this.reports.get(reportName).entries.push(entry);
}
/**
* Update report statistics
*/
updateStats(reportName, stats) {
if (this.reports.has(reportName)) {
this.reports.get(reportName).stats = { ...this.reports.get(reportName).stats, ...stats };
}
}
/**
* Get report data
*/
getReport(reportName) {
return this.reports.get(reportName);
}
/**
* Save a report to file
*/
saveReport(reportName, filename) {
const report = this.getReport(reportName);
if (!report) {
this.log(`Report '${reportName}' not found`, 'error');
return false;
}
// Ensure output directory exists
if (!fs.existsSync(this.outputDir)) {
fs.mkdirSync(this.outputDir, { recursive: true });
}
const filepath = path.join(this.outputDir, filename || `${reportName}.json`);
const reportData = {
name: report.name,
description: report.description,
generatedAt: report.startTime.toISOString(),
completedAt: new Date().toISOString(),
stats: report.stats,
entries: report.entries,
};
try {
fs.writeFileSync(filepath, JSON.stringify(reportData, null, 2));
this.log(`Report saved: ${filepath}`, 'info');
return true;
} catch (error) {
this.log(`Failed to save report: ${error.message}`, 'error');
return false;
}
}
/**
* Generate summary report
*/
generateSummary() {
const summary = {
totalReports: this.reports.size,
reports: Array.from(this.reports.values()).map((r) => ({
name: r.name,
entryCount: r.entries.length,
stats: r.stats,
})),
};
return summary;
}
}
export default Reporter;