Add injectserver / revert CLI options

This commit is contained in:
Jhen
2016-07-19 00:05:26 +08:00
parent 342878f787
commit 880e87b987
3 changed files with 99 additions and 0 deletions

View File

@ -2,6 +2,9 @@
var fs = require('fs');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var chalk = require('chalk');
var getOptions = require('./getOptions');
var injectServer = require('./injectServer');
function readFile(filePath) {
return fs.readFileSync(path.resolve(process.cwd(), filePath), 'utf-8');
@ -12,4 +15,42 @@ if (argv.protocol === 'https') {
argv.cert = argv.cert ? readFile(argv.cert) : null;
}
function log(pass, msg) {
var prefix = pass ? chalk.green.bgBlack('PASS') : chalk.red.bgBlack('FAIL');
var color = pass ? chalk.blue : chalk.red;
console.log(prefix, color(msg));
};
function getModuleName(type) {
switch (type) {
case 'desktop':
return 'react-native-desktop';
case 'reactnative':
default:
return 'react-native';
}
}
function getModulePath(type) {
var moduleName = getModuleName(type);
return path.join(process.cwd(), 'node_modules', moduleName);
}
if (argv.revert) {
var pass = injectServer.revert(getModulePath(argv.revert));
var msg = 'Revert injection of RemoteDev server from React Native local server';
log(pass, msg + (!pass ? ', the file `' + injectServer.fullPath + '` not found.' : '.'));
process.exit(pass ? 0 : 1);
}
if (argv.injectserver) {
var options = getOptions(argv);
var pass = injectServer.inject(getModulePath(argv.injectserver), options);
var msg = 'Inject RemoteDev server into React Native local server';
log(pass, msg + (pass ? '.' : ', the file `' + injectServer.fullPath + '` not found.'));
process.exit(pass ? 0 : 1);
}
require('./server')(argv);