Make flow check async

This commit is contained in:
Ludo Fardel
2015-08-17 02:05:29 -07:00
parent ca38908423
commit 8460db57bc
4 changed files with 111 additions and 9 deletions

View File

@@ -59,6 +59,13 @@ function setUpRedBoxConsoleErrorHandler() {
}
}
function setupFlowChecker() {
if (__DEV__) {
var checkFlowAtRuntime = require('checkFlowAtRuntime');
checkFlowAtRuntime();
}
}
/**
* Sets up a set of window environment wrappers that ensure that the
* BatchedBridge is flushed after each tick. In both the case of the
@@ -143,3 +150,4 @@ setUpGeolocation();
setUpWebSockets();
setupProfile();
setUpProcessEnv();
setupFlowChecker();

View File

@@ -0,0 +1,59 @@
/**
* 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.
*
* @providesModule checkFlowAtRuntime
*
*/
'use strict';
function checkFlowAtRuntime() {
var url = getPackagerURL();
if (!url) {
return;
}
fetch(url + 'flow/')
.then(response => response.json())
.then(response => {
if (response.silentError) {
return;
}
throw {
message: response.message,
stack: response.errors.map(err => {
return {
...err,
methodName: err.description,
file: err.filename,
};
}),
};
},
() => {
//if fetch fails, silently give up
})
.done();
}
function getPackagerURL() {
var NativeModules = require('NativeModules');
var scriptURL = (NativeModules
&& NativeModules.SourceCode
&& NativeModules.SourceCode.scriptURL)
|| '';
// extract the url of the packager from the whole scriptURL
// we match until the first / after http(s)://
// i.e. http://www.mypackger.com/debug/my/bundle -> http://www.mypackger.com/
return getFirstOrNull(scriptURL.match(/^https?:\/\/[^/]+\//));
}
function getFirstOrNull(ar) {
return ar ? ar[0] : null;
}
module.exports = checkFlowAtRuntime;

View File

@@ -32,7 +32,7 @@ function parseErrorStack(e, sourceMapInstance) {
return [];
}
var stack = stacktraceParser.parse(e.stack);
var stack = Array.isArray(e.stack) ? e.stack : stacktraceParser.parse(e.stack);
var framesToPop = e.framesToPop || 0;
while (framesToPop--) {

View File

@@ -10,17 +10,23 @@
var chalk = require('chalk');
var exec = require('child_process').exec;
var url = require('url');
var Activity = require('./react-packager/src/Activity');
var hasWarned = {};
var DISABLE_FLOW_CHECK = true; // temporarily disable while we figure out versioning issues.
function getFlowTypeCheckMiddleware(options) {
return function(req, res, next) {
var isBundle = req.url.indexOf('.bundle') !== -1;
if (DISABLE_FLOW_CHECK || options.skipflow || !isBundle) {
var reqObj = url.parse(req.url);
var isFlowCheck = (reqObj.path.match(/^\/flow\//));
if (!isFlowCheck) {
return next();
}
if (options.skipflow) {
_endSkipFlow(res);
return;
}
if (options.flowroot || options.projectRoots.length === 1) {
var flowroot = options.flowroot || options.projectRoots[0];
} else {
@@ -28,7 +34,8 @@ function getFlowTypeCheckMiddleware(options) {
hasWarned.noRoot = true;
console.warn('flow: No suitable root');
}
return next();
_endFlowBad(res);
return;
}
exec('command -v flow >/dev/null 2>&1', function(error, stdout) {
if (error) {
@@ -37,7 +44,8 @@ function getFlowTypeCheckMiddleware(options) {
console.warn(chalk.yellow('flow: Skipping because not installed. Install with ' +
'`brew install flow`.'));
}
return next();
_endFlowBad(res);
return;
} else {
return doFlowTypecheck(res, flowroot, next);
}
@@ -51,7 +59,8 @@ function doFlowTypecheck(res, flowroot, next) {
exec(flowCmd, function(flowError, stdout, stderr) {
Activity.endEvent(eventId);
if (!flowError) {
return next();
_endFlowOk(res);
return;
} else {
try {
var flowResponse = JSON.parse(stdout);
@@ -73,7 +82,7 @@ function doFlowTypecheck(res, flowroot, next) {
errorNum++;
});
var error = {
status: 500,
status: 200,
message: 'Flow found type errors. If you think these are wrong, ' +
'make sure your flow bin and .flowconfig are up to date, or ' +
'disable with --skipflow.',
@@ -102,10 +111,36 @@ function doFlowTypecheck(res, flowroot, next) {
));
}
}
return next();
_endFlowBad(res);
return;
}
}
});
}
function _endRes(res, message, code, silentError) {
res.writeHead(code, {
'Content-Type': 'application/json; charset=UTF-8',
});
res.end(JSON.stringify({
message: message,
errors: [],
silentError: silentError,
}));
}
function _endFlowOk(res) {
_endRes(res, 'No Flow Error', '200', true);
}
function _endFlowBad(res) {
// we want to show that flow failed
// status 200 is need for the fetch to not be rejected
_endRes(res, 'Flow failed to run! Please look at the console for more details.', '200', false);
}
function _endSkipFlow(res) {
_endRes(res, 'Flow was skipped, check the server options', '200', true);
}
module.exports = getFlowTypeCheckMiddleware;