-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (54 loc) · 1.59 KB
/
index.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
const chalk = require('chalk');
const fs = require('fs');
const globAll = require('glob-all');
const htmllint = require('htmllint');
const htmllintMessages = require('htmllint/lib/messages');
const mocha = require('mocha');
const stripAnsi = require('strip-ansi');
const textTable = require('text-table');
module.exports = function (patterns, options) {
describe('htmllint', function () {
globAll.sync(patterns).forEach(function (file) {
describe(file, () => {
it(`should pass htmllint rules`, function(done) {
fs.readFile(file, 'utf8', function(err, html) {
htmllint(html, options, function(err, issues) {
if (err) {
return done(err);
}
if (issues && issues.length > 0) {
let message = chalk.white.underline(`\n${file} did not pass htmllint rules\n`);
message += issuesTable(issues);
message += chalk.red.bold(`\n\n✖ ${issues.length} ${issues.length === 1 ? 'issue' : 'issues'}\n`);
let error = new Error(message);
delete error.stack; // Yeah, I know, but we don't care about the stack here.
return done(error);
}
done();
});
});
});
});
});
});
}
function issuesTable (issues) {
return textTable(
issues.map(function (issue) {
return [
chalk.white(''),
`line ${issue.line || 0}`,
`column ${issue.column || 0}`,
issue.code,
chalk.cyan(htmllintMessages.renderIssue(issue) || JSON.stringify(issue.data)),
chalk.white.bold(issue.rule)
]
}),
{
align: ['', 'r', 'l'],
stringLength: function (str) {
return stripAnsi(str).length;
}
}
);
}