forked from stcheng/gulp-flowtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
234 lines (209 loc) · 5.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* @flow weak */
'use strict';
var Q = require('q');
var fs = require('fs');
require('6to5/polyfill');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var flowBin = require('flow-bin');
var logSymbols = require('log-symbols');
var stylish = require('jshint-stylish');
var reporter = require(stylish).reporter;
var flowToJshint = require('flow-to-jshint');
var execFile = require('child_process').execFile;
/**
* Flow check initialises a server per folder when run,
* we can store these paths and kill them later if need be.
*/
var servers = [];
var passed = true;
/**
* Wrap critical Flow exception into default Error json format
*/
function fatalError(stderr) {
return {
errors: [{
message: [{
path: '',
code: 0,
line: 0,
col: 0,
descr: stderr
}]
}]
};
}
function optsToArgs(opts) {
var args = [];
if (opts.all) {
args.push('--all');
}
if (opts.weak) {
args.push('--weak');
}
if (opts.declarations) {
args.push('--lib', opts.declarations);
}
return args;
}
function executeFlow(_path, opts) {
var deferred = Q.defer();
var command = optsToArgs(opts).length ? (() => {
servers.push(path.dirname(_path));
return 'check';
})() : 'status';
var args = [
command,
'/' + path.relative('/', _path),
'--json'
].concat(optsToArgs(opts));
execFile(getFlowBin(), args, function (err, stdout, stderr) {
if (stderr && /server launched/.test(stderr)) {
/**
* When flow starts a server it gives us an stderr
* saying the server is starting.
*/
stderr = null;
}
var parsed = !stderr ? JSON.parse(stdout) : fatalError(stderr);
var result = {};
result.errors = parsed.errors.filter(function (error) {
error.message = error.message.filter(function (message, index) {
var isCurrentFile = message.path === _path;
var result = false;
/**
* If FlowType traces an issue to a method inside a file that is not
* the one being piped through, it adds a new element to the list
* of errors with a different file path to the current one. To detect
* whether this error is related to the current file we check the
* previous and next error to see if it ends with `found`, `in` or
* `with`, From this we can tell if the error should be shown or not.
*/
var lineEnding = /(with|found|in)$/;
var previous = error.message[index - 1];
if (previous && lineEnding.test(previous.descr)) {
result = previous.path === _path;
}
var nextMessage = error.message[index + 1];
if (nextMessage && lineEnding.test(message.descr)) {
result = nextMessage.path === _path;
}
var generalErrorRegEx = opts.generalErrorRegEx || /(Fatal)/;
var generalError = (generalErrorRegEx.test(message.descr));
return isCurrentFile || result || generalError;
});
return error.message.length > 0;
});
if (result.errors.length) {
passed = false;
reporter(flowToJshint(result));
if (args.abort) {
deferred.reject(new gutil.PluginError('gulp-flow', 'Flow failed'));
}
else {
deferred.resolve();
}
}
else {
deferred.resolve();
}
});
return deferred.promise;
}
function checkFlowConfigExist() {
var deferred = Q.defer();
var config = path.join(process.cwd(), '.flowconfig');
fs.exists(config, function(exists) {
if (exists) {
deferred.resolve();
}
else {
deferred.reject('Missing .flowconfig in the current working directory.');
}
});
return deferred.promise;
}
function hasJsxPragma(contents) {
return /@flow\b/ig
.test(contents);
}
function isFileSuitable(file) {
var deferred = Q.defer();
if (file.isNull()) {
deferred.reject();
}
else if (file.isStream()) {
deferred.reject(new gutil.PluginError('gulp-flow', 'Stream content is not supported'));
}
else if (file.isBuffer()) {
deferred.resolve();
}
else {
deferred.reject();
}
return deferred.promise;
}
function killServers() {
var defers = servers.map(function(_path) {
var deferred = Q.defer();
execFile(getFlowBin(), ['stop'], {
cwd: _path
}, deferred.resolve);
return deferred;
});
return Q.all(defers);
}
function getFlowBin() {
process.env.FLOW_BIN || flowBin;
}
module.exports = function (options={}) {
options.beep = typeof options.beep !== 'undefined' ? options.beep : true;
function Flow(file, enc, callback) {
var _continue = () => {
this.push(file);
callback();
};
isFileSuitable(file).then(() => {
var hasPragma = hasJsxPragma(file.contents.toString());
if (options.all || hasPragma) {
checkFlowConfigExist().then(() => {
executeFlow(file.path, options).then(_continue, err => {
this.emit('error', err);
callback();
});
}, msg => {
console.log(logSymbols.warning + msg);
_continue();
});
} else {
_continue();
}
}, err => {
if (err) {
this.emit('error', err);
}
callback();
});
}
return through.obj(Flow, function () {
var end = () => {
this.emit('end');
};
if (passed) {
console.log(logSymbols.success + ' Flow has found 0 errors');
} else if (options.beep) {
gutil.beep();
}
if (options.killFlow) {
if (servers.length) {
killServers().done(end);
}
else {
end();
}
} else {
end();
}
});
};