diff --git a/cli-core/reportJson.js b/cli-core/reportJson.js new file mode 100644 index 0000000..1c66253 --- /dev/null +++ b/cli-core/reportJson.js @@ -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 +} diff --git a/commands/analyse.js b/commands/analyse.js index 6cdee72..2fc250d 100644 --- a/commands/analyse.js +++ b/commands/analyse.js @@ -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 @@ -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); } @@ -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(); } @@ -136,4 +138,4 @@ function analyse(options) { analyse_core(options).catch(e=>console.error("ERROR : \n", e)) } -module.exports = analyse; \ No newline at end of file +module.exports = analyse;