redux-devtools/packages/redux-devtools-cli/bin/redux-devtools.js

95 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-01-04 01:30:48 +03:00
#! /usr/bin/env node
var fs = require('fs');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var chalk = require('chalk');
var injectServer = require('./injectServer');
var getOptions = require('./../src/options');
var server = require('../index');
var open = require('./open');
var options = getOptions(argv);
2019-01-04 01:30:48 +03:00
function readFile(filePath) {
return fs.readFileSync(path.resolve(process.cwd(), filePath), 'utf-8');
}
if (argv.protocol === 'https') {
argv.key = argv.key ? readFile(argv.key) : null;
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;
2019-01-10 20:23:33 +03:00
console.log(prefix, color(msg)); // eslint-disable-line no-console
2019-01-04 01:30:48 +03:00
}
function getModuleName(type) {
switch (type) {
case 'macos':
return 'react-native-macos';
// react-native-macos is renamed from react-native-desktop
case 'desktop':
return 'react-native-desktop';
case 'reactnative':
default:
return 'react-native';
}
}
function getModulePath(moduleName) {
return path.join(process.cwd(), 'node_modules', moduleName);
}
function getModule(type) {
var moduleName = getModuleName(type);
var modulePath = getModulePath(moduleName);
if (type === 'desktop' && !fs.existsSync(modulePath)) {
moduleName = getModuleName('macos');
modulePath = getModulePath(moduleName);
}
return {
name: moduleName,
path: modulePath,
2019-01-04 01:30:48 +03:00
};
}
2019-01-10 20:23:33 +03:00
function injectRN(type, msg) {
var module = getModule(type);
var fn = type === 'revert' ? injectServer.revert : injectServer.inject;
var pass = fn(module.path, options, module.name);
2019-01-10 21:51:14 +03:00
log(
pass,
msg +
(pass
? '.'
: ', the file `' +
path.join(module.name, injectServer.fullPath) +
'` not found.')
);
2019-01-04 01:30:48 +03:00
process.exit(pass ? 0 : 1);
}
2019-01-10 20:23:33 +03:00
if (argv.revert) {
2019-01-10 21:51:14 +03:00
injectRN(
argv.revert,
'Revert injection of ReduxDevTools server from React Native local server'
);
2019-01-10 20:23:33 +03:00
}
2019-01-04 01:30:48 +03:00
if (argv.injectserver) {
2019-01-10 21:51:14 +03:00
injectRN(
argv.injectserver,
'Inject ReduxDevTools server into React Native local server'
);
2019-01-04 01:30:48 +03:00
}
server(argv).then(function (r) {
if (argv.open && argv.open !== 'false') {
r.on('ready', function () {
open(argv.open, options);
2019-01-10 21:51:14 +03:00
});
}
});