mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Move private-cli commands to local-cli
Summary: public We cannot remove `local-cli` because is referenced by the global cli explicitly. If we do so, people would have to upgrate this global thin cli which will cause some pain. So, lets move `private-cli` commands into `local-cli` instead. Reviewed By: frantic Differential Revision: D2571983 fb-gh-sync-id: 712c29430203660fb6f0d5f23813cb2a7156ee48
This commit is contained in:
committed by
facebook-github-bot-3
parent
24537e3726
commit
849aa4dae6
40
local-cli/server/checkNodeVersion.js
Normal file
40
local-cli/server/checkNodeVersion.js
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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 chalk = require('chalk');
|
||||
const formatBanner = require('./formatBanner');
|
||||
const semver = require('semver');
|
||||
|
||||
module.exports = function() {
|
||||
if (!semver.satisfies(process.version, '>=4')) {
|
||||
const engine = semver.satisfies(process.version, '<1 >=4')
|
||||
? 'Node'
|
||||
: 'io.js';
|
||||
|
||||
const message = 'You are currently running ' + engine + ' ' +
|
||||
process.version + '.\n' +
|
||||
'\n' +
|
||||
'React Native runs on Node 4.0 or newer. There are several ways to ' +
|
||||
'upgrade Node.js depending on your preference.\n' +
|
||||
'\n' +
|
||||
'nvm: nvm install node && nvm alias default node\n' +
|
||||
'Homebrew: brew unlink iojs; brew install node\n' +
|
||||
'Installer: download the Mac .pkg from https://nodejs.org/\n' +
|
||||
'\n' +
|
||||
'About Node.js: https://nodejs.org\n' +
|
||||
'Follow along at: https://github.com/facebook/react-native/issues/2545';
|
||||
console.log(formatBanner(message, {
|
||||
chalkFunction: chalk.green,
|
||||
marginLeft: 1,
|
||||
marginRight: 1,
|
||||
paddingBottom: 1,
|
||||
}));
|
||||
}
|
||||
};
|
||||
108
local-cli/server/formatBanner.js
Normal file
108
local-cli/server/formatBanner.js
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* 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 _ = require('underscore');
|
||||
var wordwrap = require('wordwrap');
|
||||
|
||||
var HORIZONTAL_LINE = '\u2500';
|
||||
var VERTICAL_LINE = '\u2502';
|
||||
var TOP_LEFT = '\u250c';
|
||||
var TOP_RIGHT = '\u2510';
|
||||
var BOTTOM_LEFT = '\u2514';
|
||||
var BOTTOM_RIGHT = '\u2518';
|
||||
|
||||
/**
|
||||
* Prints a banner with a border around it containing the given message. The
|
||||
* following options are supported:
|
||||
*
|
||||
* type Options = {
|
||||
* // A function to apply to each line of text to decorate it
|
||||
* chalkFunction: (string: message) => string;
|
||||
* // The total width (max line length) of the banner, including margin and
|
||||
* // padding (default = 80)
|
||||
* width: number;
|
||||
* // How much leading space to prepend to each line (default = 0)
|
||||
* marginLeft: number;
|
||||
* // How much trailing space to append to each line (default = 0)
|
||||
* marginRight: number;
|
||||
* // Space between the top banner border and the text (default = 0)
|
||||
* paddingTop: number;
|
||||
* // Space between the bottom banner border and the text (default = 0)
|
||||
* paddingBottom: number;
|
||||
* // Space between the left banner border and the text (default = 2)
|
||||
* paddingLeft: number;
|
||||
* // Space between the right banner border and the text (default = 2)
|
||||
* paddingRight: number;
|
||||
* };
|
||||
*/
|
||||
function formatBanner(message, options) {
|
||||
options = options || {};
|
||||
_.defaults(options, {
|
||||
chalkFunction: _.identity,
|
||||
width: 80,
|
||||
marginLeft: 0,
|
||||
marginRight: 0,
|
||||
paddingTop: 0,
|
||||
paddingBottom: 0,
|
||||
paddingLeft: 2,
|
||||
paddingRight: 2,
|
||||
});
|
||||
|
||||
var width = options.width;
|
||||
var marginLeft = options.marginLeft;
|
||||
var marginRight = options.marginRight;
|
||||
var paddingTop = options.paddingTop;
|
||||
var paddingBottom = options.paddingBottom;
|
||||
var paddingLeft = options.paddingLeft;
|
||||
var paddingRight = options.paddingRight;
|
||||
|
||||
var horizSpacing = marginLeft + paddingLeft + paddingRight + marginRight;
|
||||
// 2 for the banner borders
|
||||
var maxLineWidth = width - horizSpacing - 2;
|
||||
var wrap = wordwrap(maxLineWidth);
|
||||
var body = wrap(message);
|
||||
|
||||
var left = spaces(marginLeft) + VERTICAL_LINE + spaces(paddingLeft);
|
||||
var right = spaces(paddingRight) + VERTICAL_LINE + spaces(marginRight);
|
||||
var bodyLines = _.flatten([
|
||||
arrayOf('', paddingTop),
|
||||
body.split('\n'),
|
||||
arrayOf('', paddingBottom),
|
||||
]).map(function(line) {
|
||||
var padding = spaces(Math.max(0, maxLineWidth - line.length));
|
||||
return left + options.chalkFunction(line) + padding + right;
|
||||
});
|
||||
|
||||
var horizontalBorderLine = repeatString(
|
||||
HORIZONTAL_LINE,
|
||||
width - marginLeft - marginRight - 2
|
||||
);
|
||||
var top = spaces(marginLeft) + TOP_LEFT + horizontalBorderLine + TOP_RIGHT +
|
||||
spaces(marginRight);
|
||||
var bottom = spaces(marginLeft) + BOTTOM_LEFT + horizontalBorderLine +
|
||||
BOTTOM_RIGHT + spaces(marginRight);
|
||||
return _.flatten([top, bodyLines, bottom]).join('\n');
|
||||
}
|
||||
|
||||
function spaces(number) {
|
||||
return repeatString(' ', number);
|
||||
}
|
||||
|
||||
function repeatString(string, number) {
|
||||
return new Array(number + 1).join(string);
|
||||
}
|
||||
|
||||
function arrayOf(value, number) {
|
||||
return _.range(number).map(function() {
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = formatBanner;
|
||||
68
local-cli/server/runServer.js
Normal file
68
local-cli/server/runServer.js
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 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 connect = require('connect');
|
||||
const cpuProfilerMiddleware = require('../../packager/cpuProfilerMiddleware');
|
||||
const getDevToolsMiddleware = require('../../packager/getDevToolsMiddleware');
|
||||
const http = require('http');
|
||||
const isAbsolutePath = require('absolute-path');
|
||||
const loadRawBodyMiddleware = require('../../packager/loadRawBodyMiddleware');
|
||||
const openStackFrameInEditorMiddleware = require('../../packager/openStackFrameInEditorMiddleware');
|
||||
const path = require('path');
|
||||
const ReactPackager = require('../../packager/react-packager');
|
||||
const statusPageMiddleware = require('../../packager/statusPageMiddleware.js');
|
||||
const systraceProfileMiddleware = require('../../packager/systraceProfileMiddleware.js');
|
||||
|
||||
function runServer(args, config, readyCallback) {
|
||||
const app = connect()
|
||||
.use(loadRawBodyMiddleware)
|
||||
.use(getDevToolsMiddleware(args))
|
||||
.use(openStackFrameInEditorMiddleware)
|
||||
.use(statusPageMiddleware)
|
||||
.use(systraceProfileMiddleware)
|
||||
.use(cpuProfilerMiddleware)
|
||||
// Temporarily disable flow check until it's more stable
|
||||
//.use(getFlowTypeCheckMiddleware(args))
|
||||
.use(getAppMiddleware(args, config));
|
||||
|
||||
args.projectRoots.forEach(root => app.use(connect.static(root)));
|
||||
|
||||
app.use(connect.logger())
|
||||
.use(connect.compress())
|
||||
.use(connect.errorHandler());
|
||||
|
||||
return http.createServer(app).listen(args.port, '::', readyCallback);
|
||||
}
|
||||
|
||||
function getAppMiddleware(args, config) {
|
||||
let transformerPath = args.transformer;
|
||||
if (!isAbsolutePath(transformerPath)) {
|
||||
transformerPath = path.resolve(process.cwd(), transformerPath);
|
||||
}
|
||||
|
||||
return ReactPackager.middleware({
|
||||
nonPersistent: args.nonPersistent,
|
||||
projectRoots: args.projectRoots,
|
||||
blacklistRE: config.getBlacklistRE(),
|
||||
cacheVersion: '3',
|
||||
transformModulePath: transformerPath,
|
||||
assetRoots: args.assetRoots,
|
||||
assetExts: ['png', 'jpeg', 'jpg'],
|
||||
resetCache: args.resetCache || args['reset-cache'],
|
||||
polyfillModuleNames: [
|
||||
require.resolve(
|
||||
'../../Libraries/JavaScriptAppEngine/polyfills/document.js'
|
||||
),
|
||||
],
|
||||
verbose: args.verbose,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = runServer;
|
||||
153
local-cli/server/server.js
Normal file
153
local-cli/server/server.js
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* 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 chalk = require('chalk');
|
||||
const checkNodeVersion = require('./checkNodeVersion');
|
||||
const formatBanner = require('./formatBanner');
|
||||
const parseCommandLine = require('../../packager/parseCommandLine');
|
||||
const path = require('path');
|
||||
const Promise = require('promise');
|
||||
const runServer = require('./runServer');
|
||||
const webSocketProxy = require('../../packager/webSocketProxy.js');
|
||||
|
||||
/**
|
||||
* Starts the React Native Packager Server.
|
||||
*/
|
||||
function server(argv, config) {
|
||||
return new Promise((resolve, reject) => {
|
||||
_server(argv, config, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
function _server(argv, config, resolve, reject) {
|
||||
const args = 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: '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('../../packager/transformer'),
|
||||
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,
|
||||
}]);
|
||||
|
||||
args.projectRoots = args.projectRoots
|
||||
? argToArray(args.projectRoots)
|
||||
: config.getProjectRoots();
|
||||
|
||||
if (args.root) {
|
||||
const additionalRoots = argToArray(args.root);
|
||||
additionalRoots.forEach(root => {
|
||||
args.projectRoots.push(path.resolve(root));
|
||||
});
|
||||
}
|
||||
|
||||
args.assetRoots = args.assetRoots
|
||||
? argToArray(args.projectRoots).map(dir =>
|
||||
path.resolve(process.cwd(), dir)
|
||||
)
|
||||
: config.getAssetRoots();
|
||||
|
||||
checkNodeVersion();
|
||||
|
||||
console.log(formatBanner(
|
||||
'Running packager on port ' + args.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(args.projectRoots.join('\n ')),
|
||||
'\n'
|
||||
);
|
||||
|
||||
process.on('uncaughtException', error => {
|
||||
if (error.code === 'EADDRINUSE') {
|
||||
console.log(
|
||||
chalk.bgRed.bold(' ERROR '),
|
||||
chalk.red('Packager can\'t listen on port', chalk.bold(args.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:' + args.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(error.message));
|
||||
const errorAttributes = JSON.stringify(error);
|
||||
if (errorAttributes !== '{}') {
|
||||
console.error(chalk.red(errorAttributes));
|
||||
}
|
||||
console.error(chalk.red(error.stack));
|
||||
}
|
||||
console.log('\nSee', chalk.underline('http://facebook.github.io/react-native/docs/troubleshooting.html'));
|
||||
console.log('for common problems and solutions.');
|
||||
reject();
|
||||
});
|
||||
|
||||
// TODO: remove once we deprecate this arg
|
||||
if (args.resetCache) {
|
||||
console.log(
|
||||
'Please start using `--reset-cache` instead. ' +
|
||||
'We\'ll deprecate this argument soon.'
|
||||
);
|
||||
}
|
||||
|
||||
startServer(args, config);
|
||||
}
|
||||
|
||||
function startServer(args, config) {
|
||||
const serverInstance = runServer(args, config, () =>
|
||||
console.log('\nReact packager ready.\n')
|
||||
);
|
||||
|
||||
webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
|
||||
webSocketProxy.attachToServer(serverInstance, '/devtools');
|
||||
}
|
||||
|
||||
function argToArray(arg) {
|
||||
return Array.isArray(arg) ? arg : arg.split(',');
|
||||
}
|
||||
|
||||
module.exports = server;
|
||||
Reference in New Issue
Block a user