Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit d6401a4

Browse files
committed
build: allow for endpoint to be specified when auditing docs
With the recent Bazel refactorings, an URL to the audit script is no longer accepted. This is necesary though as we leverage that script for monitoring the docs site.
1 parent 809a8f2 commit d6401a4

File tree

1 file changed

+56
-49
lines changed

1 file changed

+56
-49
lines changed

tools/audit-docs.js

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
/**
55
* Usage:
66
* ```sh
7-
* node tools/audit-docs <origin> <delay>
7+
* node tools/audit-docs <deploy-dir|url> <delay>
88
* ```
99
*
1010
* Runs the configured audits on several (pre-defined) pages on the specified
1111
* origin. It fails if any score for any page is below the minimum (see
1212
* `MIN_SCORES_PER_PAGE` below).
1313
*
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
1517
* deployment. It can be remote (e.g. `https://next.material.angular.io`) or local (e.g.
1618
* `http://localhost:4200`).
1719
*
@@ -29,39 +31,38 @@ const lightServer = require('light-server');
2931

3032
// Individual page a11y scores
3133
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,
4446
};
4547

46-
4748
/**
4849
* @type {{minScores: {performance: number, accessibility: number, 'best-practices': number, pwa: number, seo: number}, url: string}[]}
4950
*/
5051
const MIN_SCORES_PER_PAGE = [
5152
{
5253
url: '',
5354
minScores: {
54-
'pwa': 70,
55-
'performance': 25,
56-
'seo': 98,
55+
pwa: 70,
56+
performance: 25,
57+
seo: 98,
5758
'best-practices': 100,
58-
'accessibility': 100
59-
}
59+
accessibility: 100,
60+
},
6061
},
6162
...Object.entries(MIN_A11Y_SCORES_PER_PAGE).map(([url, accessibility]) => ({
6263
url,
63-
minScores: {accessibility}
64-
}))
64+
minScores: {accessibility},
65+
})),
6566
];
6667

6768
/**
@@ -79,51 +80,57 @@ function formatScores(scores) {
7980
return formattedScores;
8081
}
8182

82-
8383
// Launch the light-server to run tests again
84-
const bundle = process.argv[2];
84+
const urlOrDeployDir = process.argv[2];
8585
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 (!/https?:\/\//.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+
}
108112

109113
// Run the a11y audit against the above pages
110114
const lighthouseAuditCmd = `"${process.execPath}" "${__dirname}/lighthouse-audit"`;
111115

112-
setTimeout(async function() {
116+
setTimeout(async function () {
113117
console.log('Run audit tests...');
118+
console.log('Origin:', origin);
114119

115120
try {
116121
for (const {url, minScores} of MIN_SCORES_PER_PAGE) {
117122
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+
});
119126

120127
cp.on('error', reject);
121128
cp.on('exit', err => (err ? reject : resolve)(err));
122129
});
123130
}
124131

125132
process.exit(0);
126-
} catch(e) {
133+
} catch (e) {
127134
console.log('Audit failure: ', e);
128135
process.exit(1);
129136
}

0 commit comments

Comments
 (0)