Skip to content

Commit 011c5d3

Browse files
committed
Add JSON report format
Close #18
1 parent 17338d1 commit 011c5d3

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

cli-core/reportJson.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const ProgressBar = require('progress');
4+
5+
//create json report for all the analysed pages and recap
6+
async function create_JSON_report(reportObject, options){
7+
//Path of the output file
8+
let OUTPUT_FILE = path.resolve(options.report_output_file);
9+
if (OUTPUT_FILE.toLowerCase().endsWith('results.xlsx')) {
10+
OUTPUT_FILE = OUTPUT_FILE.substring(0, OUTPUT_FILE.length-'.xlsx'.length) + '.json'
11+
}
12+
if (!OUTPUT_FILE.toLowerCase().endsWith('.json')) {
13+
throw ` report_output_file : File "${OUTPUT_FILE}" does not end with the ".json" extension.`
14+
}
15+
16+
const fileList = reportObject.reports;
17+
const globalReport = reportObject.globalReport;
18+
19+
//initialise progress bar
20+
let progressBar;
21+
if (!options.ci){
22+
progressBar = new ProgressBar(' Create Json report [:bar] :percent Remaining: :etas Time: :elapseds', {
23+
complete: '=',
24+
incomplete: ' ',
25+
width: 40,
26+
total: fileList.length+2
27+
});
28+
progressBar.tick()
29+
} else {
30+
console.log('Creating JSON report ...');
31+
}
32+
33+
let global = JSON.parse(fs.readFileSync(globalReport.path).toString());
34+
let pages = {};
35+
if (progressBar) progressBar.tick()
36+
37+
fileList.forEach((file)=>{
38+
pages[file.name] = JSON.parse(fs.readFileSync(file.path).toString());
39+
if (progressBar) progressBar.tick()
40+
})
41+
42+
try {
43+
fs.writeFileSync(OUTPUT_FILE, JSON.stringify({global, pages}));
44+
} catch (error) {
45+
throw ` report_output_file : Path "${OUTPUT_FILE}" cannot be reached.`
46+
}
47+
}
48+
49+
module.exports = {
50+
create_JSON_report
51+
}

commands/analyse.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const login = require('../cli-core/analysis.js').login;
77
const create_global_report = require('../cli-core/reportGlobal.js').create_global_report;
88
const create_XLSX_report = require('../cli-core/reportExcel.js').create_XLSX_report;
99
const create_html_report = require('../cli-core/reportHtml.js').create_html_report;
10+
const create_JSON_report = require("../cli-core/reportJson").create_JSON_report;
1011
const writeToInflux = require("../cli-core/influxdb").write;
1112

1213
//launch core
@@ -83,8 +84,9 @@ async function analyse_core(options) {
8384
await create_html_report(reportObj, options);
8485
} else if (reportFormat === 'influxdb') {
8586
await writeToInflux(reports, options);
86-
}
87-
else {
87+
} else if (reportFormat === 'json') {
88+
await create_JSON_report(reportObj, options);
89+
} else {
8890
await create_XLSX_report(reportObj, options);
8991
}
9092

@@ -117,7 +119,7 @@ function readHeaders(headersFile) {
117119

118120
function getReportFormat(format, filename) {
119121
// Check if format is defined
120-
const formats = ['xlsx', 'html', 'influxdb'];
122+
const formats = ['xlsx', 'html', 'influxdb', 'json'];
121123
if (format && formats.includes(format.toLowerCase())) {
122124
return format.toLowerCase();
123125
}
@@ -136,4 +138,4 @@ function analyse(options) {
136138
analyse_core(options).catch(e=>console.error("ERROR : \n", e))
137139
}
138140

139-
module.exports = analyse;
141+
module.exports = analyse;

0 commit comments

Comments
 (0)