2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +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-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-02-08 01:23:25 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
if (!fs.existsSync(path.resolve(__dirname, '..', 'node_modules'))) {
|
|
|
|
console.log(
|
|
|
|
'\n' +
|
|
|
|
'Could not find dependencies.\n' +
|
|
|
|
'Ensure dependencies are installed - ' +
|
|
|
|
'run \'npm install\' from project root.\n'
|
|
|
|
);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2015-03-20 15:43:51 +00:00
|
|
|
var exec = require('child_process').exec;
|
2015-01-30 01:10:49 +00:00
|
|
|
var ReactPackager = require('./react-packager');
|
|
|
|
var blacklist = require('./blacklist.js');
|
|
|
|
var connect = require('connect');
|
|
|
|
var http = require('http');
|
|
|
|
var launchEditor = require('./launchEditor.js');
|
|
|
|
var parseCommandLine = require('./parseCommandLine.js');
|
2015-03-20 15:43:51 +00:00
|
|
|
var webSocketProxy = require('./webSocketProxy.js');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
var options = parseCommandLine([{
|
|
|
|
command: 'port',
|
|
|
|
default: 8081,
|
2015-02-19 19:21:34 +00:00
|
|
|
}, {
|
|
|
|
command: 'root',
|
|
|
|
description: 'add another root(s) to be used by the packager in this project',
|
2015-03-26 13:32:01 +00:00
|
|
|
}, {
|
|
|
|
command: 'assetRoots',
|
|
|
|
description: 'specify the root directories of app assets'
|
2015-01-30 01:10:49 +00:00
|
|
|
}]);
|
|
|
|
|
2015-03-20 15:43:51 +00:00
|
|
|
if (options.projectRoots) {
|
|
|
|
if (!Array.isArray(options.projectRoots)) {
|
|
|
|
options.projectRoots = options.projectRoots.split(',');
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-21 17:07:45 +00:00
|
|
|
if (__dirname.match(/node_modules\/react-native\/packager$/)) {
|
|
|
|
// packager is running from node_modules of another project
|
|
|
|
options.projectRoots = [path.resolve(__dirname, '../../..')];
|
|
|
|
} else {
|
|
|
|
options.projectRoots = [path.resolve(__dirname, '..')];
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 19:21:34 +00:00
|
|
|
if (options.root) {
|
|
|
|
if (typeof options.root === 'string') {
|
|
|
|
options.projectRoots.push(path.resolve(options.root));
|
|
|
|
} else {
|
|
|
|
options.root.forEach(function(root) {
|
|
|
|
options.projectRoots.push(path.resolve(root));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 13:32:01 +00:00
|
|
|
if (options.assetRoots) {
|
|
|
|
if (!Array.isArray(options.assetRoots)) {
|
|
|
|
options.assetRoots = options.assetRoots.split(',');
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-28 05:11:33 +00:00
|
|
|
if (__dirname.match(/node_modules\/react-native\/packager$/)) {
|
|
|
|
options.assetRoots = [path.resolve(__dirname, '../../..')];
|
|
|
|
} else {
|
|
|
|
options.assetRoots = [path.resolve(__dirname, '..')];
|
|
|
|
}
|
2015-03-20 15:43:51 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
console.log('\n' +
|
|
|
|
' ===============================================================\n' +
|
|
|
|
' | Running packager on port ' + options.port + '. \n' +
|
|
|
|
' | Keep this packager running while developing on any JS \n' +
|
|
|
|
' | projects. Feel free to close this tab and run your own \n' +
|
|
|
|
' | packager instance if you prefer. \n' +
|
|
|
|
' | \n' +
|
|
|
|
' | https://github.com/facebook/react-native \n' +
|
|
|
|
' | \n' +
|
|
|
|
' ===============================================================\n'
|
|
|
|
);
|
|
|
|
|
2015-03-23 17:06:16 +00:00
|
|
|
console.log('Looking for JS files in\n ', options.projectRoots.join('\n '));
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
process.on('uncaughtException', function(e) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.stack);
|
|
|
|
console.error('\n >>> ERROR: could not create packager - please shut down ' +
|
|
|
|
'any existing instances that are already running.\n\n');
|
|
|
|
});
|
|
|
|
|
2015-03-20 15:43:51 +00:00
|
|
|
var server = runServer(options, function() {
|
2015-01-30 01:10:49 +00:00
|
|
|
console.log('\nReact packager ready.\n');
|
|
|
|
});
|
|
|
|
|
2015-03-20 15:43:51 +00:00
|
|
|
webSocketProxy.attachToServer(server, '/debugger-proxy');
|
|
|
|
|
2015-01-30 01:10:49 +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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function openStackFrameInEditor(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 15:43:51 +00:00
|
|
|
function getDevToolsLauncher(options) {
|
|
|
|
return function(req, res, next) {
|
|
|
|
if (req.url === '/debugger-ui') {
|
|
|
|
var debuggerPath = path.join(__dirname, 'debugger.html');
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/html'});
|
|
|
|
fs.createReadStream(debuggerPath).pipe(res);
|
|
|
|
} else if (req.url === '/launch-chrome-devtools') {
|
|
|
|
var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui';
|
|
|
|
var script = 'launchChromeDevTools.applescript';
|
|
|
|
console.log('Launching Dev Tools...');
|
|
|
|
exec(path.join(__dirname, script) + ' ' + debuggerURL, function(err, stdout, stderr) {
|
|
|
|
if (err) {
|
|
|
|
console.log('Failed to run ' + script, err);
|
|
|
|
}
|
|
|
|
console.log(stdout);
|
|
|
|
console.warn(stderr);
|
|
|
|
});
|
|
|
|
res.end('OK');
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
function getAppMiddleware(options) {
|
2015-02-07 19:22:53 +00:00
|
|
|
return ReactPackager.middleware({
|
2015-02-12 00:37:51 +00:00
|
|
|
projectRoots: options.projectRoots,
|
2015-01-30 01:10:49 +00:00
|
|
|
blacklistRE: blacklist(false),
|
|
|
|
cacheVersion: '2',
|
2015-02-19 01:43:36 +00:00
|
|
|
transformModulePath: require.resolve('./transformer.js'),
|
2015-03-20 15:43:51 +00:00
|
|
|
assetRoots: options.assetRoots,
|
2015-01-30 01:10:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runServer(
|
2015-03-06 17:54:10 +00:00
|
|
|
options, /* {[]string projectRoot, bool web} */
|
2015-01-30 01:10:49 +00:00
|
|
|
readyCallback
|
|
|
|
) {
|
|
|
|
var app = connect()
|
|
|
|
.use(loadRawBody)
|
|
|
|
.use(openStackFrameInEditor)
|
2015-03-20 15:43:51 +00:00
|
|
|
.use(getDevToolsLauncher(options))
|
2015-02-12 00:37:51 +00:00
|
|
|
.use(getAppMiddleware(options));
|
|
|
|
|
|
|
|
options.projectRoots.forEach(function(root) {
|
|
|
|
app.use(connect.static(root));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(connect.logger())
|
2015-01-30 01:10:49 +00:00
|
|
|
.use(connect.compress())
|
|
|
|
.use(connect.errorHandler());
|
|
|
|
|
|
|
|
return http.createServer(app).listen(options.port, readyCallback);
|
|
|
|
}
|