Reviewed By: frantic

Differential Revision: D2561113

fb-gh-sync-id: a81fece0a948485e12bff7ec146e39d890664887
This commit is contained in:
Martín Bigio 2015-10-20 16:58:12 -07:00 коммит произвёл facebook-github-bot-3
Родитель 088c193a22
Коммит 8e7cfcd053
6 изменённых файлов: 0 добавлений и 571 удалений

Просмотреть файл

@ -1,125 +0,0 @@
/**
* 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.
*/
var http = require('http');
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var blacklist = require('../packager/blacklist.js');
var ReactPackager = require('../packager/react-packager');
var OUT_PATH = {
android: 'android/app/src/main/assets/index.android.bundle',
ios: 'ios/main.jsbundle'
};
var URL_PATH = {
android: '/index.android.bundle?platform=android&dev=',
ios: '/index.ios.bundle?platform=ios&dev='
};
var SOURCEMAP_OUT_PATH = {
android: 'android/index.android.map',
ios: 'ios/index.ios.map'
};
function getBundle(flags) {
var platform = flags.platform ? flags.platform : 'ios';
var outPath = flags.out ? flags.out : OUT_PATH[platform];
var sourceMapOutPath = SOURCEMAP_OUT_PATH[platform];
var projectRoots = [path.resolve(__dirname, '../../..')];
if (flags.root) {
projectRoots.push(path.resolve(flags.root));
}
var assetRoots = [path.resolve(__dirname, '../../..')];
if (flags.assetRoots) {
assetRoots = assetRoots.concat(flags.assetRoots.split(",").map(function(root) {
return path.resolve(root);
}));
}
var options = {
projectRoots: projectRoots,
transformModulePath: require.resolve('../packager/transformer.js'),
assetRoots: assetRoots,
cacheVersion: '3',
blacklistRE: blacklist(platform),
};
var url = flags.url ? flags.url.replace(/\.js$/i, '.bundle?dev=') : URL_PATH[platform];
url = url.match(/^\//) ? url : '/' + url;
url += flags.dev;
console.log('Building package...');
ReactPackager.buildPackageFromUrl(options, url)
.done(function(bundle) {
console.log('Build complete');
fs.writeFile(outPath, bundle.getSource({
inlineSourceMap: false,
minify: flags.minify
}), function(bundleWriteErr) {
if (bundleWriteErr) {
console.log(chalk.red('Error saving bundle to disk'));
throw bundleWriteErr;
}
console.log('Successfully saved bundle to ' + outPath);
if (flags.writeSourcemap) {
fs.writeFile(sourceMapOutPath, bundle.getSourceMap(), function(sourceMapWriteErr) {
if (sourceMapWriteErr) {
console.log(chalk.red('Error saving source map to disk'));
throw sourceMapWriteErr;
} else {
console.log('Successfully saved source map to ' + sourceMapOutPath);
}
});
}
});
});
}
function showHelp() {
console.log([
'Usage: react-native bundle [options]',
'',
'Options:',
' --dev\t\tsets DEV flag to true',
' --minify\tminify js bundle',
' --root\t\tadd another root(s) to be used in bundling in this project',
' --assetRoots\t\tspecify the root directories of app assets',
' --out\t\tspecify the output file',
' --url\t\tspecify the bundle file url',
' --platform\t\tspecify the platform(android/ios)',
' --write-sourcemap\t\twrite bundle source map to disk'
].join('\n'));
process.exit(1);
}
module.exports = {
init: function(args) {
var flags = {
help: args.indexOf('--help') !== -1,
dev: args.indexOf('--dev') !== -1,
minify: args.indexOf('--minify') !== -1,
root: args.indexOf('--root') !== -1 ? args[args.indexOf('--root') + 1] : false,
platform: args.indexOf('--platform') !== -1 ? args[args.indexOf('--platform') + 1] : false,
assetRoots: args.indexOf('--assetRoots') !== -1 ? args[args.indexOf('--assetRoots') + 1] : false,
out: args.indexOf('--out') !== -1 ? args[args.indexOf('--out') + 1] : false,
url: args.indexOf('--url') !== -1 ? args[args.indexOf('--url') + 1] : false,
writeSourcemap: args.indexOf('--write-sourcemap') !== -1,
}
if (flags.help) {
showHelp();
} else {
getBundle(flags);
}
}
}

Просмотреть файл

@ -1,69 +0,0 @@
/**
* 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 path = require('path');
var fs = require('fs');
var utils = require('./generator-utils');
function showHelp() {
console.log([
'Usage: react-native new-library <LibraryName>',
''
].join('\n'));
process.exit(1);
}
function newLibrary(libraryName) {
var root = process.cwd();
var libraries = path.resolve(root, 'Libraries');
var libraryDest = path.resolve(libraries, libraryName);
var source = path.resolve('node_modules', 'react-native', 'Libraries', 'Sample') + '/';
if (!fs.existsSync(libraries)) {
fs.mkdir(libraries);
}
if (fs.existsSync(libraryDest)) {
console.log('Library already exists in', libraryDest);
process.exit(1);
}
utils.walk(source).forEach(function(f) {
f = f.replace(source, ''); // Strip off absolute path
if (f === 'project.xcworkspace' || f.indexOf('.xcodeproj/xcuserdata') !== -1) {
return;
}
var dest = f.replace(/Sample/g, libraryName).replace(/^_/, '.');
utils.copyAndReplace(
path.resolve(source, f),
path.resolve(libraryDest, dest),
{ 'Sample': libraryName }
);
});
console.log('Created library in', libraryDest);
console.log('Next Steps:');
console.log(' Link your library in Xcode:');
console.log(' https://facebook.github.io/react-native/docs/linking-libraries-ios.html#content');
console.log('');
}
module.exports = {
init: function(args) {
var libraryName = args[1];
if (!libraryName) {
showHelp();
}
utils.validatePackageName(libraryName);
newLibrary(libraryName);
}
};

Просмотреть файл

@ -1,80 +0,0 @@
/**
* 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 child_process = require('child_process');
var fs = require('fs');
var http = require('http');
var runPackager = require('./run-packager.js');
function checkAndroid() {
return fs.existsSync('android/gradlew');
}
function buildAndRun() {
process.chdir('android');
try {
var cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew';
var gradleArgs = ['installDebug'].concat(process.argv.slice(3));
console.log(chalk.bold('Building and installing the app on the device (cd android && ' + cmd + ' ' + gradleArgs.join(' ') + ')...'));
child_process.execFileSync(cmd, gradleArgs, {
stdio: [process.stdin, process.stdout, process.stderr]
});
} catch (e) {
console.log(chalk.red('Could not install the app on the device, see the error above.'));
// stderr is automatically piped from the gradle process, so the user should see the error
// already, there is no need to do console.log(e.stderr)
return;
}
try {
var packageName = fs.readFileSync('app/src/main/AndroidManifest.xml', 'utf8').match(/package="(.+?)"/)[1];
var adbPath = process.env.ANDROID_HOME ? process.env.ANDROID_HOME + '/platform-tools/adb' : 'adb';
var adbArgs = ['shell', 'am', 'start', '-n', packageName + '/.MainActivity'];
console.log(chalk.bold('Starting the app (' + adbPath + ' ' + adbArgs.join(' ') + ')...'));
child_process.spawnSync(adbPath, adbArgs, {
stdio: [process.stdin, process.stdout, process.stderr]
});
} catch (e) {
console.log(chalk.red('adb invocation failed. Do you have adb in your PATH?'));
// stderr is automatically piped from the adb process, so the user should see the error already,
// there is no need to do console.log(e.stderr)
return;
}
}
module.exports = function() {
if (!checkAndroid()) {
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
return;
}
// is packager running?
var statusReq = http.get('http://localhost:8081/status', function(res) {
var response = '';
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
if (response === 'packager-status:running') {
console.log(chalk.bold('JS server already running.'));
} else {
console.log(chalk.yellow('[warn] JS server not recognized, continuing with build...'));
}
buildAndRun();
// make sure we don't wait around for the packager process
process.exit();
});
});
statusReq.on('error', function() {
// start packager first so it warms up
console.log(chalk.bold('Starting JS server...'));
runPackager(true);
buildAndRun();
});
};

Просмотреть файл

@ -1,56 +0,0 @@
/**
* 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 path = require('path');
var child_process = require('child_process');
/**
* Main entry point to starting the packager from JS.
* @param {boolean} newWindow If true, will start the packager in a new shell window.
*/
module.exports = function(newWindow) {
if (newWindow) {
var launchPackagerScript =
path.resolve(__dirname, '..', 'packager', 'launchPackager.command');
if (process.platform === 'darwin') {
child_process.spawnSync('open', [launchPackagerScript]);
} else if (process.platform === 'linux') {
child_process.spawn(
'xterm',
['-e', 'sh', launchPackagerScript],
{detached: true});
} else if (/^win/.test(process.platform)) {
console.log(chalk.yellow('Starting the packager in a new window ' +
'is not supported on Windows yet.\nPlease start it manually using ' +
'\'react-native start\'.'));
console.log('We believe the best Windows ' +
'support will come from a community of people\nusing React Native on ' +
'Windows on a daily basis.\n' +
'Would you be up for sending a pull request?');
} else {
console.log('Cannot start the packager. Unknown platform ' + process.platform);
}
} else {
if (/^win/.test(process.platform)) {
child_process.spawn('node', [
path.resolve(__dirname, '..', 'packager', 'packager.js'),
'--projectRoots',
process.cwd(),
], {stdio: 'inherit'});
} else {
child_process.spawn('sh', [
path.resolve(__dirname, '..', 'packager', 'packager.sh'),
'--projectRoots',
process.cwd(),
], {stdio: 'inherit'});
}
}
};

Просмотреть файл

@ -1,222 +0,0 @@
/**
* 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';
require('./babelRegisterOnly')([
/packager\/[^\/]*/
]);
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('../private-cli/src/server/checkNodeVersion');
const cpuProfilerMiddleware = require('./cpuProfilerMiddleware');
const connect = require('connect');
const formatBanner = require('../private-cli/src/server/formatBanner');
const getDevToolsMiddleware = require('./getDevToolsMiddleware');
const loadRawBodyMiddleware = require('./loadRawBodyMiddleware');
const openStackFrameInEditorMiddleware = require('./openStackFrameInEditorMiddleware');
const parseCommandLine = require('./parseCommandLine.js');
const ReactPackager = require('./react-packager');
const statusPageMiddleware = require('./statusPageMiddleware.js');
const systraceProfileMiddleware = require('./systraceProfileMiddleware.js');
const webSocketProxy = require('./webSocketProxy.js');
var options = parseCommandLine([{
command: 'port',
default: 8081,
type: 'string',
}, {
command: 'root',
type: 'string',
description: 'add another root(s) to be used by the packager in this project',
}, {
command: 'projectRoots',
type: 'string',
description: 'override the root(s) to be used by the packager',
}, {
command: 'assetRoots',
type: 'string',
description: 'specify the root directories of app assets'
}, {
command: 'skipflow',
description: 'Disable flow checks'
}, {
command: 'nonPersistent',
description: 'Disable file watcher'
}, {
command: 'transformer',
type: 'string',
default: require.resolve('./transformer.js'),
description: 'Specify a custom transformer to be used (absolute path)'
}, {
command: 'resetCache',
description: 'Removes cached files',
default: false,
}, {
command: 'reset-cache',
description: 'Removes cached files',
default: false,
}, {
command: 'verbose',
description: 'Enables logging',
default: false,
}]);
if (options.projectRoots) {
if (!Array.isArray(options.projectRoots)) {
options.projectRoots = options.projectRoots.split(',');
}
} else {
// match on either path separator
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]packager$/)) {
// packager is running from node_modules of another project
options.projectRoots = [path.resolve(__dirname, '../../..')];
} else if (__dirname.match(/Pods\/React\/packager$/)) {
// packager is running from node_modules of another project
options.projectRoots = [path.resolve(__dirname, '../../..')];
} else {
options.projectRoots = [path.resolve(__dirname, '..')];
}
}
if (options.root) {
if (!Array.isArray(options.root)) {
options.root = options.root.split(',');
}
options.root.forEach(function(root) {
options.projectRoots.push(path.resolve(root));
});
}
if (options.assetRoots) {
if (!Array.isArray(options.assetRoots)) {
options.assetRoots = options.assetRoots.split(',').map(function (dir) {
return path.resolve(process.cwd(), dir);
});
}
} else {
// match on either path separator
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]packager$/)) {
options.assetRoots = [path.resolve(__dirname, '../../..')];
} else if (__dirname.match(/Pods\/React\/packager$/)) {
options.assetRoots = [path.resolve(__dirname, '../../..')];
} else {
options.assetRoots = [path.resolve(__dirname, '..')];
}
}
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,
})
);
console.log(
'Looking for JS files in\n ',
chalk.dim(options.projectRoots.join('\n ')),
'\n'
);
process.on('uncaughtException', function(e) {
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);
});
var server = runServer(options, function() {
console.log('\nReact packager ready.\n');
});
webSocketProxy.attachToServer(server, '/debugger-proxy');
webSocketProxy.attachToServer(server, '/devtools');
function getAppMiddleware(options) {
var transformerPath = options.transformer;
if (!isAbsolutePath(transformerPath)) {
transformerPath = path.resolve(process.cwd(), transformerPath);
}
return ReactPackager.middleware({
nonPersistent: options.nonPersistent,
projectRoots: options.projectRoots,
blacklistRE: blacklist(),
cacheVersion: '3',
transformModulePath: transformerPath,
assetRoots: options.assetRoots,
assetExts: ['png', 'jpeg', 'jpg'],
resetCache: options.resetCache || options['reset-cache'],
polyfillModuleNames: [
require.resolve(
'../Libraries/JavaScriptAppEngine/polyfills/document.js'
),
],
verbose: options.verbose,
});
}
function runServer(
options,
readyCallback
) {
var app = connect()
.use(loadRawBodyMiddleware)
.use(getDevToolsMiddleware(options))
.use(openStackFrameInEditorMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
.use(cpuProfilerMiddleware)
// Temporarily disable flow check until it's more stable
//.use(getFlowTypeCheckMiddleware(options))
.use(getAppMiddleware(options));
options.projectRoots.forEach(function(root) {
app.use(connect.static(root));
});
app.use(connect.logger())
.use(connect.compress())
.use(connect.errorHandler());
return http.createServer(app).listen(options.port, '::', readyCallback);
}

Просмотреть файл

@ -1,19 +0,0 @@
#!/usr/bin/env bash
# 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.
if [ $REACT_PACKAGER_LOG ];
then
echo "Logs will be redirected to $REACT_PACKAGER_LOG"
exec &> $REACT_PACKAGER_LOG
fi
ulimit -n 4096
THIS_DIR=$(dirname "$0")
node "$THIS_DIR/packager.js" "$@"