Skip to content

Commit

Permalink
Merge pull request #25 from AxaFrance/fix/issue
Browse files Browse the repository at this point in the history
fix: solve issue with default lang and when reports option is undefined
  • Loading branch information
sandydelhoute authored May 2, 2023
2 parents 53387c7 + b3a5f2f commit 0adf92b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,15 +36,14 @@ 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 });
}
fs.mkdirSync(destFolder, { recursive: true });
options.outputPath = destFolder;


await Promise.all(reports.map(report => {
if(typeof report !== "string"){
return report(options, resultsGlobal);
Expand Down
18 changes: 12 additions & 6 deletions src/reporters/generatorReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 }) => {
Expand Down

0 comments on commit 0adf92b

Please sign in to comment.