1
+ import { series } from 'async'
2
+ import { exec } from 'child_process'
3
+ import { readFileSync , writeFileSync , existsSync , mkdirSync , copyFileSync } from 'fs'
4
+ if ( ! existsSync ( './legacy/src/urlList.js' ) ) copyFileSync ( './legacy/src/urlList.example' , './legacy/src/urlList.js' )
5
+ import { urlList , options , execOptions } from './src/urlList.js'
6
+ import { makeCommandFromURL } from './src/lib/commandHandlers.js'
7
+ import { getCategoriesFromCategoriesFlags , getCurrentCategories } from './src/lib/categoriesHandlers.js'
8
+ import { getCategoriesFlags } from "./src/lib/categoriesHandlers.js"
9
+
10
+ Object . prototype . isEmpty = ( obj ) => {
11
+ for ( const prop in obj )
12
+ if ( Object . hasOwn ( obj , prop ) ) return false
13
+
14
+ return true
15
+ }
16
+
17
+ Array . prototype . isEmpty = ( arr ) => {
18
+ return ( arr === undefined || arr . length === 0 ) ? false : true
19
+ }
20
+
21
+ const execResult = ( err = null , out , outerr = null ) => {
22
+ let accessibilityScores = ( ! existsSync ( './out/scores.json' ) ) ? readFileSync ( './src/scores.json' ) : readFileSync ( './out/scores.json' )
23
+
24
+ const data = JSON . parse ( out )
25
+
26
+ const { commandToRun } = makeCommandFromURL ( data ?. requestedUrl , options )
27
+ if ( options ?. consoleLog ?? true ) console . log ( `Stopped Test on ${ data ?. requestedUrl } ` )
28
+
29
+ const accessibilityScoresJSON = JSON . parse ( accessibilityScores )
30
+ const categoriesScoresObject = { }
31
+ const categories = getCategoriesFromCategoriesFlags ( options ?. categories )
32
+ const optionCategories = getCurrentCategories ( categories )
33
+
34
+ optionCategories . forEach ( category => {
35
+ let categoryScore = data ?. categories [ category ] . score
36
+
37
+ categoriesScoresObject [ category ] = categoryScore
38
+ } )
39
+
40
+ accessibilityScoresJSON [ data ?. requestedUrl ] = categoriesScoresObject
41
+
42
+ const newAccessibilityJSON = JSON . stringify ( accessibilityScoresJSON )
43
+
44
+ if ( ! existsSync ( './out/' ) ) mkdirSync ( './out/' )
45
+
46
+ if ( ! existsSync ( './out/logs' ) ) mkdirSync ( './out/logs' )
47
+
48
+ const REGEX_HTTPS_HTTP = / ^ ( h t t p | h t t p s ) : \/ \/ ( w w w .| ) / g
49
+
50
+ const logFileNameBasedOnUrl = data ?. requestedUrl . replace ( REGEX_HTTPS_HTTP , '' ) . replaceAll ( "/" , "" ) . split ( '.' ) . reverse ( ) . join ( '.' )
51
+ const rawOutputFilename = `./out/logs/${ logFileNameBasedOnUrl } -${ optionCategories
52
+ . join ( '-' ) } -${ Date . now ( ) } .json`
53
+
54
+ writeFileSync ( rawOutputFilename , JSON . stringify ( data ) , { flag : 'w' } )
55
+ return writeFileSync ( './out/scores.json' , newAccessibilityJSON , { flag : 'w' } )
56
+ }
57
+
58
+ const testURL = ( urlToCheck , options = { } ) => {
59
+ const { commandToRun } = makeCommandFromURL ( urlToCheck , options )
60
+ if ( options ?. consoleLog ?? true ) console . log ( `Running Test on ${ urlToCheck } ` )
61
+
62
+ series ( [
63
+ ( ) => exec ( commandToRun , execOptions , execResult ) ,
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))
66
+ ] )
67
+ }
68
+
69
+ const main = ( urlList , options ) => {
70
+ const isOptionsCategories = getCategoriesFlags ( options ?. categories )
71
+ const currentFlags = `${ isOptionsCategories } --output json --disable-full-page-screenshot --chrome-flags="--no-sandbox --headless --disable-gpu"`
72
+
73
+ console . log ( `[Legacy] TLighthouse ${ process . env . npm_package_version } - Thesis Example Code` )
74
+ console . log ( `Running with these Flags: ${ currentFlags } \n` )
75
+
76
+ urlList . forEach ( ( url , index ) => { testURL ( url , options ) } )
77
+ }
78
+
79
+ main ( urlList , options )
0 commit comments