@@ -2,16 +2,34 @@ import { series } from 'async'
2
2
import { exec } from 'child_process'
3
3
import { readFileSync , writeFileSync , existsSync , mkdirSync , copyFileSync , write , writeFile } from 'fs'
4
4
if ( ! existsSync ( './src/urlList.js' ) ) copyFileSync ( './src/urlList.example' , './src/urlList.js' )
5
- import { urlList } from './src/urlList.js'
5
+ import { urlList , options } from './src/urlList.js'
6
+
7
+ Object . prototype . isEmpty = ( obj ) => {
8
+ for ( const prop in obj ) {
9
+ if ( Object . hasOwn ( obj , prop ) ) {
10
+ return false ;
11
+ }
12
+ }
13
+
14
+ return true ;
15
+ }
6
16
7
17
const execResult = ( err = null , out , outerr = null ) => {
18
+
8
19
let accessibilityScores = ( ! existsSync ( './out/scores.json' ) ) ? readFileSync ( './src/scores.json' ) : readFileSync ( './out/scores.json' )
9
20
10
21
const data = JSON . parse ( out )
11
22
const accessibilityScoresJSON = JSON . parse ( accessibilityScores )
12
- const URLaccessibilityScoreResult = data ?. categories ?. accessibility ?. score
23
+ const categoriesScoresObject = { }
24
+ const optionCategories = options . categories ?? [ "accessibility" , "pwa" , "best-practices" , "performance" , "seo" ] ;
25
+
26
+ optionCategories . forEach ( category => {
27
+ let categoryScore = data ?. categories [ category ] . score
13
28
14
- accessibilityScoresJSON [ data . requestedUrl ] = URLaccessibilityScoreResult
29
+ categoriesScoresObject [ category ] = categoryScore
30
+ } )
31
+
32
+ accessibilityScoresJSON [ data . requestedUrl ] = categoriesScoresObject
15
33
16
34
const newAccessibilityJSON = JSON . stringify ( accessibilityScoresJSON )
17
35
@@ -25,10 +43,14 @@ const execResult = (err = null, out, outerr = null) => {
25
43
}
26
44
27
45
const testURL = ( urlToCheck , options = { } ) => {
28
- const commandToRun = `lighthouse ${ urlToCheck } --only-categories=${ lighthouseCategories ( options ?. categories ?? "accessibility" ) } --output json`
46
+ const isOptionsCategories = ( ! Object . isEmpty ( options . categories ) ?? false ) ? `--only-categories=${ lighthouseCategories ( options ?. categories ?? "accessibility" ) } ` : ""
47
+ const commandToRun = `lighthouse ${ urlToCheck } ${ isOptionsCategories } --output json`
48
+
29
49
console . log ( `running command: ${ commandToRun } ` )
50
+
30
51
series ( [
31
- ( ) => exec ( commandToRun , execResult )
52
+ ( ) => exec ( commandToRun , execResult ) ,
53
+ // () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
32
54
] )
33
55
}
34
56
@@ -37,11 +59,4 @@ const lighthouseCategories = (categories = []) => {
37
59
return categories . join ( ',' )
38
60
}
39
61
40
- urlList . forEach ( url => testURL ( url ,
41
- {
42
- "categories" : [
43
- "accessibility" ,
44
- "performance"
45
- ]
46
- } )
47
- )
62
+ urlList . forEach ( url => testURL ( url , options ) )
0 commit comments