Skip to content

Commit 95ba903

Browse files
committed
Create application.class.js
1 parent a1cc0f9 commit 95ba903

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/lib/application.class.js

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

0 commit comments

Comments
 (0)