1
- import { series } from 'async' ;
2
- import { exec } from 'child_process' ;
3
- import { readFileSync , writeFileSync , existsSync , mkdirSync , copyFileSync } from 'fs' ;
1
+ import { series } from 'async'
2
+ import { exec } from 'child_process'
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
- const data = JSON . parse ( out ) ;
21
+ const data = JSON . parse ( out )
11
22
const accessibilityScoresJSON = JSON . parse ( accessibilityScores )
12
- const URLaccessibilityScoreResult = data ?. categories ?. accessibility ?. score
13
- accessibilityScoresJSON [ data . requestedUrl ] = URLaccessibilityScoreResult
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
28
+
29
+ categoriesScoresObject [ category ] = categoryScore
30
+ } )
31
+
32
+ accessibilityScoresJSON [ data . requestedUrl ] = categoriesScoresObject
33
+
14
34
const newAccessibilityJSON = JSON . stringify ( accessibilityScoresJSON )
35
+
15
36
if ( ! existsSync ( './out/' ) ) mkdirSync ( './out/' )
37
+
38
+ if ( ! existsSync ( './out/logs' ) ) mkdirSync ( './out/logs' )
39
+ const rawOutputFilename = `./out/logs/${ data . requestedUrl . split ( '.' ) [ 1 ] } _${ Date . now ( ) } .json`
40
+
41
+ writeFileSync ( rawOutputFilename , JSON . stringify ( data ) , { flag : 'w' } )
16
42
return writeFileSync ( './out/scores.json' , newAccessibilityJSON , { flag : 'w' } )
17
43
}
18
44
19
- const testURL = ( urlToCheck ) => {
20
- const commandToRun = `lighthouse ${ urlToCheck } --only-categories=accessibility --output json`
45
+ 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`
48
+
49
+ console . log ( `running command: ${ commandToRun } ` )
21
50
22
51
series ( [
23
- ( ) => exec ( commandToRun , execResult )
52
+ ( ) => exec ( commandToRun , execResult ) ,
53
+ // () => exec("lighthouse https://emptywork.my.id --output json >> dump", (err, stdout, stderr) => console.log(stdout))
24
54
] )
25
55
}
26
56
27
- urlList . forEach ( url => testURL ( url ) )
57
+ const lighthouseCategories = ( categories = [ ] ) => {
58
+ if ( typeof categories == "string" ) return categories
59
+ return categories . join ( ',' )
60
+ }
61
+
62
+ urlList . forEach ( url => testURL ( url , options ) )
0 commit comments