-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathvalidate-amp.js
60 lines (47 loc) · 1.59 KB
/
validate-amp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const amphtmlValidator = require('amphtml-validator');
const glob = require('glob');
const fs = require('fs');
const publicDir = `${__dirname}/public`;
(async () => {
let hasError = false;
glob(`${publicDir}/**/*.html`, {}, (error, files) => {
if (error) {
console.error({error});
process.exit(1);
}
amphtmlValidator.getInstance().then(function(validator) {
files.forEach(file => {
const content = fs.readFileSync(file, 'utf-8');
// ignore non-AMP pages
if (content.indexOf('⚡') == -1) {
return;
}
const result = validator.validateString(content);
const errors = result.errors.filter(error => {
// ignore this error. This error occurs because the optimize.js script removes white space from the style, but this is OK for valid AMP
return error.message.indexOf("head > style[amp-boilerplate]' is missing or incorrect") == -1;
})
for (let ii = 0; ii < errors.length; ii++) {
const error = result.errors[ii];
let msg = '-------\n';
msg += `file: ${file}\n`;
msg += `line: ${error.line}\n`;
msg += `col: ${error.col}\n`;
msg += `message: ${error.message}\n`;
if (error.specUrl !== null) {
msg += `see: ${error.specUrl}\n`;
}
msg += '-------\n';
console.log(msg)
}
if (errors.length > 0) {
hasError = true;
}
})
});
})
if (hasError) {
console.error('AMP validation error');
process.exit(2);
}
})()