Skip to content

Commit 7868d67

Browse files
committed
Update index.js added support for execOptions and more
1 parent abb2920 commit 7868d67

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,52 @@ Array.prototype.isEmpty = (arr) => {
1717
}
1818

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

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

2324
const data = JSON.parse(out)
25+
26+
const commandToRun = `lighthouse ${data.requestedUrl} ${isOptionsCategories}--output json`
27+
console.log(`command stopped: ${commandToRun}`)
28+
2429
const accessibilityScoresJSON = JSON.parse(accessibilityScores)
2530
const categoriesScoresObject = {}
26-
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"]
2733

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

3137
categoriesScoresObject[category] = categoryScore
3238
})
3339

34-
accessibilityScoresJSON[data.requestedUrl] = categoriesScoresObject
40+
accessibilityScoresJSON[data?.requestedUrl] = categoriesScoresObject
3541

3642
const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
3743

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

4046
if (!existsSync('./out/logs')) mkdirSync('./out/logs')
41-
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`
4251

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

4756
const testURL = (urlToCheck, options = {}) => {
48-
const isOptionsCategories = (!Object.isEmpty(options.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")}` : ""
49-
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`
5059

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

5362
series([
54-
() => exec(commandToRun, execResult),
63+
() => exec(commandToRun, execOptions, execResult),
5564
// () => 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))
5666
])
5767
}
5868

0 commit comments

Comments
 (0)