Skip to content

Commit 87bbceb

Browse files
committed
Merge branch 'development' of https://github.com/EmptyWork/lighthouse-accessibility-thesis into development
2 parents 7e3e9be + ee8c4a7 commit 87bbceb

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

legacy/index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { series } from 'async'
2+
import { exec } from 'child_process'
3+
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs'
4+
if (!existsSync('./legacy/src/urlList.js')) copyFileSync('./legacy/src/urlList.example', './legacy/src/urlList.js')
5+
import { urlList, options, execOptions } from './src/urlList.js'
6+
import { makeCommandFromURL } from './src/lib/commandHandlers.js'
7+
import { getCategoriesFromCategoriesFlags, getCurrentCategories } from './src/lib/categoriesHandlers.js'
8+
import { getCategoriesFlags } from "./src/lib/categoriesHandlers.js"
9+
10+
Object.prototype.isEmpty = (obj) => {
11+
for (const prop in obj)
12+
if (Object.hasOwn(obj, prop)) return false
13+
14+
return true
15+
}
16+
17+
Array.prototype.isEmpty = (arr) => {
18+
return (arr === undefined || arr.length === 0) ? false : true
19+
}
20+
21+
const execResult = (err = null, out, outerr = null) => {
22+
let accessibilityScores = (!existsSync('./out/scores.json')) ? readFileSync('./src/scores.json') : readFileSync('./out/scores.json')
23+
24+
const data = JSON.parse(out)
25+
26+
const { commandToRun } = makeCommandFromURL(data?.requestedUrl, options)
27+
if (options?.consoleLog ?? true) console.log(`Stopped Test on ${data?.requestedUrl}`)
28+
29+
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
30+
const categoriesScoresObject = {}
31+
const categories = getCategoriesFromCategoriesFlags(options?.categories)
32+
const optionCategories = getCurrentCategories(categories)
33+
34+
optionCategories.forEach(category => {
35+
let categoryScore = data?.categories[category].score
36+
37+
categoriesScoresObject[category] = categoryScore
38+
})
39+
40+
accessibilityScoresJSON[data?.requestedUrl] = categoriesScoresObject
41+
42+
const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
43+
44+
if (!existsSync('./out/')) mkdirSync('./out/')
45+
46+
if (!existsSync('./out/logs')) mkdirSync('./out/logs')
47+
48+
const REGEX_HTTPS_HTTP = /^(http|https):\/\/(www.|)/g
49+
50+
const logFileNameBasedOnUrl = data?.requestedUrl.replace(REGEX_HTTPS_HTTP, '').replaceAll("/", "").split('.').reverse().join('.')
51+
const rawOutputFilename = `./out/logs/${logFileNameBasedOnUrl}-${optionCategories
52+
.join('-')}-${Date.now()}.json`
53+
54+
writeFileSync(rawOutputFilename, JSON.stringify(data), { flag: 'w' })
55+
return writeFileSync('./out/scores.json', newAccessibilityJSON, { flag: 'w' })
56+
}
57+
58+
const testURL = (urlToCheck, options = {}) => {
59+
const { commandToRun } = makeCommandFromURL(urlToCheck, options)
60+
if (options?.consoleLog ?? true) console.log(`Running Test on ${urlToCheck}`)
61+
62+
series([
63+
() => exec(commandToRun, execOptions, execResult),
64+
// () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
65+
// () => exec("lighthouse --help", (err, stdout, stderr) => console.log(stdout))
66+
])
67+
}
68+
69+
const main = (urlList, options) => {
70+
const isOptionsCategories = getCategoriesFlags(options?.categories)
71+
const currentFlags = `${isOptionsCategories}--output json --disable-full-page-screenshot --chrome-flags="--no-sandbox --headless --disable-gpu"`
72+
73+
console.log(`[Legacy] TLighthouse ${process.env.npm_package_version} - Thesis Example Code`)
74+
console.log(`Running with these Flags: ${currentFlags}\n`)
75+
76+
urlList.forEach((url, index) => { testURL(url, options) })
77+
}
78+
79+
main(urlList, options)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
const getCategoriesFlags = array => {
3+
return (!Array.isEmpty(array) ?? false) ? `--only-categories=${lighthouseCategories(array ?? "accessibility")} ` : ""
4+
}
5+
6+
const getCurrentCategories = categories => {
7+
return categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"]
8+
}
9+
10+
const lighthouseCategories = (categories = []) => {
11+
return (typeof categories == "string") ? categories : categories.join(',')
12+
}
13+
14+
const getCategoriesFromCategoriesFlags = array => {
15+
return (!Array.isEmpty(array)) ? array : undefined
16+
}
17+
18+
export { getCategoriesFlags, lighthouseCategories, getCurrentCategories, getCategoriesFromCategoriesFlags }

legacy/src/lib/commandHandlers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getCategoriesFlags } from "./categoriesHandlers.js"
2+
3+
const makeCommandFromURL = (url, options) => {
4+
const isOptionsCategories = getCategoriesFlags(options?.categories)
5+
const currentFlags = `${isOptionsCategories}--output json --disable-full-page-screenshot --chrome-flags="--no-sandbox --headless --disable-gpu"`
6+
const commandToRun = `lighthouse ${url} ${currentFlags}`
7+
return { commandToRun, currentFlags }
8+
}
9+
10+
export { makeCommandFromURL }

legacy/src/urlList.example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const _urlList = [
2+
// you can add more links in here, but keep in mind, the more links you add
3+
// the more resource it will use
4+
"https://www.emptywork.my.id"
5+
]
6+
7+
const _options = {
8+
// "categories": []
9+
}
10+
11+
const execOptions = {
12+
"maxBuffer": 2048 * 1024
13+
}
14+
15+
export { _urlList as urlList, _options as options }

0 commit comments

Comments
 (0)