mirror of
https://github.com/status-im/metro.git
synced 2025-01-18 23:21:35 +00:00
[ReactNative] Don't redbox on flow config errors
This commit is contained in:
parent
4b9ec6c5f7
commit
4d9f4301ec
@ -8,8 +8,11 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var chalk = require('chalk');
|
||||||
var exec = require('child_process').exec;
|
var exec = require('child_process').exec;
|
||||||
|
|
||||||
|
var hasWarned = {};
|
||||||
|
|
||||||
function getFlowTypeCheckMiddleware(options) {
|
function getFlowTypeCheckMiddleware(options) {
|
||||||
return function(req, res, next) {
|
return function(req, res, next) {
|
||||||
if (options.skipflow) {
|
if (options.skipflow) {
|
||||||
@ -18,13 +21,19 @@ function getFlowTypeCheckMiddleware(options) {
|
|||||||
if (options.flowroot || options.projectRoots.length === 1) {
|
if (options.flowroot || options.projectRoots.length === 1) {
|
||||||
var flowroot = options.flowroot || options.projectRoots[0];
|
var flowroot = options.flowroot || options.projectRoots[0];
|
||||||
} else {
|
} else {
|
||||||
console.warn('flow: No suitable root');
|
if (!hasWarned.noRoot) {
|
||||||
|
hasWarned.noRoot = true;
|
||||||
|
console.warn('flow: No suitable root');
|
||||||
|
}
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
exec('command -v flow >/dev/null 2>&1', function(error, stdout) {
|
exec('command -v flow >/dev/null 2>&1', function(error, stdout) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.warn('flow: Skipping because not installed. Install with ' +
|
if (!hasWarned.noFlow) {
|
||||||
'`brew install flow`.');
|
hasWarned.noFlow = true;
|
||||||
|
console.warn(chalk.yellow('flow: Skipping because not installed. Install with ' +
|
||||||
|
'`brew install flow`.'));
|
||||||
|
}
|
||||||
return next();
|
return next();
|
||||||
} else {
|
} else {
|
||||||
return doFlowTypecheck(res, flowroot, next);
|
return doFlowTypecheck(res, flowroot, next);
|
||||||
@ -36,10 +45,19 @@ function getFlowTypeCheckMiddleware(options) {
|
|||||||
function doFlowTypecheck(res, flowroot, next) {
|
function doFlowTypecheck(res, flowroot, next) {
|
||||||
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
||||||
var start = Date.now();
|
var start = Date.now();
|
||||||
console.log('flow: Running static typechecks.');
|
// Log start message if flow is slow to let user know something is happening.
|
||||||
exec(flowCmd, function(flowError, stdout) {
|
var flowSlow = setTimeout(
|
||||||
|
function() {
|
||||||
|
console.log(chalk.gray('flow: Running static typechecks.'));
|
||||||
|
},
|
||||||
|
500
|
||||||
|
);
|
||||||
|
exec(flowCmd, function(flowError, stdout, stderr) {
|
||||||
|
clearTimeout(flowSlow);
|
||||||
if (!flowError) {
|
if (!flowError) {
|
||||||
console.log('flow: Typechecks passed (' + (Date.now() - start) + 'ms).');
|
console.log(chalk.gray(
|
||||||
|
'flow: Typechecks passed (' + (Date.now() - start) + 'ms).')
|
||||||
|
);
|
||||||
return next();
|
return next();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@ -61,24 +79,38 @@ function doFlowTypecheck(res, flowroot, next) {
|
|||||||
});
|
});
|
||||||
errorNum++;
|
errorNum++;
|
||||||
});
|
});
|
||||||
var message = 'Flow found type errors. If you think these are wrong, ' +
|
var error = {
|
||||||
'make sure flow is up to date, or disable with --skipflow.';
|
status: 500,
|
||||||
|
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.',
|
||||||
|
type: 'FlowError',
|
||||||
|
errors: errors,
|
||||||
|
};
|
||||||
|
console.error(chalk.yellow('flow: Error running command `' + flowCmd +
|
||||||
|
'`:\n' + JSON.stringify(error))
|
||||||
|
);
|
||||||
|
res.writeHead(error.status, {
|
||||||
|
'Content-Type': 'application/json; charset=UTF-8',
|
||||||
|
});
|
||||||
|
res.end(JSON.stringify(error));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
var message =
|
if (stderr.match(/Could not find a \.flowconfig/)) {
|
||||||
'Flow failed to provide parseable output:\n\n`' + stdout + '`';
|
if (!hasWarned.noConfig) {
|
||||||
console.error(message, '\nException: `', e, '`\n\n');
|
hasWarned.noConfig = true;
|
||||||
|
console.warn(chalk.yellow('flow: ' + stderr));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!hasWarned.brokenFlow) {
|
||||||
|
hasWarned.brokenFlow = true;
|
||||||
|
console.warn(chalk.yellow(
|
||||||
|
'Flow failed to provide parseable output:\n\n`' + stdout +
|
||||||
|
'`.\n' + 'stderr: `' + stderr + '`'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next();
|
||||||
}
|
}
|
||||||
var error = {
|
|
||||||
status: 500,
|
|
||||||
message: message,
|
|
||||||
type: 'FlowError',
|
|
||||||
errors: errors,
|
|
||||||
};
|
|
||||||
console.error('flow: Error running command `' + flowCmd + '`:\n', error);
|
|
||||||
res.writeHead(error.status, {
|
|
||||||
'Content-Type': 'application/json; charset=UTF-8',
|
|
||||||
});
|
|
||||||
res.end(JSON.stringify(error));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user