From b3a5f2f0e7fdfa70956bd96515a037f514ed691d Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Tue, 2 May 2023 12:15:21 +0200 Subject: [PATCH] fix: solve issue with default lang and when reports option is undefined --- src/main.js | 10 ++++++++-- src/reporters/generatorReports.js | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main.js b/src/main.js index d3a2c84..84f9f84 100644 --- a/src/main.js +++ b/src/main.js @@ -11,6 +11,13 @@ const defaultThreshold = { fail: 30 }; +const formatReports = reports => { + if(!reports){ + return []; + } + return Array.isArray(reports) ? reports : [reports]; +}; + module.exports = async (_options) => { let options = { ...defaultThreshold, @@ -29,7 +36,7 @@ module.exports = async (_options) => { const resultsGlobal = aggregatorGlobalService(options, resultsGlobalLighthouse, resultsGlobalEcoindex); - const reports = Array.isArray(options.reports) ? options.reports : [options.reports]; + const reports = formatReports(options.reports); const destFolder = path.join(process.cwd(), options.outputPath ?? "globalReports"); if(fs.existsSync(destFolder)){ fs.rmSync(destFolder, { recursive: true }); @@ -37,7 +44,6 @@ module.exports = async (_options) => { fs.mkdirSync(destFolder, { recursive: true }); options.outputPath = destFolder; - await Promise.all(reports.map(report => { if(typeof report !== "string"){ return report(options, resultsGlobal); diff --git a/src/reporters/generatorReports.js b/src/reporters/generatorReports.js index 3b0115c..f929f94 100644 --- a/src/reporters/generatorReports.js +++ b/src/reporters/generatorReports.js @@ -3,7 +3,7 @@ const path = require("path"); const folderTranslate = "translate"; const minify = require("html-minifier").minify; const fse = require("fs-extra"); - +const defaultLang = "en-GB"; const { globalNoteTag, globalPerformanceTag, @@ -108,8 +108,9 @@ const generateReports = async (options, results) => { console.log("Generate reports html."); } if (!options.lang) { - options.lang = "en-GB"; + options.lang = defaultLang; } + options.translations = populateTranslation(options); if (options.srcLighthouse) { @@ -386,16 +387,21 @@ const populateTemplateEcoIndex = (options, ecoIndex, numberPage) => { }; const populateTranslation = options => { - const templateFile = `${options.lang}.json`; + const i18nFile = `${options.lang}.json`; if (options?.verbose) { - console.log("Translate by files:", templateFile); + console.log("Translate by files:", i18nFile); } - const templatePath = path.join(__dirname, folderTranslate, templateFile); + const templatePath = path.join(__dirname, folderTranslate, i18nFile); if (fs.existsSync(templatePath)) { return require(templatePath); } - return require(dafaultPath); + + if (options?.verbose) { + console.log(`The file ${i18nFile} does not exist. We will use the default one.`); + } + + return populateTranslation({ ...options, lang: defaultLang}); }; const generateCSSClassBasedOnValue = (value, { pass, fail }) => {