Skip to content

Commit ed97826

Browse files
authored
Syntaxing Class-based implementation and legacy support (#10)
Syntaxing `Class-based` implementation and legacy support
2 parents 28d5268 + c7b7cd8 commit ed97826

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

src/lib/application.class.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ import { series } from "async"
22
import { exec } from 'child_process'
33
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'
44
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-
}
5+
import Command from "./commandHandlers.class.js"
146

157
export default class ThesisLighthouse {
168
options
@@ -20,20 +12,10 @@ export default class ThesisLighthouse {
2012
this.options = appOptions
2113
this.execOptions = execOptions
2214
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) })
3315
}
3416

3517
testURL(urlToCheck, options = {}) {
36-
const { commandToRun } = makeCommandFromURL(urlToCheck, options)
18+
const { commandToRun } = Command.make(urlToCheck, options)
3719
if (options?.consoleLog ?? true) console.log(`Running Test on ${urlToCheck}`)
3820

3921
series([
@@ -78,4 +60,16 @@ export default class ThesisLighthouse {
7860
writeFileSync(rawOutputFilename, JSON.stringify(data), { flag: 'w' })
7961
return writeFileSync('./out/scores.json', newAccessibilityJSON, { flag: 'w' })
8062
}
63+
64+
start() {
65+
if (this.options?.consoleLog) console.log(this.options)
66+
67+
const isOptionsCategories = getCategoriesFlags(this.options?.categories)
68+
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"`
69+
70+
console.log(`ThesisLighthouse ${process.env.npm_package_version} - Thesis Example Code`)
71+
console.log(`Running with these flags: ${currentFlags}\n`)
72+
73+
this.urlList.forEach((url, index) => { this.testURL(url, this.options) })
74+
}
8175
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
import Utility from './utilities.class.js'
22

33
class Categories {
4-
getCategoriesFlags = array => {
5-
return (!Array.isEmpty(array) ?? false) ? `--only-categories=${lighthouseCategories(array ?? "accessibility")} ` : ""
4+
// getCategoriesFlags = array => {
5+
getFlags = array => {
6+
return (!Utility.isEmpty(array) ?? false) ? `--only-categories=${lighthouseCategories(array ?? "accessibility")} ` : ""
67
}
78

8-
getCurrentCategories = categories => {
9+
// getCurrentCategories = categories => {
10+
getCurrent = categories => {
911
return categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"]
1012
}
1113

12-
lighthouseCategories = (categories = []) => {
14+
// lighthouseCategories = (categories = []) => {
15+
lighthouse = (categories = []) => {
1316
return (typeof categories == "string") ? categories : categories.join(',')
1417
}
1518

16-
getCategoriesFromCategoriesFlags = array => {
19+
// getCategoriesFromCategoriesFlags = array => {
20+
getFromFlags = array => {
1721
return (!Utility.isEmpty(array)) ? array : undefined
1822
}
1923
}
2024

21-
export const { getCategoriesFlags, lighthouseCategories, getCurrentCategories, getCategoriesFromCategoriesFlags } = new Categories()
22-
export default Categories
25+
export const {
26+
getFlags: getCategoriesFlags, // deprecated: only use this for non-class implementation
27+
lighthouse: lighthouseCategories, // deprecated: only use this for non-class implementation
28+
getCurrent: getCurrentCategories, // deprecated: only use this for non-class implementation
29+
getFromFlags: getCategoriesFromCategoriesFlags // deprecated: only use this for non-class implementation
30+
} = new Categories()
31+
export default new Categories()

src/lib/commandHandlers.class.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getCategoriesFlags } from "./categoriesHandlers.class.js"
2+
3+
class Command {
4+
make = (url, options) => {
5+
const isOptionsCategories = getCategoriesFlags(options?.categories)
6+
const currentFlags = `${isOptionsCategories}--output json --disable-full-page-screenshot --chrome-flags="--no-sandbox --headless --disable-gpu"`
7+
const commandToRun = `lighthouse ${url} ${currentFlags}`
8+
return { commandToRun, currentFlags }
9+
}
10+
}
11+
12+
export const {
13+
make: makeCommandFromURL // deprecated: only use this for non-class implementation
14+
} = new Command()
15+
export default new Command()

0 commit comments

Comments
 (0)