Skip to content

Commit

Permalink
Feature ExecOptions based on ./src/urlList.js and bunch of improv…
Browse files Browse the repository at this point in the history
…ement (#4)

Feature `ExecOptions` based on `./src/urlList.js` and bunch of improvement
  • Loading branch information
EmptyWork authored Jun 11, 2023
2 parents 8e8f741 + efbacce commit 22e2407
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_modules/
/out/
/src/urlList.js

.gitlog
.gitlog

test.js
40 changes: 26 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,68 @@
import { series } from 'async'
import { exec } from 'child_process'
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync, write, writeFile } from 'fs'
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs'
if (!existsSync('./src/urlList.js')) copyFileSync('./src/urlList.example', './src/urlList.js')
import { urlList, options } from './src/urlList.js'
import { urlList, options, execOptions } from './src/urlList.js'

Object.prototype.isEmpty = (obj) => {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
for (const prop in obj)
if (Object.hasOwn(obj, prop)) return false

return true;
return true
}

Array.prototype.isEmpty = (arr) => {
if (arr === undefined || arr.length === 0) return false
return true
}

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`
console.log(`command stopped: ${commandToRun}`)

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

optionCategories.forEach(category => {
let categoryScore = data?.categories[category].score

categoriesScoresObject[category] = categoryScore
})

accessibilityScoresJSON[data.requestedUrl] = categoriesScoresObject
accessibilityScoresJSON[data?.requestedUrl] = categoriesScoresObject

const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)

if (!existsSync('./out/')) mkdirSync('./out/')

if (!existsSync('./out/logs')) mkdirSync('./out/logs')
const rawOutputFilename = `./out/logs/${data.requestedUrl.split('.')[1]}_${Date.now()}.json`

const logFileNameBasedOnUrl = data?.requestedUrl.replace(/^(http|https):\/\/(www.|)/g, '').replaceAll("/", "").split('.').reverse().join('.')
const rawOutputFilename = `./out/logs/${logFileNameBasedOnUrl}-${optionCategories
.join('-')}-${Date.now()}.json`

writeFileSync(rawOutputFilename, JSON.stringify(data), { flag: 'w' })
return writeFileSync('./out/scores.json', newAccessibilityJSON, { flag: 'w' })
}

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

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

series([
() => exec(commandToRun, execResult),
() => exec(commandToRun, execOptions, execResult),
// () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
// () => exec("lighthouse --help", (err, stdout, stderr) => console.log(stdout))
])
}

Expand Down
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.1",
"version": "0.0.2",
"description": "a way to automatically use lighthouse to get accessibility report number",
"main": "index.js",
"type": "module",
Expand Down
9 changes: 7 additions & 2 deletions src/urlList.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ const _urlList = [
// you can add more links in here, but keep in mind, the more links you add
// the more resource it will use
"https://www.emptywork.my.id"
];
]

const _options = {
// "categories": []
}

export { _urlList as urlList, _options as options };
const execOptions = {
"maxBuffer": 2048 * 1024
}

export { _urlList as urlList, _options as options }

0 comments on commit 22e2407

Please sign in to comment.