This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_html
executable file
·76 lines (61 loc) · 1.91 KB
/
validate_html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
const globby = require('globby');
const validator = require('html-validator');
const fse = require('fs-extra');
const indent = require('indent-string');
const _ = require('chalk');
function readPaths(paths) {
return Promise.all(paths.map(path => fse.readFile(path)));
}
function validateInputs(inputs) {
return Promise.all(inputs.map(data => validator({
data
})));
}
function processResults(results, paths) {
return new Promise((resolve, reject) => {
let errors = 0;
let warnings = 0;
let passed = 0;
console.log(`\n${_.underline(`Validating ${paths.length} files:`)}\n`);
results.forEach((raw, index) => {
const result = JSON.parse(raw);
const path = paths[index];
if (result.messages.length < 1) {
passed += 1;
console.log(`[${_.green('✓')}] ${path}`);
}
result.messages.forEach((message) => {
if (message.type === 'error') {
errors += 1;
console.log(`[${_.red('x')}] ${path}`);
} else {
warnings += 1;
console.log(`[${_.yellow('?')}] ${path}`);
}
console.log(`\n${indent(message.message, 4)}\n`);
});
});
console.log(
`\nTotal: ${_.red(errors)} errors,`,
`${_.yellow(warnings)} warnings,`,
`${_.green(passed)} passed.\n`
);
if (errors > 0) {
reject();
} else {
resolve();
}
});
}
globby(process.argv.splice(2))
.then(paths =>
readPaths(paths)
.then(inputs => validateInputs(inputs))
.then(results => processResults(results, paths))
).catch((error) => {
if (error) {
console.error(error);
}
process.exit(1);
});