Skip to content

Commit 91894c3

Browse files
committed
Update index.js added fully customize option for categories
1 parent bbc987a commit 91894c3

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

index.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@ import { series } from 'async'
22
import { exec } from 'child_process'
33
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync, write, writeFile } from 'fs'
44
if (!existsSync('./src/urlList.js')) copyFileSync('./src/urlList.example', './src/urlList.js')
5-
import { urlList } from './src/urlList.js'
5+
import { urlList, options } from './src/urlList.js'
6+
7+
Object.prototype.isEmpty = (obj) => {
8+
for (const prop in obj) {
9+
if (Object.hasOwn(obj, prop)) {
10+
return false;
11+
}
12+
}
13+
14+
return true;
15+
}
616

717
const execResult = (err = null, out, outerr = null) => {
18+
819
let accessibilityScores = (!existsSync('./out/scores.json')) ? readFileSync('./src/scores.json') : readFileSync('./out/scores.json')
920

1021
const data = JSON.parse(out)
1122
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
12-
const URLaccessibilityScoreResult = data?.categories?.accessibility?.score
23+
const categoriesScoresObject = {}
24+
const optionCategories = options.categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"];
25+
26+
optionCategories.forEach(category => {
27+
let categoryScore = data?.categories[category].score
1328

14-
accessibilityScoresJSON[data.requestedUrl] = URLaccessibilityScoreResult
29+
categoriesScoresObject[category] = categoryScore
30+
})
31+
32+
accessibilityScoresJSON[data.requestedUrl] = categoriesScoresObject
1533

1634
const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
1735

@@ -25,10 +43,14 @@ const execResult = (err = null, out, outerr = null) => {
2543
}
2644

2745
const testURL = (urlToCheck, options = {}) => {
28-
const commandToRun = `lighthouse ${urlToCheck} --only-categories=${lighthouseCategories(options?.categories ?? "accessibility")} --output json`
46+
const isOptionsCategories = (!Object.isEmpty(options.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")}` : ""
47+
const commandToRun = `lighthouse ${urlToCheck} ${isOptionsCategories} --output json`
48+
2949
console.log(`running command: ${commandToRun}`)
50+
3051
series([
31-
() => exec(commandToRun, execResult)
52+
() => exec(commandToRun, execResult),
53+
// () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
3254
])
3355
}
3456

@@ -37,11 +59,4 @@ const lighthouseCategories = (categories = []) => {
3759
return categories.join(',')
3860
}
3961

40-
urlList.forEach(url => testURL(url,
41-
{
42-
"categories": [
43-
"accessibility",
44-
"performance"
45-
]
46-
})
47-
)
62+
urlList.forEach(url => testURL(url, options))

0 commit comments

Comments
 (0)