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