2015-04-08 04:50:24 +00:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
|
2015-04-10 00:49:20 +00:00
|
|
|
var chalk = require('chalk');
|
2015-04-08 04:50:24 +00:00
|
|
|
var exec = require('child_process').exec;
|
2015-08-17 09:05:29 +00:00
|
|
|
var url = require('url');
|
2015-08-19 00:36:35 +00:00
|
|
|
var Activity = require('./react-packager').Activity;
|
2015-04-08 04:50:24 +00:00
|
|
|
|
2015-04-10 00:49:20 +00:00
|
|
|
var hasWarned = {};
|
|
|
|
|
2015-04-08 04:50:24 +00:00
|
|
|
function getFlowTypeCheckMiddleware(options) {
|
|
|
|
return function(req, res, next) {
|
2015-08-17 09:05:29 +00:00
|
|
|
var reqObj = url.parse(req.url);
|
|
|
|
var isFlowCheck = (reqObj.path.match(/^\/flow\//));
|
|
|
|
|
|
|
|
if (!isFlowCheck) {
|
2015-04-08 04:50:24 +00:00
|
|
|
return next();
|
|
|
|
}
|
2015-08-17 09:05:29 +00:00
|
|
|
if (options.skipflow) {
|
|
|
|
_endSkipFlow(res);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-08 04:50:24 +00:00
|
|
|
if (options.flowroot || options.projectRoots.length === 1) {
|
|
|
|
var flowroot = options.flowroot || options.projectRoots[0];
|
|
|
|
} else {
|
2015-04-10 00:49:20 +00:00
|
|
|
if (!hasWarned.noRoot) {
|
|
|
|
hasWarned.noRoot = true;
|
|
|
|
console.warn('flow: No suitable root');
|
|
|
|
}
|
2015-08-17 09:05:29 +00:00
|
|
|
_endFlowBad(res);
|
|
|
|
return;
|
2015-04-08 04:50:24 +00:00
|
|
|
}
|
|
|
|
exec('command -v flow >/dev/null 2>&1', function(error, stdout) {
|
|
|
|
if (error) {
|
2015-04-10 00:49:20 +00:00
|
|
|
if (!hasWarned.noFlow) {
|
|
|
|
hasWarned.noFlow = true;
|
|
|
|
console.warn(chalk.yellow('flow: Skipping because not installed. Install with ' +
|
|
|
|
'`brew install flow`.'));
|
|
|
|
}
|
2015-08-17 09:05:29 +00:00
|
|
|
_endFlowBad(res);
|
|
|
|
return;
|
2015-04-08 04:50:24 +00:00
|
|
|
} else {
|
|
|
|
return doFlowTypecheck(res, flowroot, next);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function doFlowTypecheck(res, flowroot, next) {
|
|
|
|
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
2015-04-18 00:01:18 +00:00
|
|
|
var eventId = Activity.startEvent('flow static typechecks');
|
2015-04-10 00:49:20 +00:00
|
|
|
exec(flowCmd, function(flowError, stdout, stderr) {
|
2015-04-18 00:01:18 +00:00
|
|
|
Activity.endEvent(eventId);
|
2015-04-08 04:50:24 +00:00
|
|
|
if (!flowError) {
|
2015-08-17 09:05:29 +00:00
|
|
|
_endFlowOk(res);
|
|
|
|
return;
|
2015-04-08 04:50:24 +00:00
|
|
|
} 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++;
|
|
|
|
});
|
2015-04-10 00:49:20 +00:00
|
|
|
var error = {
|
2015-08-17 09:05:29 +00:00
|
|
|
status: 200,
|
2015-04-10 00:49:20 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
res.writeHead(error.status, {
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
});
|
|
|
|
res.end(JSON.stringify(error));
|
2015-04-08 04:50:24 +00:00
|
|
|
} catch (e) {
|
2015-04-10 00:49:20 +00:00
|
|
|
if (stderr.match(/Could not find a \.flowconfig/)) {
|
|
|
|
if (!hasWarned.noConfig) {
|
|
|
|
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 + '`'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 09:05:29 +00:00
|
|
|
_endFlowBad(res);
|
|
|
|
return;
|
2015-04-08 04:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-17 09:05:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-08 04:50:24 +00:00
|
|
|
module.exports = getFlowTypeCheckMiddleware;
|