2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 18:48:02 +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.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-10-08 21:15:13 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const childProcess = require('child_process');
|
|
|
|
const http = require('http');
|
|
|
|
const isAbsolutePath = require('absolute-path');
|
|
|
|
|
|
|
|
const blacklist = require('./blacklist.js');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const checkNodeVersion = require('./checkNodeVersion');
|
|
|
|
const connect = require('connect');
|
|
|
|
const formatBanner = require('./formatBanner');
|
|
|
|
const getDevToolsMiddleware = require('./getDevToolsMiddleware');
|
2015-10-08 21:15:14 +00:00
|
|
|
const openStackFrameInEditorMiddleware = require('./openStackFrameInEditorMiddleware');
|
2015-10-08 21:15:13 +00:00
|
|
|
const parseCommandLine = require('./parseCommandLine.js');
|
|
|
|
const ReactPackager = require('./react-packager');
|
2015-10-08 21:15:15 +00:00
|
|
|
const statusPageMiddleware = require('./statusPageMiddleware.js');
|
2015-10-08 21:15:13 +00:00
|
|
|
const webSocketProxy = require('./webSocketProxy.js');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
var options = parseCommandLine([{
|
|
|
|
command: 'port',
|
|
|
|
default: 8081,
|
2015-05-26 21:16:47 +00:00
|
|
|
type: 'string',
|
2015-02-20 04:10:52 +00:00
|
|
|
}, {
|
|
|
|
command: 'root',
|
2015-05-26 21:16:47 +00:00
|
|
|
type: 'string',
|
2015-02-20 04:10:52 +00:00
|
|
|
description: 'add another root(s) to be used by the packager in this project',
|
2015-03-26 09:11:53 +00:00
|
|
|
}, {
|
|
|
|
command: 'assetRoots',
|
2015-05-26 21:16:47 +00:00
|
|
|
type: 'string',
|
2015-03-26 09:11:53 +00:00
|
|
|
description: 'specify the root directories of app assets'
|
2015-04-08 04:50:24 +00:00
|
|
|
}, {
|
|
|
|
command: 'skipflow',
|
|
|
|
description: 'Disable flow checks'
|
2015-06-11 17:43:15 +00:00
|
|
|
}, {
|
|
|
|
command: 'nonPersistent',
|
|
|
|
description: 'Disable file watcher'
|
2015-06-26 22:56:22 +00:00
|
|
|
}, {
|
|
|
|
command: 'transformer',
|
|
|
|
type: 'string',
|
|
|
|
default: require.resolve('./transformer.js'),
|
|
|
|
description: 'Specify a custom transformer to be used (absolute path)'
|
2015-09-07 08:23:21 +00:00
|
|
|
}, {
|
|
|
|
command: 'resetCache',
|
|
|
|
description: 'Removes cached files',
|
|
|
|
default: false,
|
|
|
|
}, {
|
|
|
|
command: 'reset-cache',
|
|
|
|
description: 'Removes cached files',
|
|
|
|
default: false,
|
2015-02-20 04:10:52 +00:00
|
|
|
}]);
|
|
|
|
|
2015-03-20 21:29:42 +00:00
|
|
|
if (options.projectRoots) {
|
|
|
|
if (!Array.isArray(options.projectRoots)) {
|
|
|
|
options.projectRoots = options.projectRoots.split(',');
|
|
|
|
}
|
|
|
|
} else {
|
2015-06-26 23:18:49 +00:00
|
|
|
// match on either path separator
|
|
|
|
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]packager$/)) {
|
|
|
|
// packager is running from node_modules of another project
|
2015-03-20 23:31:31 +00:00
|
|
|
options.projectRoots = [path.resolve(__dirname, '../../..')];
|
2015-06-22 10:12:35 +00:00
|
|
|
} else if (__dirname.match(/Pods\/React\/packager$/)) {
|
|
|
|
// packager is running from node_modules of another project
|
|
|
|
options.projectRoots = [path.resolve(__dirname, '../../..')];
|
2015-03-20 23:31:31 +00:00
|
|
|
} else {
|
|
|
|
options.projectRoots = [path.resolve(__dirname, '..')];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.root) {
|
2015-05-26 21:16:47 +00:00
|
|
|
if (!Array.isArray(options.root)) {
|
|
|
|
options.root = options.root.split(',');
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-05-26 21:16:47 +00:00
|
|
|
|
|
|
|
options.root.forEach(function(root) {
|
|
|
|
options.projectRoots.push(path.resolve(root));
|
|
|
|
});
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 09:11:53 +00:00
|
|
|
if (options.assetRoots) {
|
|
|
|
if (!Array.isArray(options.assetRoots)) {
|
2015-06-22 19:58:12 +00:00
|
|
|
options.assetRoots = options.assetRoots.split(',').map(function (dir) {
|
|
|
|
return path.resolve(process.cwd(), dir);
|
|
|
|
});
|
2015-03-26 09:11:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-26 23:18:49 +00:00
|
|
|
// match on either path separator
|
|
|
|
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]packager$/)) {
|
2015-03-28 02:27:17 +00:00
|
|
|
options.assetRoots = [path.resolve(__dirname, '../../..')];
|
2015-06-22 10:12:35 +00:00
|
|
|
} else if (__dirname.match(/Pods\/React\/packager$/)) {
|
|
|
|
options.assetRoots = [path.resolve(__dirname, '../../..')];
|
2015-03-28 02:27:17 +00:00
|
|
|
} else {
|
|
|
|
options.assetRoots = [path.resolve(__dirname, '..')];
|
|
|
|
}
|
2015-03-19 02:13:30 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 07:23:58 +00:00
|
|
|
checkNodeVersion();
|
|
|
|
|
|
|
|
console.log(formatBanner(
|
|
|
|
'Running packager on port ' + options.port + '.\n'+
|
|
|
|
'\n' +
|
|
|
|
'Keep this packager running while developing on any JS projects. Feel free ' +
|
|
|
|
'to close this tab and run your own packager instance if you prefer.\n' +
|
|
|
|
'\n' +
|
|
|
|
'https://github.com/facebook/react-native', {
|
|
|
|
marginLeft: 1,
|
|
|
|
marginRight: 1,
|
|
|
|
paddingBottom: 1,
|
|
|
|
})
|
2015-02-20 04:10:52 +00:00
|
|
|
);
|
|
|
|
|
2015-04-07 22:52:52 +00:00
|
|
|
console.log(
|
|
|
|
'Looking for JS files in\n ',
|
|
|
|
chalk.dim(options.projectRoots.join('\n ')),
|
|
|
|
'\n'
|
|
|
|
);
|
2015-03-23 05:56:41 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
process.on('uncaughtException', function(e) {
|
2015-04-07 22:52:52 +00:00
|
|
|
if (e.code === 'EADDRINUSE') {
|
|
|
|
console.log(
|
|
|
|
chalk.bgRed.bold(' ERROR '),
|
|
|
|
chalk.red('Packager can\'t listen on port', chalk.bold(options.port))
|
|
|
|
);
|
|
|
|
console.log('Most likely another process is already using this port');
|
|
|
|
console.log('Run the following command to find out which process:');
|
|
|
|
console.log('\n ', chalk.bold('lsof -n -i4TCP:' + options.port), '\n');
|
|
|
|
console.log('You can either shut down the other process:');
|
|
|
|
console.log('\n ', chalk.bold('kill -9 <PID>'), '\n');
|
|
|
|
console.log('or run packager on different port.');
|
|
|
|
} else {
|
|
|
|
console.log(chalk.bgRed.bold(' ERROR '), chalk.red(e.message));
|
|
|
|
var errorAttributes = JSON.stringify(e);
|
|
|
|
if (errorAttributes !== '{}') {
|
|
|
|
console.error(chalk.red(errorAttributes));
|
|
|
|
}
|
|
|
|
console.error(chalk.red(e.stack));
|
|
|
|
}
|
|
|
|
console.log('\nSee', chalk.underline('http://facebook.github.io/react-native/docs/troubleshooting.html'));
|
|
|
|
console.log('for common problems and solutions.');
|
|
|
|
process.exit(1);
|
2015-02-20 04:10:52 +00:00
|
|
|
});
|
|
|
|
|
2015-03-19 19:10:41 +00:00
|
|
|
var server = runServer(options, function() {
|
2015-02-20 04:10:52 +00:00
|
|
|
console.log('\nReact packager ready.\n');
|
|
|
|
});
|
|
|
|
|
2015-03-19 19:10:41 +00:00
|
|
|
webSocketProxy.attachToServer(server, '/debugger-proxy');
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
function loadRawBody(req, res, next) {
|
|
|
|
req.rawBody = '';
|
|
|
|
req.setEncoding('utf8');
|
|
|
|
|
|
|
|
req.on('data', function(chunk) {
|
|
|
|
req.rawBody += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('end', function() {
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-09 15:50:15 +00:00
|
|
|
function systraceProfileMiddleware(req, res, next) {
|
2015-09-11 13:35:25 +00:00
|
|
|
if (req.url !== '/systrace') {
|
2015-09-09 15:50:15 +00:00
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Dumping profile information...');
|
2015-09-23 17:54:26 +00:00
|
|
|
var dumpName = '/tmp/dump_' + Date.now() + '.json';
|
|
|
|
var prefix = process.env.TRACE_VIEWER_PATH || '';
|
|
|
|
var cmd = path.join(prefix, 'trace2html') + ' ' + dumpName;
|
2015-09-09 15:50:15 +00:00
|
|
|
fs.writeFileSync(dumpName, req.rawBody);
|
|
|
|
childProcess.exec(cmd, function(error) {
|
|
|
|
if (error) {
|
|
|
|
if (error.code === 127) {
|
2015-10-01 19:48:37 +00:00
|
|
|
var response = '\n** Failed executing `' + cmd + '` **\n\n' +
|
2015-09-11 13:35:25 +00:00
|
|
|
'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
|
2015-10-01 19:48:37 +00:00
|
|
|
console.log(response);
|
|
|
|
res.end(response);
|
2015-09-09 15:50:15 +00:00
|
|
|
} else {
|
2015-09-11 13:35:25 +00:00
|
|
|
console.error(error);
|
2015-10-01 19:48:37 +00:00
|
|
|
res.end('Unknown error: ' + error.message);
|
2015-09-09 15:50:15 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
childProcess.exec('rm ' + dumpName);
|
|
|
|
childProcess.exec('open ' + dumpName.replace(/json$/, 'html'), function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
2015-09-11 13:35:25 +00:00
|
|
|
res.end(err.message);
|
|
|
|
} else {
|
|
|
|
res.end();
|
2015-09-09 15:50:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-11 13:35:25 +00:00
|
|
|
function cpuProfileMiddleware(req, res, next) {
|
|
|
|
if (req.url !== '/cpu-profile') {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Dumping CPU profile information...');
|
2015-09-23 17:54:26 +00:00
|
|
|
var dumpName = '/tmp/cpu-profile_' + Date.now();
|
2015-09-11 13:35:25 +00:00
|
|
|
fs.writeFileSync(dumpName + '.json', req.rawBody);
|
|
|
|
|
2015-10-01 19:48:37 +00:00
|
|
|
var cmd = path.join(__dirname, '..', 'react-native-github', 'JSCLegacyProfiler', 'json2trace') + ' -cpuprofiler ' + dumpName + '.cpuprofile ' + dumpName + '.json';
|
2015-09-11 13:35:25 +00:00
|
|
|
childProcess.exec(cmd, function(error) {
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
2015-10-01 19:48:37 +00:00
|
|
|
res.end('Unknown error: ' + error.message);
|
2015-09-11 13:35:25 +00:00
|
|
|
} else {
|
2015-10-01 19:48:37 +00:00
|
|
|
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);
|
2015-09-11 13:35:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
function getAppMiddleware(options) {
|
2015-06-26 22:56:22 +00:00
|
|
|
var transformerPath = options.transformer;
|
|
|
|
if (!isAbsolutePath(transformerPath)) {
|
|
|
|
transformerPath = path.resolve(process.cwd(), transformerPath);
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
return ReactPackager.middleware({
|
2015-06-11 17:43:15 +00:00
|
|
|
nonPersistent: options.nonPersistent,
|
2015-02-20 04:10:52 +00:00
|
|
|
projectRoots: options.projectRoots,
|
2015-08-26 11:53:41 +00:00
|
|
|
blacklistRE: blacklist(),
|
2015-09-24 03:15:18 +00:00
|
|
|
cacheVersion: '3',
|
2015-06-26 22:56:22 +00:00
|
|
|
transformModulePath: transformerPath,
|
2015-03-19 02:13:30 +00:00
|
|
|
assetRoots: options.assetRoots,
|
2015-05-14 00:45:36 +00:00
|
|
|
assetExts: ['png', 'jpeg', 'jpg'],
|
2015-09-07 08:23:21 +00:00
|
|
|
resetCache: options.resetCache || options['reset-cache'],
|
2015-05-14 00:45:36 +00:00
|
|
|
polyfillModuleNames: [
|
|
|
|
require.resolve(
|
|
|
|
'../Libraries/JavaScriptAppEngine/polyfills/document.js'
|
|
|
|
),
|
|
|
|
],
|
2015-02-20 04:10:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runServer(
|
2015-04-14 15:52:40 +00:00
|
|
|
options,
|
2015-02-20 04:10:52 +00:00
|
|
|
readyCallback
|
|
|
|
) {
|
|
|
|
var app = connect()
|
|
|
|
.use(loadRawBody)
|
2015-10-08 21:15:13 +00:00
|
|
|
.use(getDevToolsMiddleware(options))
|
2015-10-08 21:15:14 +00:00
|
|
|
.use(openStackFrameInEditorMiddleware)
|
2015-04-01 06:41:15 +00:00
|
|
|
.use(statusPageMiddleware)
|
2015-09-09 15:50:15 +00:00
|
|
|
.use(systraceProfileMiddleware)
|
2015-09-11 13:35:25 +00:00
|
|
|
.use(cpuProfileMiddleware)
|
2015-04-28 02:55:24 +00:00
|
|
|
// Temporarily disable flow check until it's more stable
|
|
|
|
//.use(getFlowTypeCheckMiddleware(options))
|
2015-02-20 04:10:52 +00:00
|
|
|
.use(getAppMiddleware(options));
|
|
|
|
|
|
|
|
options.projectRoots.forEach(function(root) {
|
|
|
|
app.use(connect.static(root));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(connect.logger())
|
|
|
|
.use(connect.compress())
|
|
|
|
.use(connect.errorHandler());
|
|
|
|
|
2015-04-08 21:24:00 +00:00
|
|
|
return http.createServer(app).listen(options.port, '::', readyCallback);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|