-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
115 lines (95 loc) · 2.84 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
'use strict';
const { execFile } = require('node:child_process');
const pc = require('picocolors');
const PluginError = require('plugin-error');
const through = require('through2');
const vnuJar = require('vnu-jar');
const winston = require('winston');
const defaultOptions = {
'errors-only': false,
'format': 'gnu',
'html': false,
'no-stream': false,
'verbose': false
};
const vnuErrorLevels = {
levels: {
'success': 0,
'error': 1,
'info': 2,
'non-document-error': 3
},
colors: {
'success': 'green',
'error': 'red',
'info': 'yellow',
'non-document-error': 'black'
}
};
const logger = winston.createLogger({
levels: vnuErrorLevels.levels,
transports: [
new (winston.transports.Console)({
formatter(options) {
let levelType = '';
// Return string will be passed to logger.
switch (options.level) {
case 'error': {
levelType = pc.red('error: ');
break;
}
case 'info': {
levelType = pc.yellow('warning: ');
break;
}
case 'success': {
return `${pc.green(options.message) + pc.underline(pc.bold(options.meta.path))}\n`;
}
default: {
return pc.bold('non-document-error: ') + options.message;
}
}
return levelType + pc.underline(pc.bold(options.meta.url)) + '\n' +
pc.bold(options.meta.lastLine + ':' + options.meta.firstColumn) + '\t' + options.message + '\n' +
'source: ' + options.meta.extract + '\n';
}
})
]
});
const handleJsonError = (error, messages, path) => {
const parsedMessages = JSON.parse(messages).messages;
if (error === null && parsedMessages.length === 0) {
return logger.log('success', 'Document is valid: ', { path });
}
return parsedMessages.map(message => logger.log(message.type, message.message, message));
};
module.exports = options => {
const vnuArgs = ['-Xss1024k', '-jar', `"${vnuJar}"`];
const mergedOptions = { ...defaultOptions, ...options };
// Set options
for (const [key, value] of Object.entries(mergedOptions)) {
if (key === 'format' && value !== 'gnu') {
vnuArgs.push('--format', value);
}
if (value === true) {
vnuArgs.push(`--${key}`);
}
}
return through.obj((file, enc, cb) => {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError('gulp-html', 'Streaming not supported'));
}
vnuArgs.push(file.history.map(f => `"${f}"`));
execFile('java', vnuArgs, { shell: true }, (error, stdout, stderr) => {
if (mergedOptions.format === 'json') {
return cb(handleJsonError(error, stderr, file.history[0]));
}
return error === null ?
cb(null, file) :
cb(new PluginError('gulp-html', stderr));
});
});
};