Skip to content

Commit 2d3338e

Browse files
authored
Feature Libs create more robust system using Libraries (#5)
Feature `Libs` create more robust system using `Libraries`
2 parents 22e2407 + f1da68c commit 2d3338e

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { exec } from 'child_process'
33
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs'
44
if (!existsSync('./src/urlList.js')) copyFileSync('./src/urlList.example', './src/urlList.js')
55
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'
68

79
Object.prototype.isEmpty = (obj) => {
810
for (const prop in obj)
@@ -17,19 +19,17 @@ Array.prototype.isEmpty = (arr) => {
1719
}
1820

1921
const execResult = (err = null, out, outerr = null) => {
20-
const isOptionsCategories = (!Array.isEmpty(options?.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")} ` : ""
21-
2222
let accessibilityScores = (!existsSync('./out/scores.json')) ? readFileSync('./src/scores.json') : readFileSync('./out/scores.json')
2323

2424
const data = JSON.parse(out)
2525

26-
const commandToRun = `lighthouse ${data.requestedUrl} ${isOptionsCategories}--output json`
26+
const commandToRun = makeCommandFromURL(data?.requestedUrl, options)
2727
console.log(`command stopped: ${commandToRun}`)
2828

2929
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
3030
const categoriesScoresObject = {}
31-
const categories = (!Array.isEmpty(options?.categories)) ? options?.categories : undefined
32-
const optionCategories = categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"]
31+
const categories = getCategoriesFromCategoriesFlags(options?.categories)
32+
const optionCategories = getCurrentCategories(categories)
3333

3434
optionCategories.forEach(category => {
3535
let categoryScore = data?.categories[category].score
@@ -54,8 +54,7 @@ const execResult = (err = null, out, outerr = null) => {
5454
}
5555

5656
const testURL = (urlToCheck, options = {}) => {
57-
const isOptionsCategories = (!Array.isEmpty(options?.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")} ` : ""
58-
const commandToRun = `lighthouse ${urlToCheck} ${isOptionsCategories}--output json --disable-full-page-screenshot`
57+
const commandToRun = makeCommandFromURL(urlToCheck, options)
5958

6059
console.log(`running command: ${commandToRun}`)
6160

@@ -66,9 +65,4 @@ const testURL = (urlToCheck, options = {}) => {
6665
])
6766
}
6867

69-
const lighthouseCategories = (categories = []) => {
70-
if (typeof categories == "string") return categories
71-
return categories.join(',')
72-
}
73-
7468
urlList.forEach(url => testURL(url, options))

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lighthouse-link-thesis",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "a way to automatically use lighthouse to get accessibility report number",
55
"main": "index.js",
66
"type": "module",

src/lib/categoriesHandlers.js

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 }

src/lib/commandHandlers.js

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

0 commit comments

Comments
 (0)