Skip to content

Commit

Permalink
fix(lighthouse): permet de mettre le lien en relatif
Browse files Browse the repository at this point in the history
  • Loading branch information
titiBeOne committed Apr 26, 2023
1 parent a02b1de commit 802c19c
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 55 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\src\\cli.js",
"args": ["--srcLighthouse", "./example/lighthouse", "--srcEcoIndex","./example/ecoindex" ,"--reports","html", "--outputPath" , "./example/report_final"]
}
]
}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
".eslintrc.js"
],
"scripts": {
"start": "node src/cli.js --srcLighthouse=./example/lighthouse --srcEcoIndex=./example/ecoindex --reports=html --outputPath=./example/report_final",
"lint": "eslint",
"test": "jest --coverage",
"prepare": "husky install"
Expand Down
41 changes: 29 additions & 12 deletions src/lighthouse/aggregatorService.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs");
const path = require("path");

module.exports = async (options) => {
module.exports = async options => {
if (!options.srcLighthouse || !fs.existsSync(options.srcLighthouse)) {
console.error("lighthouse folder not found!");
process.exit(1);
Expand All @@ -17,47 +17,64 @@ const readFiles = (options, lighthouseJsonReportsFiles) => {
let globalBestPractices = 0;
const perPages = [];

lighthouseJsonReportsFiles.forEach((fileName) => {
lighthouseJsonReportsFiles.forEach(fileName => {
const pageName = fileName.replace(".json", "");
const pathFile = path.join(options.srcLighthouse, fileName);
const data = fs.readFileSync(pathFile);
const result = JSON.parse(data);
const performance = result?.categories?.performance?.score ? Math.round(result.categories.performance.score * 100) : 0;
const accessibility = result?.categories?.accessibility?.score ? Math.round(result?.categories.accessibility.score * 100) : 0;
const bestPractices = result?.categories["best-practices"]?.score ? Math.round(result?.categories["best-practices"].score * 100) : 0;
const performance = result?.categories?.performance?.score
? Math.round(result.categories.performance.score * 100)
: 0;
const accessibility = result?.categories?.accessibility?.score
? Math.round(result?.categories.accessibility.score * 100)
: 0;
const bestPractices = result?.categories["best-practices"]?.score
? Math.round(result?.categories["best-practices"].score * 100)
: 0;

globalPerformance += performance;
globalAccessibility += accessibility;
globalBestPractices += bestPractices;
perPages.push({
pageName,
lighthouseReport: path.join(options.srcLighthouse, `${pageName}.html`),
lighthouseReport: path.join("lighthouse", `${pageName}.html`),
accessibility,
bestPractices,
performance,
});
});
if (globalPerformance !== 0) {
globalPerformance = Math.ceil(globalPerformance / lighthouseJsonReportsFiles.length);
globalPerformance = Math.ceil(
globalPerformance / lighthouseJsonReportsFiles.length
);
}
if (globalAccessibility !== 0) {
globalAccessibility = Math.ceil(globalAccessibility / lighthouseJsonReportsFiles.length);
globalAccessibility = Math.ceil(
globalAccessibility / lighthouseJsonReportsFiles.length
);
}
if (globalBestPractices !== 0) {
globalBestPractices = Math.ceil(globalBestPractices / lighthouseJsonReportsFiles.length);
globalBestPractices = Math.ceil(
globalBestPractices / lighthouseJsonReportsFiles.length
);
}

if (options.verbose) {
console.log("global performance:", globalPerformance);
console.log("global accessibility:", globalAccessibility);
console.log("global bestPractices:", globalBestPractices);
}
return { performance: globalPerformance, accessibility: globalAccessibility, bestPractices: globalBestPractices, perPages };
return {
performance: globalPerformance,
accessibility: globalAccessibility,
bestPractices: globalBestPractices,
perPages,
};
};
const listFiles = (options) => {
const listFiles = options => {
const lighthouseJsonReportsFiles = [];
const files = fs.readdirSync(options.srcLighthouse);
files.forEach((file) => {
files.forEach(file => {
if (path.extname(file) === ".json") {
if (options.verbose) {
console.log("Add file in list for aggregation: ", file);
Expand Down
21 changes: 10 additions & 11 deletions src/lighthouse/aggregatorService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@ const path = require("path");
jest.mock("fs");

describe("AggregatorService", () => {

const inputValue = {
categories: {
performance: {
score: 0.76
score: 0.76,
},
accessibility: {
score: 0.60
score: 0.6,
},
"best-practices": {
score: 0.78
}
}
score: 0.78,
},
},
};

it("should return the lightouse output", async () => {
const options = { srcLighthouse: "test", verbose: true };
const output =
{
const output = {
performance: 76,
accessibility: 60,
bestPractices: 78,
perPages: [
{
pageName: "foo",
lighthouseReport: path.join("test", "foo.html"),
lighthouseReport: path.join("lighthouse", "foo.html"),
performance: 76,
accessibility: 60,
bestPractices: 78,
},
{
pageName: "bar",
lighthouseReport: path.join("test", "bar.html"),
lighthouseReport: path.join("lighthouse", "bar.html"),
performance: 76,
accessibility: 60,
bestPractices: 78,
}]
},
],
};
jest.spyOn(fs, "readdirSync").mockImplementation(() => {
return ["foo.json", "bar.json"];
Expand Down
2 changes: 1 addition & 1 deletion src/reporters/__snapshots__/generatorReports.spec.js.snap

Large diffs are not rendered by default.

68 changes: 37 additions & 31 deletions src/reporters/generatorReports.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ const { statusGreen } = require("./utils/statusGreen");
const { statusPerPage } = require("./utils/statusPerPage");
const { basename } = require("path");



const generateReportsSonar = async (options, results) => {
if (!options.sonarFilePath) {
console.error("You should define the sonarFilePath property");
process.exit(1);
}

const issues = [];

const addIssues = (value, ruleId, name) => {
Expand Down Expand Up @@ -80,10 +78,21 @@ const generateReportsSonar = async (options, results) => {

addIssues(results.ecoIndex, "eco-index-below-threshold", "ecoindex");
addIssues(results.performance, "performance-below-threshold", "performance");
addIssues(results.accessibility, "accessibility-below-threshold", "accessibility");
addIssues(results.bestPractices, "bestPractices-below-threshold", "bestPractices");
addIssues(
results.accessibility,
"accessibility-below-threshold",
"accessibility"
);
addIssues(
results.bestPractices,
"bestPractices-below-threshold",
"bestPractices"
);

fs.writeFileSync(path.join(options.outputPath, "report.json"), JSON.stringify({ issues }));
fs.writeFileSync(
path.join(options.outputPath, "report.json"),
JSON.stringify({ issues })
);
};

const generateReports = async (options, results) => {
Expand All @@ -98,34 +107,29 @@ const generateReports = async (options, results) => {
if (options?.verbose) {
console.log("Generate reports html.");
}
if(!options.lang){
if (!options.lang) {
options.lang = "en-GB";
}
options.translations = populateTranslation(options);

if(options.srcLighthouse){
const finalSrcLighthouse = options.outputPath + "/" + basename(options.srcLighthouse);
fse.copySync(options.srcLighthouse, finalSrcLighthouse, { overwrite: true });
if (options.srcLighthouse) {
const finalSrcLighthouse =
options.outputPath + "/" + basename(options.srcLighthouse);
fse.copySync(options.srcLighthouse, finalSrcLighthouse, {
overwrite: true,
});
options.srcLighthouse = finalSrcLighthouse;
}
if(options.srcEcoIndex){
const finalSrcEcoIndex = options.outputPath + "/" + basename(options.srcEcoIndex);
if (options.srcEcoIndex) {
const finalSrcEcoIndex =
options.outputPath + "/" + basename(options.srcEcoIndex);
fse.copySync(options.srcEcoIndex, finalSrcEcoIndex, { overwrite: true });
options.srcEcoIndex = finalSrcEcoIndex;
}
results.perPages = results.perPages.map(page => {
return {
...page,
lighthouseReport: page.lighthouseReport ? `${options.srcLighthouse}/${basename(page.lighthouseReport)}` : "",
};
});


const htmlPerPageResult = await populateTemplatePerPage(options, results);
let htmlResult = await populateTemplate(options, results, htmlPerPageResult);




const minifyOptions = {
includeAutoGeneratedTags: true,
removeAttributeQuotes: true,
Expand Down Expand Up @@ -189,7 +193,7 @@ const populateTemplate = async (options, results, htmlPerPageResult) => {
GlobalGreenItMetrics: GlobalGreenItMetricsTemplate,
Translations: options.translations,
style: readTemplate("./style.css"),
lang: options.lang
lang: options.lang,
});
};

Expand All @@ -199,10 +203,10 @@ const populateMetrics = (options, metric) => {
}
const template = readTemplate("templatePageMetrics.html");
const NumberOfRequestMetric =
metric?.find((m) => m.name === "number_requests") ?? {};
const PageSizeMetric = metric?.find((m) => m.name === "page_size") ?? {};
metric?.find(m => m.name === "number_requests") ?? {};
const PageSizeMetric = metric?.find(m => m.name === "page_size") ?? {};
const PageComplexityMetric =
metric?.find((m) => m.name === "Page_complexity") ?? {};
metric?.find(m => m.name === "Page_complexity") ?? {};

return ejs.render(template, {
Translations: options.translations,
Expand Down Expand Up @@ -252,19 +256,19 @@ const populateGreentItMetrics = (
gasesNumberOfVisits,
waterNumberOfVisits,
Translations: options.translations,
mySvg:{svgIconCo2:svgIconCo2,svgIconWater:svgIconWater},
mySvg: { svgIconCo2: svgIconCo2, svgIconWater: svgIconWater },
gasesNumberOfVisits,
waterNumberOfVisits,
Translations: options.translations,
mySvg:{svgIconCo2:svgIconCo2,svgIconWater:svgIconWater}
mySvg: { svgIconCo2: svgIconCo2, svgIconWater: svgIconWater },
});
};

const populateTemplatePerPage = async (options, results) => {
let htmlPerPage = "";
const defaultTemplatePerPage = readTemplate("templatePerPage.html");
let numberPage = 0;
results.perPages.forEach((page) => {
results.perPages.forEach(page => {
numberPage += 1;
if (options?.verbose) {
console.log("Populate reports page:", numberPage);
Expand Down Expand Up @@ -296,9 +300,11 @@ const populateTemplatePerPage = async (options, results) => {
waterShower: page.waterShower,
greenhouseGases: page.greenhouseGases,
water: page.water,
gasesNumberOfVisits: page.estimatation_water?.commentDetails?.numberOfVisit,
gasesNumberOfVisits:
page.estimatation_water?.commentDetails?.numberOfVisit,
waterNumberOfVisits: page.estimatation_co2?.commentDetails?.numberOfVisit,
});

const templatePerPage = ejs.render(defaultTemplatePerPage, {
Translations: options.translations,
[performanceBlock]: performanceBlockTemplate,
Expand Down Expand Up @@ -379,7 +385,7 @@ const populateTemplateEcoIndex = (options, ecoIndex, numberPage) => {
);
};

const populateTranslation = (options) => {
const populateTranslation = options => {
const templateFile = `${options.lang}.json`;

if (options?.verbose) {
Expand Down

0 comments on commit 802c19c

Please sign in to comment.