Skip to content

Commit 8a12bcd

Browse files
committed
Update index.js added support for options
1 parent b3dc27f commit 8a12bcd

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

index.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
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 } from './src/urlList.js'
66

77
const execResult = (err = null, out, outerr = null) => {
88
let accessibilityScores = (!existsSync('./out/scores.json')) ? readFileSync('./src/scores.json') : readFileSync('./out/scores.json')
99

10-
const data = JSON.parse(out);
10+
const data = JSON.parse(out)
1111
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
1212
const URLaccessibilityScoreResult = data?.categories?.accessibility?.score
13+
1314
accessibilityScoresJSON[data.requestedUrl] = URLaccessibilityScoreResult
15+
1416
const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
17+
1518
if (!existsSync('./out/')) mkdirSync('./out/')
19+
20+
if (!existsSync('./out/logs')) mkdirSync('./out/logs')
21+
const rawOutputFilename = `./out/logs/${data.requestedUrl.split('.')[1]}_${Date.now()}.json`
22+
23+
writeFileSync(rawOutputFilename, JSON.stringify(data), { flag: 'w' })
1624
return writeFileSync('./out/scores.json', newAccessibilityJSON, { flag: 'w' })
1725
}
1826

19-
const testURL = (urlToCheck) => {
20-
const commandToRun = `lighthouse ${urlToCheck} --only-categories=accessibility --output json`
21-
27+
const testURL = (urlToCheck, options = {}) => {
28+
const commandToRun = `lighthouse ${urlToCheck} --only-categories=${lighthouseCategories(options?.categories ?? "accessibility")} --output json`
29+
console.log(`running command: ${commandToRun}`)
2230
series([
2331
() => exec(commandToRun, execResult)
2432
])
2533
}
2634

27-
urlList.forEach(url => testURL(url))
35+
const lighthouseCategories = (categories = []) => {
36+
if (typeof categories == "string") return categories
37+
return categories.join(',')
38+
}
39+
40+
urlList.forEach(url => testURL(url,
41+
{
42+
"categories": [
43+
"accessibility",
44+
"performance"
45+
]
46+
})
47+
)

0 commit comments

Comments
 (0)