-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (122 loc) · 3.39 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const through = require('through2');
const BufferStreams = require('bufferstreams');
const path = require('path');
const PugLint = require('pug-lint');
const Utils = require('./lib/utils');
/**
* gulpPuglint - description
*
* @param {type} options description
* @return {type} description
*/
function gulpPuglint(options) {
const puglint = new PugLint();
/**
* verify - description
*
* @param {type} string description
* @param {type} filePath description
* @return {type} description
*/
function verify(string, filePath) {
return puglint.checkString(string, filePath);
}
const stream = through.obj((file, encoding, callback) => {
const filePath = path.relative(process.cwd(), file.path);
let config = {};
try {
config = Utils.migrateOptions(options);
} catch (error) {
stream.emit('error', Utils.createError(error.message));
return;
}
puglint.configure(config);
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
file.contents = file.contents.pipe(
new BufferStreams((error, buffer, done) => {
file.puglint = verify(String(buffer), filePath);
done(null, buffer);
callback(null, file);
})
);
return;
}
file.puglint = verify(file.contents.toString(), filePath);
// if (file.puglint.length) {
// const linterOutputs = file.puglint
// .map((result) => result.message)
// .join('\n\n');
// gutil.log(linterOutputs);
// }
callback(null, file);
});
return stream;
}
gulpPuglint.result = (action) => {
if (typeof action !== 'function') {
throw new Error('Expected callable argument');
}
const stream = through.obj((file, encoding, callback) => {
const linterOutputs = file.puglint;
if (linterOutputs) {
const transformCallback = Utils.handleCallback(callback, file);
Utils.tryResultAction(action, linterOutputs, transformCallback);
} else {
callback(null, file);
}
});
return stream;
};
gulpPuglint.results = (action) => {
if (typeof action !== 'function') {
throw new Error('Expected callable argument');
}
const results = [];
const stream = through.obj((file, encoding, callback) => {
const linterOutputs = file.puglint;
if (linterOutputs) {
linterOutputs.forEach((linterOutput) => {
results.push(linterOutput);
});
}
callback(null, file);
}, (callback) => {
const flushCallback = Utils.handleCallback(callback);
Utils.tryResultAction(action, results, flushCallback);
});
return stream;
};
gulpPuglint.failOnError = () => {
return gulpPuglint.result((result) => {
const error = Utils.firstResultMessage(result, Utils.isErrorMessage);
if (!error) {
return;
}
throw Utils.createError({
name: 'PugLintError',
fileName: result.filePath,
message: error.message,
lineNumber: error.line,
});
});
};
gulpPuglint.failAfterError = () => {
// TODO
};
gulpPuglint.formatEach = (formatter, writable) => {
// TODO
};
gulpPuglint.format = (formatter, writable) => {
formatter = Utils.resolveFormatter(formatter);
writable = Utils.resolveWritable(writable);
return gulpPuglint.results((results) => {
if (results.length) {
Utils.writeResults(results, formatter, writable);
}
});
};
module.exports = gulpPuglint;