Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit e98c765

Browse files
committed
Print stats and display category name only if violations exist
1 parent 4fe8f62 commit e98c765

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

index.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ const queue = require('async/queue')
66
const fs = require('fs')
77
const colors = require('colors')
88

9+
const stats = {
10+
pageCount: 0,
11+
violationCounts: {},
12+
passedAuditsCount: 0,
13+
startTime: null,
14+
auditTimesByPageUrl: {}
15+
}
16+
917
module.exports = (options) => {
18+
stats.startTime = new Date()
1019
const configPath = path.resolve(options.config)
1120
const config = JSON.parse(fs.readFileSync(configPath))
1221

@@ -39,6 +48,7 @@ module.exports = (options) => {
3948
})
4049
crawler.once('complete', () => {
4150
lighthouseQueue.drain = () => {
51+
printStats()
4252
if (totalErrorCount > 0) {
4353
process.exit(1)
4454
}
@@ -49,6 +59,7 @@ module.exports = (options) => {
4959
}
5060

5161
function runLighthouse (url, configPath, callback) {
62+
stats.pageCount++
5263
const args = [
5364
url,
5465
'--output=json',
@@ -67,7 +78,10 @@ function runLighthouse (url, configPath, callback) {
6778
lighthouse.stdout.on('data', (data) => {
6879
output += data
6980
})
81+
82+
stats.auditTimesByPageUrl[url] = {startTime: new Date()}
7083
lighthouse.once('close', () => {
84+
stats.auditTimesByPageUrl[url].endTime = new Date()
7185
let errorCount = 0
7286

7387
let report
@@ -80,22 +94,34 @@ function runLighthouse (url, configPath, callback) {
8094
}
8195

8296
report.reportCategories.forEach((category) => {
83-
console.log();
84-
console.log(category.name.bold.underline);
97+
let displayedCategory = false
8598
category.audits.forEach((audit) => {
86-
if (audit.score !== 100) {
99+
if (audit.score === 100) {
100+
stats.passedAuditsCount++
101+
} else {
102+
if (!displayedCategory) {
103+
console.log();
104+
console.log(category.name.bold.underline);
105+
displayedCategory = true
106+
}
87107
errorCount++
88108
console.log(url.replace(/\/$/, ''), '\u2717'.red, audit.id.bold, '-', audit.result.description.italic)
89109

110+
if (stats.violationCounts[category.name] === undefined) {
111+
stats.violationCounts[category.name] = 0
112+
}
113+
90114
if (audit.result.extendedInfo) {
91115
const {value} = audit.result.extendedInfo
92116
if (Array.isArray(value)) {
117+
stats.violationCounts[category.name] += value.length
93118
value.forEach((result) => {
94119
if (result.url) {
95120
console.log(` ${result.url}`)
96121
}
97122
})
98123
} else if (Array.isArray(value.nodes)) {
124+
stats.violationCounts[category.name] += value.nodes.length
99125
const messagesToNodes = {}
100126
value.nodes.forEach((result) => {
101127
let message = result.failureSummary
@@ -109,9 +135,11 @@ function runLighthouse (url, configPath, callback) {
109135
Object.keys(messagesToNodes).forEach((message) => {
110136
console.log(` ${message}`)
111137
messagesToNodes[message].forEach(node => {
112-
console.log(` ${node}`.dim)
138+
console.log(` ${node}`.gray)
113139
})
114140
})
141+
} else {
142+
stats.violationCounts[category.name]++
115143
}
116144
}
117145
}
@@ -121,3 +149,21 @@ function runLighthouse (url, configPath, callback) {
121149
callback(errorCount)
122150
})
123151
}
152+
153+
function printStats() {
154+
console.log();
155+
console.log();
156+
console.log('Lighthouse Summary'.bold.underline);
157+
console.log(` Total Pages Scanned: ${stats.pageCount}`);
158+
console.log(` Total Auditing Time: ${new Date() - stats.startTime} ms`);
159+
const totalTime = Object.keys(stats.auditTimesByPageUrl).reduce((sum, url) => {
160+
const {endTime, startTime} = stats.auditTimesByPageUrl[url]
161+
return (endTime - startTime) + sum
162+
}, 0)
163+
console.log(` Average Page Audit Time: ${Math.round(totalTime/stats.pageCount)} ms`);
164+
console.log(` Total Audits Passed: ${stats.passedAuditsCount}`, '\u2713'.green);
165+
console.log(` Total Violations:`);
166+
Object.keys(stats.violationCounts).forEach(category => {
167+
console.log(` ${category}: ${stats.violationCounts[category]}`, '\u2717'.red);
168+
})
169+
}

0 commit comments

Comments
 (0)