forked from stcheng/gulp-flowtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (195 loc) · 4.69 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
/* @flow weak */
'use strict';
const Q = require('q');
const fs = require('fs');
const path = require('path');
const gutil = require('gulp-util');
const through = require('through2');
const flowBin = require('flow-bin');
const logSymbols = require('log-symbols');
const childProcess = require('child_process');
//const chalk = require('chalk');
const reporter = require('flow-reporter');
/**
* Flow check initialises a server per folder when run,
* we can store these paths and kill them later if need be.
*/
const servers = [];
let passed = true;
/**
* Wrap critical Flow exception into default Error json format
*/
function fatalError(stderr) {
return {
errors: [{
message: [{
path: '',
code: 0,
line: 0,
start: 0,
descr: stderr
}]
}]
};
}
function optsToArgs(opts) {
const args = [];
if (opts.all) {
args.push('--all');
}
if (opts.weak) {
args.push('--weak');
}
if (opts.declarations) {
args.push('--lib', opts.declarations);
}
return args;
}
function getFlowBin() {
return process.env.FLOW_BIN || flowBin;
}
function executeFlow(_path, options) {
const deferred = Q.defer();
const opts = optsToArgs(options);
const command = opts.length || options.killFlow ? (() => {
servers.push(path.dirname(_path));
return 'check';
})() : 'status';
const args = [
command,
...opts,
'/' + path.relative('/', _path),
'--json'
];
const stream = childProcess.spawn(getFlowBin(), args);
let dat = '';
stream.stdout.on('data', data => {
dat += data.toString();
});
stream.stdout.on('end', () =>{
let parsed;
try {
parsed = JSON.parse(dat);
}
catch(e) {
parsed = fatalError(dat);
}
const result = {};
// loop through errors in file
result.errors = parsed.errors.filter(function (error) {
let isCurrentFile = error.message[0].path === _path;
let generalError = (/(Fatal)/.test(error.message[0].descr));
return isCurrentFile || generalError;
});
if (result.errors.length) {
passed = false;
const report = typeof options.reporter === 'undefined' ?
reporter : options.reporter;
report(result.errors);
if (options.abort) {
deferred.reject(new gutil.PluginError('gulp-flow', 'Flow failed'));
}
else {
deferred.resolve();
}
}
else {
deferred.resolve();
}
});
return deferred.promise;
}
function checkFlowConfigExist() {
const deferred = Q.defer();
const 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) {
const 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() {
const defers = servers.map(function (_path) {
const deferred = Q.defer();
childProcess.execFile(getFlowBin(), ['stop'], {
cwd: _path
}, deferred.resolve);
return deferred;
});
return Q.all(defers);
}
module.exports = function (options={}) {
options.beep = typeof options.beep !== 'undefined' ? options.beep : true;
function Flow(file, enc, callback) {
const _continue = () => {
this.push(file);
callback();
};
isFileSuitable(file).then(() => {
const 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 () {
const end = () => {
this.emit('end');
passed = true;
};
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();
}
});
};