forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlowTypecheckPlugin.js
220 lines (207 loc) · 6.2 KB
/
FlowTypecheckPlugin.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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const childProcess = require('child_process');
const flowBinPath = require('flow-bin');
function exec(command, args, options) {
return new Promise((resolve, reject) => {
var stdout = new Buffer('');
var stderr = new Buffer('');
var oneTimeProcess = childProcess.spawn(command, args, options);
oneTimeProcess.stdout.on('data', chunk => {
stdout = Buffer.concat([stdout, chunk]);
});
oneTimeProcess.stderr.on('data', chunk => {
stderr = Buffer.concat([stderr, chunk]);
});
oneTimeProcess.on('error', error => reject(error));
oneTimeProcess.on('exit', code => {
switch (code) {
case 0: {
return resolve(stdout);
}
default: {
return reject(new Error(Buffer.concat([stdout, stderr]).toString()));
}
}
});
});
}
function createVersionWarning(flowVersion) {
return 'Flow: ' +
chalk.red(
chalk.bold(
`Your global flow version is incompatible with this tool.
To fix warning, uninstall it or run \`npm install -g flow-bin@${flowVersion}\`.`
)
);
}
function formatFlowErrors(error) {
return error
.toString()
.split('\n')
.filter(line => {
return !(/flow is still initializing/.test(line) ||
/Found \d+ error/.test(line) ||
/The flow server is not responding/.test(line) ||
/Going to launch a new one/.test(line) ||
/The flow server is not responding/.test(line) ||
/Spawned flow server/.test(line) ||
/Logs will go to/.test(line) ||
/version didn't match the client's/.test(line));
})
.map(line => line.replace(/^Error:\s*/, ''))
.join('\n');
}
function getFlowVersion(global) {
return exec(global ? 'flow' : flowBinPath, ['version', '--json'])
.then(data => JSON.parse(data.toString('utf8')).semver || '0.0.0')
.catch(() => null);
}
class FlowTypecheckPlugin {
constructor() {
this.shouldRun = false;
this.flowStarted = false;
this.flowStarting = null;
this.flowVersion = require(path.join(
__dirname,
'package.json'
)).dependencies['flow-bin'];
}
startFlow(cwd) {
if (this.flowStarted) {
return Promise.resolve();
}
if (this.flowStarting != null) {
return this.flowStarting.then(err => {
// We need to do it like this because of unhandled rejections
// ... basically, we can't actually reject a promise unless someone
// has it handled -- which is only the case when we're returned from here
if (err != null) {
throw err;
}
});
}
console.log(chalk.cyan('Starting the flow server ...'));
const flowConfigPath = path.join(cwd, '.flowconfig');
let delegate;
this.flowStarting = new Promise(resolve => {
delegate = resolve;
});
return getFlowVersion(true)
.then(globalVersion => {
if (globalVersion === null) return;
return getFlowVersion(false).then(ourVersion => {
if (globalVersion !== ourVersion) {
return Promise.reject('__FLOW_VERSION_MISMATCH__');
}
});
})
.then(
() => new Promise(resolve => {
fs.access(flowConfigPath, err => {
if (err) {
resolve(exec(flowBinPath, ['init'], { cwd }));
} else {
resolve();
}
});
})
)
.then(() => exec(flowBinPath, ['stop'], {
cwd,
}))
.then(() => exec(flowBinPath, ['start'], { cwd }).catch(err => {
if (
typeof err.message === 'string' &&
err.message.indexOf('There is already a server running') !== -1
) {
return true;
} else {
throw err;
}
}))
.then(() => {
this.flowStarted = true;
delegate();
this.flowStarting = null;
})
.catch(err => {
delegate(err);
this.flowStarting = null;
throw err;
});
}
apply(compiler) {
compiler.plugin('compile', () => {
this.shouldRun = false;
});
compiler.plugin('compilation', compilation => {
compilation.plugin('normal-module-loader', (loaderContext, module) => {
if (
this.shouldRun ||
module.resource.indexOf('node_modules') !== -1 ||
!/[.]js(x)?$/.test(module.resource)
) {
return;
}
const contents = loaderContext.fs.readFileSync(module.resource, 'utf8');
if (
/^\s*\/\/.*@flow/.test(contents) || /^\s*\/\*.*@flow/.test(contents)
) {
this.shouldRun = true;
}
});
});
// Run lint checks
compiler.plugin('emit', (compilation, callback) => {
if (!this.shouldRun) {
callback();
return;
}
const cwd = compiler.options.context;
const first = this.flowStarting == null && !this.flowStarted;
this.startFlow(cwd)
.then(() => {
if (first) {
console.log(
chalk.yellow(
'Flow is initializing, ' +
chalk.bold('this might take a while...')
)
);
} else {
console.log('Running flow...');
}
exec(flowBinPath, ['status', '--color=always'], { cwd })
.then(() => {
callback();
})
.catch(e => {
compilation.warnings.push(formatFlowErrors(e));
callback();
});
})
.catch(e => {
if (e === '__FLOW_VERSION_MISMATCH__') {
compilation.warnings.push(createVersionWarning(this.flowVersion));
} else {
compilation.warnings.push(
'Flow: Type checking has been disabled due to an error in Flow.'
);
}
callback();
});
});
}
}
module.exports = FlowTypecheckPlugin;