Files
flipper/headless/index.js
Daniel Büchele 8b91ec68a7 Catch error on export
Summary:
`exportStore` throws, when no device is connected. The error wasn't catched and `process.exit();` never called. The result is that the CLI couldn't be interrupted as long as `exportStore` thorws (e.g. no device connected).

Catching this error now.

Reviewed By: passy

Differential Revision: D14520754

fbshipit-source-id: 97b245c4de53e58bac8066a1b15874d873bf2841
2019-03-19 07:23:29 -07:00

109 lines
2.9 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {createStore} from 'redux';
import reducers from '../src/reducers/index.js';
import dispatcher from '../src/dispatcher/index.js';
import {init as initLogger} from '../src/fb-stubs/Logger.js';
import path from 'path';
// $FlowFixMe this file exist, trust me, flow!
import setup from '../static/setup.js';
import yargs from 'yargs';
import {exportStore} from '../src/utils/exportData.js';
yargs
.usage('$0 [args]')
.command(
'*',
'Start a headless Flipper instance',
yargs => {
yargs.option('secure-port', {
default: '8088',
describe: 'Secure port the Flipper server should run on.',
type: 'string',
});
yargs.option('insecure-port', {
default: '8089',
describe: 'Insecure port the Flipper server should run on.',
type: 'string',
});
yargs.option('dev', {
default: false,
describe:
'Enable redux-devtools. Tries to connect to devtools running on port 8181',
type: 'boolean',
});
yargs.option('v', {
alias: 'verbose',
default: false,
describe: 'Enable verbose logging',
type: 'boolean',
});
},
startFlipper,
)
.version(global.__VERSION__)
.help().argv; // http://yargs.js.org/docs/#api-argv
function startFlipper({
dev,
verbose,
'insecure-port': insecurePort,
'secure-port': securePort,
}) {
console.error(`
_____ _ _
| __| |_|___ ___ ___ ___
| __| | | . | . | -_| _|
|__| |_|_| _| _|___|_| v${global.__VERSION__}
|_| |_|
`);
// redirect all logging to stderr
const originalConsole = global.console;
global.console = new Proxy(console, {
get: function(obj, prop) {
return (...args) => {
if (prop === 'error' || verbose) {
originalConsole.error(`[${prop}] `, ...args);
}
};
},
});
// Polyfills
global.WebSocket = require('ws'); // used for redux devtools
global.fetch = require('node-fetch/lib/index');
process.env.BUNDLED_PLUGIN_PATH =
process.env.BUNDLED_PLUGIN_PATH ||
path.join(path.dirname(process.execPath), 'plugins');
process.env.FLIPPER_PORTS = `${insecurePort},${securePort}`;
// needs to be required after WebSocket polyfill is loaded
const devToolsEnhancer = require('remote-redux-devtools').default;
setup({});
const store = dev
? createStore(
reducers,
devToolsEnhancer({realtime: true, hostname: 'localhost', port: 8181}),
)
: createStore(reducers);
const logger = initLogger(store, {isHeadless: true});
dispatcher(store, logger);
process.on('SIGINT', async () => {
try {
originalConsole.log(await exportStore(store));
} catch (e) {
console.error(e);
}
process.exit();
});
}