4
4
/**
5
5
* Usage:
6
6
* ```sh
7
- * node tools/audit-docs <origin > <delay>
7
+ * node tools/audit-docs <deploy-dir|url > <delay>
8
8
* ```
9
9
*
10
10
* Runs the configured audits on several (pre-defined) pages on the specified
11
11
* origin. It fails if any score for any page is below the minimum (see
12
12
* `MIN_SCORES_PER_PAGE` below).
13
13
*
14
- * `<origin>` is the origin (scheme + hostname + port) of an material.angular.io
14
+ * <deploy-dir> is a path to a directory which should be served and tested.
15
+ *
16
+ * `<url>` is the origin (scheme + hostname + port) of an material.angular.io
15
17
* deployment. It can be remote (e.g. `https://next.material.angular.io`) or local (e.g.
16
18
* `http://localhost:4200`).
17
19
*
@@ -29,39 +31,38 @@ const lightServer = require('light-server');
29
31
30
32
// Individual page a11y scores
31
33
const MIN_A11Y_SCORES_PER_PAGE = {
32
- '' : 100 ,
33
- 'components/categories' : 91 ,
34
- 'cdk/categories' : 91 ,
35
- ' guides' : 100 ,
36
- 'guide/creating-a-custom-form-field-control' : 97 ,
37
- 'guide/getting-started' : 96 ,
38
- 'cdk/a11y/overview' : 85 ,
39
- 'cdk/a11y/api' : 89 ,
40
- 'cdk/a11y/examples' : 85 ,
41
- 'components/button/overview' : 92 ,
42
- 'components/button/api' : 89 ,
43
- 'components/button/examples' : 90 ,
34
+ '' : 100 ,
35
+ 'components/categories' : 91 ,
36
+ 'cdk/categories' : 91 ,
37
+ guides : 100 ,
38
+ 'guide/creating-a-custom-form-field-control' : 97 ,
39
+ 'guide/getting-started' : 96 ,
40
+ 'cdk/a11y/overview' : 85 ,
41
+ 'cdk/a11y/api' : 89 ,
42
+ 'cdk/a11y/examples' : 85 ,
43
+ 'components/button/overview' : 92 ,
44
+ 'components/button/api' : 89 ,
45
+ 'components/button/examples' : 90 ,
44
46
} ;
45
47
46
-
47
48
/**
48
49
* @type {{minScores: {performance: number, accessibility: number, 'best-practices': number, pwa: number, seo: number}, url: string}[] }
49
50
*/
50
51
const MIN_SCORES_PER_PAGE = [
51
52
{
52
53
url : '' ,
53
54
minScores : {
54
- ' pwa' : 70 ,
55
- ' performance' : 25 ,
56
- ' seo' : 98 ,
55
+ pwa : 70 ,
56
+ performance : 25 ,
57
+ seo : 98 ,
57
58
'best-practices' : 100 ,
58
- ' accessibility' : 100
59
- }
59
+ accessibility : 100 ,
60
+ } ,
60
61
} ,
61
62
...Object . entries ( MIN_A11Y_SCORES_PER_PAGE ) . map ( ( [ url , accessibility ] ) => ( {
62
63
url,
63
- minScores : { accessibility}
64
- } ) )
64
+ minScores : { accessibility} ,
65
+ } ) ) ,
65
66
] ;
66
67
67
68
/**
@@ -79,51 +80,57 @@ function formatScores(scores) {
79
80
return formattedScores ;
80
81
}
81
82
82
-
83
83
// Launch the light-server to run tests again
84
- const bundle = process . argv [ 2 ] ;
84
+ const urlOrDeployDir = process . argv [ 2 ] ;
85
85
const delay = process . argv [ 3 ] ;
86
-
87
- const bind = 'localhost' ;
88
- const port = 4200 ;
89
- const origin = `http://${ bind } :${ port } ` ;
90
-
91
- console . log ( 'Launch audit HTTP server...' ) ;
92
-
93
- lightServer ( {
94
- port,
95
- bind,
96
- serve : bundle ,
97
- quiet : true ,
98
- noReload : true ,
99
- historyindex : '/index.html' ,
100
-
101
- // Defaults from .bin/light-server
102
- interval : 500 ,
103
- delay : 0 ,
104
- proxypaths : [ '/' ] ,
105
- watchexps : [ ]
106
- } )
107
- . start ( )
86
+ let origin = urlOrDeployDir ;
87
+
88
+ // If a directory has been specified instead of an origin,
89
+ // we start light server for this directory.
90
+ if ( ! / h t t p s ? : \/ \/ / . test ( urlOrDeployDir ) ) {
91
+ const bind = 'localhost' ;
92
+ const port = 4200 ;
93
+
94
+ origin = `http://${ bind } :${ port } ` ;
95
+ console . log ( 'Launch audit HTTP server...' ) ;
96
+
97
+ lightServer ( {
98
+ port,
99
+ bind,
100
+ serve : urlOrDeployDir ,
101
+ quiet : true ,
102
+ noReload : true ,
103
+ historyindex : '/index.html' ,
104
+
105
+ // Defaults from .bin/light-server
106
+ interval : 500 ,
107
+ delay : 0 ,
108
+ proxypaths : [ '/' ] ,
109
+ watchexps : [ ] ,
110
+ } ) . start ( ) ;
111
+ }
108
112
109
113
// Run the a11y audit against the above pages
110
114
const lighthouseAuditCmd = `"${ process . execPath } " "${ __dirname } /lighthouse-audit"` ;
111
115
112
- setTimeout ( async function ( ) {
116
+ setTimeout ( async function ( ) {
113
117
console . log ( 'Run audit tests...' ) ;
118
+ console . log ( 'Origin:' , origin ) ;
114
119
115
120
try {
116
121
for ( const { url, minScores} of MIN_SCORES_PER_PAGE ) {
117
122
await new Promise ( ( resolve , reject ) => {
118
- const cp = sh . exec ( `${ lighthouseAuditCmd } ${ origin } /${ url } ${ formatScores ( minScores ) } ` , { async : true } ) ;
123
+ const cp = sh . exec ( `${ lighthouseAuditCmd } ${ origin } /${ url } ${ formatScores ( minScores ) } ` , {
124
+ async : true ,
125
+ } ) ;
119
126
120
127
cp . on ( 'error' , reject ) ;
121
128
cp . on ( 'exit' , err => ( err ? reject : resolve ) ( err ) ) ;
122
129
} ) ;
123
130
}
124
131
125
132
process . exit ( 0 ) ;
126
- } catch ( e ) {
133
+ } catch ( e ) {
127
134
console . log ( 'Audit failure: ' , e ) ;
128
135
process . exit ( 1 ) ;
129
136
}
0 commit comments