-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmllint.js
33 lines (31 loc) · 1.31 KB
/
htmllint.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
const htmllint = require("htmllint");
const glob = require("glob");
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const opts = JSON.parse(fs.readFileSync(path.join(__dirname, ".htmllintrc"), "utf8"));
glob("**/*.ejs", function (er, files) {
Promise.all(files.map(file => new Promise((resolve, reject) =>
fs.readFile(file, "utf8", (err, data) => err ? reject(err) : resolve(data))
))).then(fileStrings => Promise.all(fileStrings.map((f, i) => {
const filename = files[i];
let escaped = f
.replace(new RegExp("<%(=|-).*?%>", "gs"), "null")
.replace(new RegExp("<(?!%)((.(?!>))*?)<%.*%>(.*?)(?<!%)>", "g"), "<$1$3>") // eslint-disable-line
.replace(new RegExp("<%(.*?)%>", "gs"), "<!--$1-->");
return htmllint(escaped, opts).then(issues => {
issues.forEach(issue => {
console.log(`${chalk.magenta(filename)}:${issue.line}:${issue.column} ${chalk.red(htmllint.messages.renderIssue(issue))}\n`);
});
return issues.length;
}).catch(err => {
throw ("[htmllint error in " + filename + " ] " + err);
});
})).then(issues => {
const totalIssues = issues.reduce((acc, val) => acc + val);
if (totalIssues > 0) {
console.log(chalk.red(`${totalIssues} issues were found\n`));
process.exit(1);
}
}));
});