Skip to content

Commit

Permalink
Add JSON report format
Browse files Browse the repository at this point in the history
Close #18
  • Loading branch information
MacFJA committed Nov 27, 2022
1 parent 17338d1 commit 011c5d3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
51 changes: 51 additions & 0 deletions cli-core/reportJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
const path = require('path');
const ProgressBar = require('progress');

//create json report for all the analysed pages and recap
async function create_JSON_report(reportObject, options){
//Path of the output file
let OUTPUT_FILE = path.resolve(options.report_output_file);
if (OUTPUT_FILE.toLowerCase().endsWith('results.xlsx')) {
OUTPUT_FILE = OUTPUT_FILE.substring(0, OUTPUT_FILE.length-'.xlsx'.length) + '.json'
}
if (!OUTPUT_FILE.toLowerCase().endsWith('.json')) {
throw ` report_output_file : File "${OUTPUT_FILE}" does not end with the ".json" extension.`
}

const fileList = reportObject.reports;
const globalReport = reportObject.globalReport;

//initialise progress bar
let progressBar;
if (!options.ci){
progressBar = new ProgressBar(' Create Json report [:bar] :percent Remaining: :etas Time: :elapseds', {
complete: '=',
incomplete: ' ',
width: 40,
total: fileList.length+2
});
progressBar.tick()
} else {
console.log('Creating JSON report ...');
}

let global = JSON.parse(fs.readFileSync(globalReport.path).toString());
let pages = {};
if (progressBar) progressBar.tick()

fileList.forEach((file)=>{
pages[file.name] = JSON.parse(fs.readFileSync(file.path).toString());
if (progressBar) progressBar.tick()
})

try {
fs.writeFileSync(OUTPUT_FILE, JSON.stringify({global, pages}));
} catch (error) {
throw ` report_output_file : Path "${OUTPUT_FILE}" cannot be reached.`
}
}

module.exports = {
create_JSON_report
}
10 changes: 6 additions & 4 deletions commands/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const login = require('../cli-core/analysis.js').login;
const create_global_report = require('../cli-core/reportGlobal.js').create_global_report;
const create_XLSX_report = require('../cli-core/reportExcel.js').create_XLSX_report;
const create_html_report = require('../cli-core/reportHtml.js').create_html_report;
const create_JSON_report = require("../cli-core/reportJson").create_JSON_report;
const writeToInflux = require("../cli-core/influxdb").write;

//launch core
Expand Down Expand Up @@ -83,8 +84,9 @@ async function analyse_core(options) {
await create_html_report(reportObj, options);
} else if (reportFormat === 'influxdb') {
await writeToInflux(reports, options);
}
else {
} else if (reportFormat === 'json') {
await create_JSON_report(reportObj, options);
} else {
await create_XLSX_report(reportObj, options);
}

Expand Down Expand Up @@ -117,7 +119,7 @@ function readHeaders(headersFile) {

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

module.exports = analyse;
module.exports = analyse;

0 comments on commit 011c5d3

Please sign in to comment.