Skip to content

Commit

Permalink
Feature Libs create more robust system using Libraries (#5)
Browse files Browse the repository at this point in the history
Feature `Libs` create more robust system using `Libraries`
  • Loading branch information
EmptyWork authored Jun 12, 2023
2 parents 22e2407 + f1da68c commit 2d3338e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
18 changes: 6 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { exec } from 'child_process'
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs'
if (!existsSync('./src/urlList.js')) copyFileSync('./src/urlList.example', './src/urlList.js')
import { urlList, options, execOptions } from './src/urlList.js'
import { makeCommandFromURL } from './src/lib/commandHandlers.js'
import { getCategoriesFromCategoriesFlags, getCurrentCategories } from './src/lib/categoriesHandlers.js'

Object.prototype.isEmpty = (obj) => {
for (const prop in obj)
Expand All @@ -17,19 +19,17 @@ Array.prototype.isEmpty = (arr) => {
}

const execResult = (err = null, out, outerr = null) => {
const isOptionsCategories = (!Array.isEmpty(options?.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")} ` : ""

let accessibilityScores = (!existsSync('./out/scores.json')) ? readFileSync('./src/scores.json') : readFileSync('./out/scores.json')

const data = JSON.parse(out)

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

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

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

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

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

Expand All @@ -66,9 +65,4 @@ const testURL = (urlToCheck, options = {}) => {
])
}

const lighthouseCategories = (categories = []) => {
if (typeof categories == "string") return categories
return categories.join(',')
}

urlList.forEach(url => testURL(url, options))
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lighthouse-link-thesis",
"version": "0.0.2",
"version": "0.0.3",
"description": "a way to automatically use lighthouse to get accessibility report number",
"main": "index.js",
"type": "module",
Expand Down
18 changes: 18 additions & 0 deletions src/lib/categoriesHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

const getCategoriesFlags = array => {
return (!Array.isEmpty(array) ?? false) ? `--only-categories=${lighthouseCategories(array ?? "accessibility")} ` : ""
}

const getCurrentCategories = categories => {
return categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"]
}

const lighthouseCategories = (categories = []) => {
return (typeof categories == "string") ? categories : categories.join(',')
}

const getCategoriesFromCategoriesFlags = array => {
return (!Array.isEmpty(array)) ? array : undefined
}

export { getCategoriesFlags, lighthouseCategories, getCurrentCategories, getCategoriesFromCategoriesFlags }
8 changes: 8 additions & 0 deletions src/lib/commandHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getCategoriesFlags } from "./categoriesHandlers.js"

const makeCommandFromURL = (url, options) => {
const isOptionsCategories = getCategoriesFlags(options?.categories)
return `lighthouse ${url} ${isOptionsCategories}--output json --disable-full-page-screenshot`
}

export { makeCommandFromURL }

0 comments on commit 2d3338e

Please sign in to comment.