-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
132 lines (109 loc) · 2.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fancyLog = require('fancy-log');
const through = require('through2');
const PluginError = require('plugin-error');
const { Reporter, CLI } = require('html-validate');
const pluginName = 'gulp-html-validate';
const defaultFormatter = 'stylish';
const htmlValidateCli = new CLI();
module.exports = gulpHtmlValidate;
/**
* Apply html-validate report to each file.
*
* @return {Object} Stream object
*/
function gulpHtmlValidate() {
const htmlvalidate = htmlValidateCli.getValidator();
return through.obj((file, encoding, callback) => {
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new PluginError(pluginName, 'Streaming not supported'));
return;
}
const result = htmlvalidate.validateString(
file.contents.toString(),
file.path
);
file.htmlValidateResult = result;
callback(null, file);
});
}
/**
* Print a report where all errors/warnings are listed.
*
* @param {String} formatterName Formatter supported by html-validate
*
* @return {Object} Stream object
*/
gulpHtmlValidate.format = (formatterName = defaultFormatter) => {
const formatter = htmlValidateCli.getFormatter(formatterName);
return processHtmlValidateResults((results) => {
const { errorCount, warningCount } = results;
if (errorCount === 0 && warningCount === 0) {
return;
}
const formatterOutput = formatter(results);
fancyLog(formatterOutput);
});
};
/**
* Fail when the stream ends if any error(s) occurred.
*
* @return {Object} Stream object
*/
gulpHtmlValidate.failAfterError = () => {
return processHtmlValidateResults((results) => {
const { errorCount } = results;
if (errorCount > 0) {
throw new PluginError(pluginName, {
message: `Failed with ${errorCount} ${
errorCount === 1 ? 'error' : 'errors'
}`,
});
}
});
};
/**
* Process html-validate reports with the given function.
*
* @param {Function} action Callback which receives a list of results
*
* @return {Object} Stream object
*/
function processHtmlValidateResults(action) {
if (typeof action !== 'function') {
throw new TypeError(
`Invalid action: expected a function, got a ${typeof action}`
);
}
const results = [];
function onFile(file, encoding, callback) {
if (file.htmlValidateResult) {
const result = file.htmlValidateResult;
if (result) {
results.push(result);
}
}
callback(null, file);
}
function onStreamEnd(callback) {
const mergedResults = Reporter.merge(results);
tryResultAction(action, mergedResults, callback);
}
return through.obj(onFile, onStreamEnd);
}
function tryResultAction(action, result, done) {
try {
const isAsyncAction = action.length > 1;
if (isAsyncAction) {
action.call(this, result, done);
} else {
action.call(this, result);
done();
}
} catch (err) {
done(err || new Error('Unknown Error'));
}
}