Skip to content

Commit 22e2407

Browse files
authored
Feature ExecOptions based on ./src/urlList.js and bunch of improvement (#4)
Feature `ExecOptions` based on `./src/urlList.js` and bunch of improvement
2 parents 8e8f741 + efbacce commit 22e2407

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ node_modules/
33
/out/
44
/src/urlList.js
55

6-
.gitlog
6+
.gitlog
7+
8+
test.js

index.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,68 @@
11
import { series } from 'async'
22
import { exec } from 'child_process'
3-
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync, write, writeFile } from 'fs'
3+
import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs'
44
if (!existsSync('./src/urlList.js')) copyFileSync('./src/urlList.example', './src/urlList.js')
5-
import { urlList, options } from './src/urlList.js'
5+
import { urlList, options, execOptions } from './src/urlList.js'
66

77
Object.prototype.isEmpty = (obj) => {
8-
for (const prop in obj) {
9-
if (Object.hasOwn(obj, prop)) {
10-
return false;
11-
}
12-
}
8+
for (const prop in obj)
9+
if (Object.hasOwn(obj, prop)) return false
1310

14-
return true;
11+
return true
12+
}
13+
14+
Array.prototype.isEmpty = (arr) => {
15+
if (arr === undefined || arr.length === 0) return false
16+
return true
1517
}
1618

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

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

2124
const data = JSON.parse(out)
25+
26+
const commandToRun = `lighthouse ${data.requestedUrl} ${isOptionsCategories}--output json`
27+
console.log(`command stopped: ${commandToRun}`)
28+
2229
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
2330
const categoriesScoresObject = {}
24-
const optionCategories = options.categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"];
31+
const categories = (!Array.isEmpty(options?.categories)) ? options?.categories : undefined
32+
const optionCategories = categories ?? ["accessibility", "pwa", "best-practices", "performance", "seo"]
2533

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

2937
categoriesScoresObject[category] = categoryScore
3038
})
3139

32-
accessibilityScoresJSON[data.requestedUrl] = categoriesScoresObject
40+
accessibilityScoresJSON[data?.requestedUrl] = categoriesScoresObject
3341

3442
const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
3543

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

3846
if (!existsSync('./out/logs')) mkdirSync('./out/logs')
39-
const rawOutputFilename = `./out/logs/${data.requestedUrl.split('.')[1]}_${Date.now()}.json`
47+
48+
const logFileNameBasedOnUrl = data?.requestedUrl.replace(/^(http|https):\/\/(www.|)/g, '').replaceAll("/", "").split('.').reverse().join('.')
49+
const rawOutputFilename = `./out/logs/${logFileNameBasedOnUrl}-${optionCategories
50+
.join('-')}-${Date.now()}.json`
4051

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

4556
const testURL = (urlToCheck, options = {}) => {
46-
const isOptionsCategories = (!Object.isEmpty(options.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")}` : ""
47-
const commandToRun = `lighthouse ${urlToCheck} ${isOptionsCategories} --output json`
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`
4859

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

5162
series([
52-
() => exec(commandToRun, execResult),
63+
() => exec(commandToRun, execOptions, execResult),
5364
// () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
65+
// () => exec("lighthouse --help", (err, stdout, stderr) => console.log(stdout))
5466
])
5567
}
5668

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.1",
3+
"version": "0.0.2",
44
"description": "a way to automatically use lighthouse to get accessibility report number",
55
"main": "index.js",
66
"type": "module",

src/urlList.example

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ const _urlList = [
22
// you can add more links in here, but keep in mind, the more links you add
33
// the more resource it will use
44
"https://www.emptywork.my.id"
5-
];
5+
]
66

77
const _options = {
8+
// "categories": []
89
}
910

10-
export { _urlList as urlList, _options as options };
11+
const execOptions = {
12+
"maxBuffer": 2048 * 1024
13+
}
14+
15+
export { _urlList as urlList, _options as options }

0 commit comments

Comments
 (0)