mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-26 07:04:05 +08:00
Move files from packager/ to local-cli
Summary: public This is not only to put the files on a place where it makes more sense but also to allow to use ES6 features on them as `/packager` is not whitelisted on `babel`. Reviewed By: mkonicek Differential Revision: D2577267 fb-gh-sync-id: b69a17c0aad349a3eda987e33d1778d97a8e1549
This commit is contained in:
committed by
facebook-github-bot-3
parent
f87d2e15bd
commit
bcf762af60
47
local-cli/server/middleware/cpuProfilerMiddleware.js
Normal file
47
local-cli/server/middleware/cpuProfilerMiddleware.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 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 execFile = require('child_process').execFile;
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
if (req.url !== '/cpu-profile') {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Dumping CPU profile information...');
|
||||
var dumpName = '/tmp/cpu-profile_' + Date.now();
|
||||
fs.writeFileSync(dumpName + '.json', req.rawBody);
|
||||
|
||||
var cmdPath = path.join(
|
||||
__dirname,
|
||||
'../react-native-github/JSCLegacyProfiler/json2trace'
|
||||
);
|
||||
execFile(
|
||||
cmdPath,
|
||||
[
|
||||
'-cpuprofiler',
|
||||
dumpName + '.cpuprofile ' + dumpName + '.json'
|
||||
],
|
||||
function(error) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
res.end('Unknown error: ' + error.message);
|
||||
} else {
|
||||
var response = 'Your profile was generated at\n\n' + dumpName + '.cpuprofile\n\n' +
|
||||
'Open `Chrome Dev Tools > Profiles > Load` and select the profile to visualize it.';
|
||||
console.log(response);
|
||||
res.end(response);
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
61
local-cli/server/middleware/getDevToolsMiddleware.js
Normal file
61
local-cli/server/middleware/getDevToolsMiddleware.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 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 execFile = require('child_process').execFile;
|
||||
var fs = require('fs');
|
||||
var opn = require('opn');
|
||||
var path = require('path');
|
||||
|
||||
function getChromeAppName() {
|
||||
switch (process.platform) {
|
||||
case 'darwin':
|
||||
return 'google chrome';
|
||||
case 'win32':
|
||||
return 'chrome';
|
||||
default:
|
||||
return 'google-chrome';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function(options, isDebuggerConnected) {
|
||||
return function(req, res, next) {
|
||||
if (req.url === '/debugger-ui') {
|
||||
var debuggerPath = path.join(__dirname, '..', 'util', 'debugger.html');
|
||||
res.writeHead(200, {'Content-Type': 'text/html'});
|
||||
fs.createReadStream(debuggerPath).pipe(res);
|
||||
} else if (req.url === '/debuggerWorker.js') {
|
||||
var workerPath = path.join(__dirname, '..', 'util', 'debuggerWorker.js');
|
||||
res.writeHead(200, {'Content-Type': 'application/javascript'});
|
||||
fs.createReadStream(workerPath).pipe(res);
|
||||
} else if (req.url === '/launch-safari-devtools') {
|
||||
// TODO: remove `console.log` and dev tools binary
|
||||
console.log(
|
||||
'We removed support for Safari dev-tools. ' +
|
||||
'If you still need this, please let us know.'
|
||||
);
|
||||
} else if (req.url === '/launch-chrome-devtools') {
|
||||
if (isDebuggerConnected()) {
|
||||
// Dev tools are already open; no need to open another session
|
||||
res.end('OK');
|
||||
return;
|
||||
}
|
||||
var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui';
|
||||
console.log('Launching Dev Tools...');
|
||||
opn(debuggerURL, {app: [getChromeAppName()]}, function(err) {
|
||||
if (err) {
|
||||
console.error('Google Chrome exited with error:', err);
|
||||
}
|
||||
});
|
||||
res.end('OK');
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
};
|
||||
150
local-cli/server/middleware/getFlowTypeCheckMiddleware.js
Normal file
150
local-cli/server/middleware/getFlowTypeCheckMiddleware.js
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* 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 chalk = require('chalk');
|
||||
var exec = require('child_process').exec;
|
||||
var url = require('url');
|
||||
var Activity = require('../../../packager/react-packager').Activity;
|
||||
|
||||
var hasWarned = {};
|
||||
|
||||
function getFlowTypeCheckMiddleware(options) {
|
||||
return function(req, res, next) {
|
||||
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 {
|
||||
if (!hasWarned.noRoot) {
|
||||
hasWarned.noRoot = true;
|
||||
console.warn('flow: No suitable root');
|
||||
}
|
||||
_endFlowBad(res);
|
||||
return;
|
||||
}
|
||||
exec('command -v flow >/dev/null 2>&1', function(error, stdout) {
|
||||
if (error) {
|
||||
if (!hasWarned.noFlow) {
|
||||
hasWarned.noFlow = true;
|
||||
console.warn(chalk.yellow('flow: Skipping because not installed. Install with ' +
|
||||
'`brew install flow`.'));
|
||||
}
|
||||
_endFlowBad(res);
|
||||
return;
|
||||
} else {
|
||||
return doFlowTypecheck(res, flowroot, next);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function doFlowTypecheck(res, flowroot, next) {
|
||||
var flowCmd = 'cd "' + flowroot + '" && flow --json --timeout 20';
|
||||
var eventId = Activity.startEvent('flow static typechecks');
|
||||
exec(flowCmd, function(flowError, stdout, stderr) {
|
||||
Activity.endEvent(eventId);
|
||||
if (!flowError) {
|
||||
_endFlowOk(res);
|
||||
return;
|
||||
} 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 error = {
|
||||
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.',
|
||||
type: 'FlowError',
|
||||
errors: errors,
|
||||
};
|
||||
res.writeHead(error.status, {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
});
|
||||
res.end(JSON.stringify(error));
|
||||
} catch (e) {
|
||||
if (stderr.match(/Could not find a \.flowconfig/)) {
|
||||
if (!hasWarned.noConfig) {
|
||||
hasWarned.noConfig = true;
|
||||
console.warn(chalk.yellow('flow: ' + stderr));
|
||||
}
|
||||
_endFlowBad(res);
|
||||
} else if (flowError.code === 3) {
|
||||
if (!hasWarned.timeout) {
|
||||
hasWarned.timeout = true;
|
||||
console.warn(chalk.yellow('flow: ' + stdout));
|
||||
}
|
||||
_endSkipFlow(res);
|
||||
} else {
|
||||
if (!hasWarned.brokenFlow) {
|
||||
hasWarned.brokenFlow = true;
|
||||
console.warn(chalk.yellow(
|
||||
'Flow failed to provide parseable output:\n\n`' + stdout +
|
||||
'`.\n' + 'stderr: `' + stderr + '`'
|
||||
));
|
||||
}
|
||||
_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;
|
||||
22
local-cli/server/middleware/loadRawBodyMiddleware.js
Normal file
22
local-cli/server/middleware/loadRawBodyMiddleware.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* 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';
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
req.rawBody = '';
|
||||
req.setEncoding('utf8');
|
||||
|
||||
req.on('data', function(chunk) {
|
||||
req.rawBody += chunk;
|
||||
});
|
||||
|
||||
req.on('end', function() {
|
||||
next();
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 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 launchEditor = require('../util/launchEditor');
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
if (req.url === '/open-stack-frame') {
|
||||
var frame = JSON.parse(req.rawBody);
|
||||
launchEditor(frame.file, frame.lineNumber);
|
||||
res.end('OK');
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
21
local-cli/server/middleware/statusPageMiddleware.js
Normal file
21
local-cli/server/middleware/statusPageMiddleware.js
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 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';
|
||||
|
||||
/**
|
||||
* Status page so that anyone who needs to can verify that the packager is
|
||||
* running on 8081 and not another program / service.
|
||||
*/
|
||||
module.exports = function(req, res, next) {
|
||||
if (req.url === '/status') {
|
||||
res.end('packager-status:running');
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
52
local-cli/server/middleware/systraceProfileMiddleware.js
Normal file
52
local-cli/server/middleware/systraceProfileMiddleware.js
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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 exec = require('child_process').exec;
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
if (req.url !== '/systrace') {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Dumping profile information...');
|
||||
var dumpName = '/tmp/dump_' + Date.now() + '.json';
|
||||
var prefix = process.env.TRACE_VIEWER_PATH || '';
|
||||
var cmd = path.join(prefix, 'trace2html') + ' ' + dumpName;
|
||||
fs.writeFileSync(dumpName, req.rawBody);
|
||||
exec(cmd, function(error) {
|
||||
if (error) {
|
||||
if (error.code === 127) {
|
||||
var response = '\n** Failed executing `' + cmd + '` **\n\n' +
|
||||
'Google trace-viewer is required to visualize the data, ' +
|
||||
'You can install it with `brew install trace2html`\n\n' +
|
||||
'NOTE: Your profile data was kept at:\n' + dumpName;
|
||||
console.log(response);
|
||||
res.end(response);
|
||||
} else {
|
||||
console.error(error);
|
||||
res.end('Unknown error: ' + error.message);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
exec('rm ' + dumpName);
|
||||
exec('open ' + dumpName.replace(/json$/, 'html'), function(err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.end(err.message);
|
||||
} else {
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user