Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used Page Speed Insight to run Lighthouse audit #522

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions api/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ app.post('/scanresult/:api/:buildId', async (req, res) => {
performanceScore: lhr.categories.performance.score,
accessibilityScore: lhr.categories.accessibility.score,
bestPracticesScore: lhr.categories['best-practices'].score,
seoScore: lhr.categories.seo.score,
pwaScore: lhr.categories.pwa.score,
seoScore: lhr.categories.seo.score
};
}

Expand Down
2 changes: 0 additions & 2 deletions docker/customHtmlRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ exports.addCustomHtmlRule = () => {
if (event.tagName !== "code" && event.tagName !== "a") {
if (event.lastEvent.raw) {
if (re.test(event.lastEvent.raw.toLowerCase())) {
console.log('BRUHHHHH')
console.log(event)
reporter.warn(
"Page must not show email addresses.",
event.line,
Expand Down
16 changes: 5 additions & 11 deletions docker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ const {
processBrokenLinks,
getFinalEval,
sendAlertEmail,
convertSpecialCharUrl
convertSpecialCharUrl,
runLighthouseReport
} = require("./utils");

const { readGithubSuperLinter } = require("./parseSuperLinter");

const LIGHTHOUSEFOLDER = "./.lighthouseci/";
const LIGHTHOUSEFOLDER = "./lhr.json";
const ARTILLERYFOLDER = "./artilleryOut.json";

const PACKAGE_CONFIG = require('./package-lock.json');
Expand All @@ -51,7 +52,7 @@ const _getAgrs = () => {
alias: "t",
describe: "Dashboard token (sign up at https://codeauditor.com/)",
type: "string",
demandOption: true,
demandOption: false,
})
.option("buildId", {
describe: "Build/Run number, e.g. CI Build number",
Expand Down Expand Up @@ -181,14 +182,7 @@ const main = async () => {
// Lighthouse
if (options.lighthouse) {
writeLog(`start lighthouse`);
try {
const rs = execSync(
`./node_modules/.bin/lhci collect --url="${options.url}" -n 1`
).toString();
writeLog(`lighthouse check finished`, rs);
} catch (e) {
writeLog(`lighthouse check failed`, e);
}
await runLighthouseReport(options.url);
}

// Artillery
Expand Down
8 changes: 4 additions & 4 deletions docker/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"htmlhint": "^0.11.0",
"minimatch": "^3.1.2",
"mocha": "^9.2.2",
"node-fetch": "^2.6.11",
"node-fetch": "^2.6.12",
"node-fetch-cookies": "^1.5.0",
"nodemailer": "^6.9.3",
"object-assign": "^4.1.1",
Expand Down
63 changes: 39 additions & 24 deletions docker/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { execSync } = require("child_process");
const slug = require("slug");
const nodemailer = require("nodemailer");
const fns = require('date-fns')
const fetch = require('node-fetch')

exports.sendAlertEmail = async (email, emailConfig, scanSummary) => {
// create reusable transporter object using the default SMTP transport
Expand Down Expand Up @@ -255,42 +256,65 @@ exports.runBrokenLinkCheck = (url, maxthread) => {
}
};

/**
* run Lighthouse Report
* @param {string} url
*/
exports.runLighthouseReport = async (url) => {
const categories = ["accessibility","best_practices","performance","SEO"]
const lighthouseUrl = new URL(
'https://www.googleapis.com/pagespeedonline/v5/runPagespeed'
)
lighthouseUrl.searchParams.append('key', 'AIzaSyBGZtZ045o_LRrs3Wgk4MvrDabMqMjHFnQ')
lighthouseUrl.searchParams.append('url', url)
lighthouseUrl.searchParams.append('strategy', 'mobile')
for (const category of categories) {
lighthouseUrl.searchParams.append('category', category.toUpperCase())
}
const response = await fetch(lighthouseUrl)
if (response.ok) {
const data = await response.json()
const lighthouseResult = data.lighthouseResult
var json = JSON.stringify(lighthouseResult);
if (lighthouseResult) {
fs.writeFile('lhr.json', json, 'utf8', err => {
if (err) {
console.error(err);
}
});
}
}
}

/**
* parse Lighthouse Report
* @param {string} folder - .lighthouseci folder
* @param {string} folder - lhr file
* @param {func} writeLog - logging method
*/
exports.readLighthouseReport = (folder, writeLog) => {
if (!fs.existsSync(folder)) {
console.log(
'ERROR => No lighthouse report found. Run again with `-v "%.LIGHTHOUSECI%:/usr/app/.lighthouseci"` option'
'ERROR => No lighthouse report found'
);
return [null, null];
}

writeLog(`Reading Lighthouse report files`);
let lhFiles = fs.readdirSync(folder);

if (lhFiles.filter((x) => x.endsWith(".json")).length === 0) {
return [null, null];
}

const jsonReport = lhFiles.filter((x) => x.endsWith(".json")).splice(-1)[0];
const lhr = JSON.parse(fs.readFileSync(`${folder}${jsonReport}`).toString());
const lhr = JSON.parse(fs.readFileSync(`lhr.json`).toString());

const lhrSummary = {
performanceScore: lhr.categories.performance.score,
accessibilityScore: lhr.categories.accessibility.score,
bestPracticesScore: lhr.categories["best-practices"].score,
seoScore: lhr.categories.seo.score,
pwaScore: lhr.categories.pwa.score,
};
return [lhr, lhrSummary];
};

/**
* parse Artillery Report
* @param {string} folder - .lighthouseci folder
* @param {string} folder - Artillery report file
* @param {func} writeLog - logging method
*/
exports.readArtilleryReport = (folder, writeLog) => {
Expand Down Expand Up @@ -553,16 +577,14 @@ exports.printResultsToConsole = (
// output Lighthouse Score Box
lhScaled = {
performanceScore: Math.round(lh.performanceScore * 100),
pwaScore: Math.round(lh.pwaScore * 100),
seoScore: Math.round(lh.seoScore * 100),
accessibilityScore: Math.round(lh.accessibilityScore * 100),
bestPracticesScore: Math.round(lh.bestPracticesScore * 100),
average: Math.round(
((lh.performanceScore +
lh.seoScore +
lh.bestPracticesScore +
lh.accessibilityScore +
lh.pwaScore) /
lh.accessibilityScore) /
5) *
100
),
Expand All @@ -572,7 +594,6 @@ exports.printResultsToConsole = (
let strPerformance = "Performance: ";
let strSeo = "SEO: ";
let strBP = "Best practices: ";
let strPwa = "PWA: ";

let avg = chalk(
`${strAvg.padEnd(10)} ${lhScaled.average.toString().padStart(10)} / 100`
Expand All @@ -590,11 +611,8 @@ exports.printResultsToConsole = (
.toString()
.padStart(4)} / 100`
);
let pwa = chalk(
`${strPwa.padEnd(10)} ${lhScaled.pwaScore.toString().padStart(10)} / 100`
);

consoleBox([avg, performance, bestPractice, seo, pwa], "green");
consoleBox([avg, performance, bestPractice, seo], "green");
}

if (atrSummary) {
Expand Down Expand Up @@ -740,16 +758,14 @@ exports.getFinalEval = (
// output Lighthouse Score Box
lhScaled = {
performanceScore: Math.round(lh.performanceScore * 100),
pwaScore: Math.round(lh.pwaScore * 100),
seoScore: Math.round(lh.seoScore * 100),
accessibilityScore: Math.round(lh.accessibilityScore * 100),
bestPracticesScore: Math.round(lh.bestPracticesScore * 100),
average: Math.round(
((lh.performanceScore +
lh.seoScore +
lh.bestPracticesScore +
lh.accessibilityScore +
lh.pwaScore) /
lh.accessibilityScore) /
5) *
100
),
Expand All @@ -767,7 +783,6 @@ exports.getFinalEval = (
(reqThreshold.bestPracticesScore &&
lhScaled.bestPracticesScore < reqThreshold.bestPracticesScore) ||
(reqThreshold.seoScore && lhScaled.seoScore < reqThreshold.seoScore) ||
(reqThreshold.pwaScore && lhScaled.pwaScore < reqThreshold.pwaScore) ||
(reqThreshold.average && lhScaled.average < reqThreshold.average)
) {
consoleBox(
Expand All @@ -777,7 +792,7 @@ exports.getFinalEval = (
reqThreshold.accessibilityScore
} Best practices=${reqThreshold.bestPracticesScore} SEO=${
reqThreshold.seoScore
} PWA=${reqThreshold.pwaScore} !!!`,
}`,
"red"
);
failedThreshold = true;
Expand Down
23 changes: 0 additions & 23 deletions ui/public/build/bundle.css

This file was deleted.

Loading