|
1 | 1 | import { series } from 'async'
|
2 | 2 | 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' |
4 | 4 | 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' |
6 | 6 |
|
7 | 7 | 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 |
13 | 10 |
|
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 |
15 | 17 | }
|
16 | 18 |
|
17 | 19 | const execResult = (err = null, out, outerr = null) => {
|
| 20 | + const isOptionsCategories = (!Array.isEmpty(options?.categories) ?? false) ? `--only-categories=${lighthouseCategories(options?.categories ?? "accessibility")} ` : "" |
18 | 21 |
|
19 | 22 | let accessibilityScores = (!existsSync('./out/scores.json')) ? readFileSync('./src/scores.json') : readFileSync('./out/scores.json')
|
20 | 23 |
|
21 | 24 | const data = JSON.parse(out)
|
| 25 | + |
| 26 | + const commandToRun = `lighthouse ${data.requestedUrl} ${isOptionsCategories}--output json` |
| 27 | + console.log(`command stopped: ${commandToRun}`) |
| 28 | + |
22 | 29 | const accessibilityScoresJSON = JSON.parse(accessibilityScores)
|
23 | 30 | 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"] |
25 | 33 |
|
26 | 34 | optionCategories.forEach(category => {
|
27 | 35 | let categoryScore = data?.categories[category].score
|
28 | 36 |
|
29 | 37 | categoriesScoresObject[category] = categoryScore
|
30 | 38 | })
|
31 | 39 |
|
32 |
| - accessibilityScoresJSON[data.requestedUrl] = categoriesScoresObject |
| 40 | + accessibilityScoresJSON[data?.requestedUrl] = categoriesScoresObject |
33 | 41 |
|
34 | 42 | const newAccessibilityJSON = JSON.stringify(accessibilityScoresJSON)
|
35 | 43 |
|
36 | 44 | if (!existsSync('./out/')) mkdirSync('./out/')
|
37 | 45 |
|
38 | 46 | 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` |
40 | 51 |
|
41 | 52 | writeFileSync(rawOutputFilename, JSON.stringify(data), { flag: 'w' })
|
42 | 53 | return writeFileSync('./out/scores.json', newAccessibilityJSON, { flag: 'w' })
|
43 | 54 | }
|
44 | 55 |
|
45 | 56 | 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` |
48 | 59 |
|
49 | 60 | console.log(`running command: ${commandToRun}`)
|
50 | 61 |
|
51 | 62 | series([
|
52 |
| - () => exec(commandToRun, execResult), |
| 63 | + () => exec(commandToRun, execOptions, execResult), |
53 | 64 | // () => 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)) |
54 | 66 | ])
|
55 | 67 | }
|
56 | 68 |
|
|
0 commit comments