Skip to content

Commit 8e8f741

Browse files
authored
Feature Options:Categories based on ./src/urlList.js (#3)
Feature `Options:Categories` based on `./src/urlList.js`
2 parents b5e2ee9 + 91894c3 commit 8e8f741

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

clean.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { existsSync, rmSync } from 'fs';
1+
import { existsSync, rmSync } from 'fs'
22

33
const cleanDirectory = () => {
4-
if (!existsSync('./out/')) return console.error(`failed: dir out not exist`)
4+
if (!existsSync('./out/')) return console.error(`failed: dir out not exist`)
55
rmSync('./out/', { recursive: true })
66
console.log(`complete: dir out has been deleted`)
77
}

index.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
1-
import { series } from 'async';
2-
import { exec } from 'child_process';
3-
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs';
1+
import { series } from 'async'
2+
import { exec } from 'child_process'
3+
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

10-
const data = JSON.parse(out);
21+
const data = JSON.parse(out)
1122
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
12-
const URLaccessibilityScoreResult = data?.categories?.accessibility?.score
13-
accessibilityScoresJSON[data.requestedUrl] = URLaccessibilityScoreResult
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
28+
29+
categoriesScoresObject[category] = categoryScore
30+
})
31+
32+
accessibilityScoresJSON[data.requestedUrl] = categoriesScoresObject
33+
1434
const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
35+
1536
if (!existsSync('./out/')) mkdirSync('./out/')
37+
38+
if (!existsSync('./out/logs')) mkdirSync('./out/logs')
39+
const rawOutputFilename = `./out/logs/${data.requestedUrl.split('.')[1]}_${Date.now()}.json`
40+
41+
writeFileSync(rawOutputFilename, JSON.stringify(data), { flag: 'w' })
1642
return writeFileSync('./out/scores.json', newAccessibilityJSON, { flag: 'w' })
1743
}
1844

19-
const testURL = (urlToCheck) => {
20-
const commandToRun = `lighthouse ${urlToCheck} --only-categories=accessibility --output json`
45+
const testURL = (urlToCheck, options = {}) => {
46+
const isOptionsCategories = (!Object.isEmpty(options.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")}` : ""
47+
const commandToRun = `lighthouse ${urlToCheck} ${isOptionsCategories} --output json`
48+
49+
console.log(`running command: ${commandToRun}`)
2150

2251
series([
23-
() => exec(commandToRun, execResult)
52+
() => exec(commandToRun, execResult),
53+
// () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
2454
])
2555
}
2656

27-
urlList.forEach(url => testURL(url))
57+
const lighthouseCategories = (categories = []) => {
58+
if (typeof categories == "string") return categories
59+
return categories.join(',')
60+
}
61+
62+
urlList.forEach(url => testURL(url, options))

src/urlList.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ const _urlList = [
44
"https://www.emptywork.my.id"
55
];
66

7-
export { _urlList as urlList };
7+
const _options = {
8+
}
9+
10+
export { _urlList as urlList, _options as options };

0 commit comments

Comments
 (0)