Refactor the startFlipper function

Summary:
Refactors `startFlipper` function in headless flipper into three different functions.

`earlyExitActions`: Before the dispatcher is called on the store and will exit if the user arguments are one which are meant to terminate the process.
`exitActions`: This function will be called after the dispatcher is called and will exit if the user arguments are one which are meant to terminate the process.
`storeModifyngActions`: It involves business logic which updates the store based on the user arguments.

Reviewed By: passy

Differential Revision: D15556130

fbshipit-source-id: 9d1b035525e613bb2b75454fa4fd6c193993b530
This commit is contained in:
Pritesh Nandgaonkar
2019-05-30 10:12:51 -07:00
committed by Facebook Github Bot
parent bab2aaaf0e
commit b5bda8dfae

View File

@@ -20,6 +20,18 @@ import {
import {listDevices} from '../src/utils/listDevices'; import {listDevices} from '../src/utils/listDevices';
// $FlowFixMe this file exist, trust me, flow! // $FlowFixMe this file exist, trust me, flow!
import setup from '../static/setup.js'; import setup from '../static/setup.js';
import type {Store} from '../src/reducers';
type UserArguments = {|
securePort: string,
insecurePort: string,
dev: boolean,
exit: 'sigint' | 'disconnect',
verbose: boolean,
metrics: string,
showDevices: boolean,
selectedDeviceID: string,
|};
yargs yargs
.usage('$0 [args]') .usage('$0 [args]')
@@ -28,11 +40,13 @@ yargs
'Start a headless Flipper instance', 'Start a headless Flipper instance',
yargs => { yargs => {
yargs.option('secure-port', { yargs.option('secure-port', {
alias: 'securePort',
default: '8088', default: '8088',
describe: 'Secure port the Flipper server should run on.', describe: 'Secure port the Flipper server should run on.',
type: 'string', type: 'string',
}); });
yargs.option('insecure-port', { yargs.option('insecure-port', {
alias: 'insecurePort',
default: '8089', default: '8089',
describe: 'Insecure port the Flipper server should run on.', describe: 'Insecure port the Flipper server should run on.',
type: 'string', type: 'string',
@@ -86,16 +100,90 @@ function shouldExportMetric(metrics): boolean {
return true; return true;
} }
async function startFlipper({ async function earlyExitActions(
dev, userArguments: UserArguments,
verbose, originalConsole: typeof global.console,
metrics, ): Promise<void> {
showDevices, const {showDevices} = userArguments;
selectedDeviceID, if (showDevices) {
exit, const devices = await listDevices();
'insecure-port': insecurePort, originalConsole.log(devices);
'secure-port': securePort, process.exit();
}) { }
}
async function exitActions(
userArguments: UserArguments,
originalConsole: typeof global.console,
store: Store,
): Promise<void> {
const {metrics, exit} = userArguments;
if (shouldExportMetric(metrics) && metrics && metrics.length > 0) {
try {
const payload = await exportMetricsFromTrace(metrics, store.getState());
originalConsole.log(payload);
} catch (error) {
console.error(error);
}
process.exit();
}
if (exit == 'sigint') {
process.on('SIGINT', async () => {
try {
if (shouldExportMetric(metrics) && !metrics) {
const state = store.getState();
const payload = await exportMetricsWithoutTrace(
state,
state.pluginStates,
);
originalConsole.log(payload);
} else {
const {serializedString, errorArray} = await exportStore(store);
errorArray.forEach(console.error);
originalConsole.log(serializedString);
}
} catch (e) {
console.error(e);
}
process.exit();
});
}
}
async function storeModifyingActions(
userArguments: UserArguments,
originalConsole: typeof global.console,
store: Store,
): Promise<void> {
const {selectedDeviceID} = userArguments;
if (selectedDeviceID) {
//$FlowFixMe: Checked the class name before calling reverse.
const devices = await listDevices();
const matchedDevice = devices.find(
device => device.serial === selectedDeviceID,
);
if (matchedDevice) {
if (matchedDevice.constructor.name === 'AndroidDevice') {
const ports = store.getState().application.serverPorts;
matchedDevice.reverse([ports.secure, ports.insecure]);
}
store.dispatch({
type: 'REGISTER_DEVICE',
payload: matchedDevice,
});
store.dispatch({
type: 'SELECT_DEVICE',
payload: matchedDevice,
});
} else {
console.error(`No device matching the serial ${selectedDeviceID}`);
process.exit();
}
}
}
async function startFlipper(userArguments: UserArguments) {
const {verbose, metrics, exit, insecurePort, securePort} = userArguments;
console.error(` console.error(`
_____ _ _ _____ _ _
| __| |_|___ ___ ___ ___ | __| |_|___ ___ ___ ___
@@ -161,68 +249,11 @@ async function startFlipper({
); );
const logger = initLogger(store, {isHeadless: true}); const logger = initLogger(store, {isHeadless: true});
//TODO: T45068486 Refactor this function into separate components. await earlyExitActions(userArguments, originalConsole);
if (showDevices) {
const devices = await listDevices();
originalConsole.log(devices);
process.exit();
}
dispatcher(store, logger); dispatcher(store, logger);
if (shouldExportMetric(metrics) && metrics && metrics.length > 0) {
try {
const payload = await exportMetricsFromTrace(metrics, store.getState());
originalConsole.log(payload);
} catch (error) {
console.error(error);
}
process.exit();
}
if (selectedDeviceID) { await storeModifyingActions(userArguments, originalConsole, store);
//$FlowFixMe: Checked the class name before calling reverse.
const devices = await listDevices();
const matchedDevice = devices.find(
device => device.serial === selectedDeviceID,
);
if (matchedDevice) {
if (matchedDevice.constructor.name === 'AndroidDevice') {
const ports = store.getState().application.serverPorts;
matchedDevice.reverse([ports.secure, ports.insecure]);
}
store.dispatch({
type: 'REGISTER_DEVICE',
payload: matchedDevice,
});
store.dispatch({
type: 'SELECT_DEVICE',
payload: matchedDevice,
});
} else {
console.error(`No device matching the serial ${selectedDeviceID}`);
process.exit();
}
}
if (exit == 'sigint') { await exitActions(userArguments, originalConsole, store);
process.on('SIGINT', async () => {
try {
if (shouldExportMetric(metrics) && !metrics) {
const state = store.getState();
const payload = await exportMetricsWithoutTrace(
state,
state.pluginStates,
);
originalConsole.log(payload);
} else {
const {serializedString, errorArray} = await exportStore(store);
errorArray.forEach(console.error);
originalConsole.log(serializedString);
}
} catch (e) {
console.error(e);
}
process.exit();
});
}
} }