-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
113 lines (94 loc) · 3.01 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
const configFile = require('pug-lint/lib/config-file');
const fancyLog = require('fancy-log');
const PluginError = require('plugin-error');
const PugLint = require('pug-lint');
const throughObj = require('through2').obj;
const PLUGIN_NAME = 'gulp-pug-linter';
/**
* @name getReporter
* @description Gets the reporter depending on its type
* Falls back to the default reporter with a warning
* @param {*} reporter Existing reporter type, name, or function
* @returns {Function} Reporter function to be called with lint errors
*/
const getReporter = (reporter) => {
if (reporter === 'default') {
return report;
} if (typeof reporter === 'function') {
return reporter;
} if (typeof reporter === 'string') {
try {
/* eslint-disable-next-line global-require, import/no-dynamic-require */
return require(reporter);
} catch (error) {}
}
fancyLog.warn([
PLUGIN_NAME,
'warning:',
reporter,
'not found; falling back to default',
].join(' '));
return report;
};
/**
* @name gulpPugLinter
* @description Hooks to PugLint to lint for errors
* @param {Object} options Configuration object
* @param {Boolean} failAfterError Whether to throw a plugin error after
* encountering one or more lint errors
* @param {*} reporter Reporter type, name, or function to show lint errors
* @param {Boolean} silenceOnSuccess Whether to bypass the reporter when there
* are no lint errors
*/
const gulpPugLinter = (options = {}) => {
const config = configFile.load();
const linter = new PugLint();
/**
* @name checkFile
* @description Checks the given file for lint errors
* @param {Object} file File chunk
* @param {String} file.path File path
* @returns {Array} List of error messages
*/
const checkFile = (file) => linter.checkFile(file.path);
linter.configure(config);
return throughObj(function transform(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
} if (file.isStream()) {
return callback(
new PluginError(PLUGIN_NAME, 'Streaming is not supported'),
);
}
const errors = checkFile(file);
const hasErrors = errors.length > 0;
if (Object.keys(options).includes('reporter')) {
const reporter = getReporter(options.reporter);
if (hasErrors || !options.silenceOnSuccess) {
reporter(errors);
}
}
if (options.failAfterError && hasErrors) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Lint failed'));
}
file.pugLinter = {
errors,
options,
};
return callback(null, file);
});
};
/**
* @name report
* @description Combines and logs lint error messages
* @param {Object[]} errors List of lint errors
* @param {String} errors.message Lint error message
*/
const report = (errors) => {
let allErrors;
if (errors.length) {
allErrors = errors.map((error) => error.message).join('\n\n');
fancyLog(allErrors);
}
};
module.exports = gulpPugLinter;