mirror of
https://github.com/status-im/metro.git
synced 2025-01-18 15:11:17 +00:00
[ReactNative] Do flow check when running packager
This commit is contained in:
parent
5cbbc10e4f
commit
061de15c1c
86
getFlowTypeCheckMiddleware.js
Normal file
86
getFlowTypeCheckMiddleware.js
Normal file
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* 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';
|
||||
|
||||
var exec = require('child_process').exec;
|
||||
|
||||
function getFlowTypeCheckMiddleware(options) {
|
||||
return function(req, res, next) {
|
||||
if (options.skipflow) {
|
||||
return next();
|
||||
}
|
||||
if (options.flowroot || options.projectRoots.length === 1) {
|
||||
var flowroot = options.flowroot || options.projectRoots[0];
|
||||
} else {
|
||||
console.warn('flow: No suitable root');
|
||||
return next();
|
||||
}
|
||||
exec('command -v flow >/dev/null 2>&1', function(error, stdout) {
|
||||
if (error) {
|
||||
console.warn('flow: Skipping because not installed. Install with ' +
|
||||
'`brew install flow`.');
|
||||
return next();
|
||||
} else {
|
||||
return doFlowTypecheck(res, flowroot, next);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function doFlowTypecheck(res, flowroot, next) {
|
||||
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
||||
var start = Date.now();
|
||||
console.log('flow: Running static typechecks.');
|
||||
exec(flowCmd, function(flowError, stdout) {
|
||||
if (!flowError) {
|
||||
console.log('flow: Typechecks passed (' + (Date.now() - start) + 'ms).');
|
||||
return next();
|
||||
} else {
|
||||
try {
|
||||
var flowResponse = JSON.parse(stdout);
|
||||
var errors = [];
|
||||
var errorNum = 1;
|
||||
flowResponse.errors.forEach(function(err) {
|
||||
// flow errors are paired across callsites, so we indent and prefix to
|
||||
// group them
|
||||
var indent = '';
|
||||
err.message.forEach(function(msg) {
|
||||
errors.push({
|
||||
description: indent + 'E' + errorNum + ': ' + msg.descr,
|
||||
filename: msg.path,
|
||||
lineNumber: msg.line,
|
||||
column: msg.start,
|
||||
});
|
||||
indent = ' ';
|
||||
});
|
||||
errorNum++;
|
||||
});
|
||||
var message = 'Flow found type errors. If you think these are wrong, ' +
|
||||
'make sure flow is up to date, or disable with --skipflow.';
|
||||
} catch (e) {
|
||||
var message =
|
||||
'Flow failed to provide parseable output:\n\n`' + stdout + '`';
|
||||
console.error(message, '\nException: `', e, '`\n\n');
|
||||
}
|
||||
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));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = getFlowTypeCheckMiddleware;
|
@ -13,6 +13,8 @@ var path = require('path');
|
||||
var exec = require('child_process').exec;
|
||||
var http = require('http');
|
||||
|
||||
var getFlowTypeCheckMiddleware = require('./getFlowTypeCheckMiddleware');
|
||||
|
||||
if (!fs.existsSync(path.resolve(__dirname, '..', 'node_modules'))) {
|
||||
console.log(
|
||||
'\n' +
|
||||
@ -40,6 +42,9 @@ var options = parseCommandLine([{
|
||||
}, {
|
||||
command: 'assetRoots',
|
||||
description: 'specify the root directories of app assets'
|
||||
}, {
|
||||
command: 'skipflow',
|
||||
description: 'Disable flow checks'
|
||||
}]);
|
||||
|
||||
if (options.projectRoots) {
|
||||
@ -203,6 +208,7 @@ function runServer(
|
||||
.use(openStackFrameInEditor)
|
||||
.use(getDevToolsLauncher(options))
|
||||
.use(statusPageMiddleware)
|
||||
.use(getFlowTypeCheckMiddleware(options))
|
||||
.use(getAppMiddleware(options));
|
||||
|
||||
options.projectRoots.forEach(function(root) {
|
||||
|
6
react-packager/src/Server/index.js
vendored
6
react-packager/src/Server/index.js
vendored
@ -320,6 +320,12 @@ function handleError(res, error) {
|
||||
});
|
||||
|
||||
if (error.type === 'TransformError' || error.type === 'NotFoundError') {
|
||||
error.errors = [{
|
||||
description: error.description,
|
||||
filename: error.filename,
|
||||
lineNumber: error.lineNumber,
|
||||
}];
|
||||
console.error(error);
|
||||
res.end(JSON.stringify(error));
|
||||
} else {
|
||||
console.error(error.stack || error);
|
||||
|
Loading…
x
Reference in New Issue
Block a user