diff --git a/headless/index.js b/headless/index.js index 78231544b..b4a717e47 100644 --- a/headless/index.js +++ b/headless/index.js @@ -13,6 +13,7 @@ import path from 'path'; // $FlowFixMe this file exist, trust me, flow! import setup from '../static/setup.js'; import yargs from 'yargs'; +import {serializeStore} from '../src/utils/exportData.js'; yargs .usage('$0 [args]') @@ -96,4 +97,11 @@ function startFlipper({ const logger = new Logger(store); init(store); dispatcher(store, logger); + + process.on('SIGINT', () => { + originalConsole.log( + JSON.stringify(serializeStore(store.getState()), null, 2), + ); + process.exit(); + }); } diff --git a/src/reducers/index.js b/src/reducers/index.js index 119b11a21..48ea6244f 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -45,16 +45,16 @@ type Actions = | PluginsAction | {|type: 'INIT'|}; -export type Store = ReduxStore< - {| - application: ApplicationState, - connections: DevicesState, - pluginStates: PluginStatesState, - notifications: NotificationsState, - plugins: PluginsState, - |}, - Actions, ->; +export type State = {| + application: ApplicationState, + connections: DevicesState, + pluginStates: PluginStatesState, + notifications: NotificationsState, + plugins: PluginsState, +|}; + +// $FlowFixMe introduced when removing $Subtype/$Supertype +export type Store = ReduxStore; export default combineReducers<_, Actions>({ application, diff --git a/src/utils/exportData.js b/src/utils/exportData.js index 5ab6129b1..dcb4cd071 100644 --- a/src/utils/exportData.js +++ b/src/utils/exportData.js @@ -10,6 +10,7 @@ import type {State as PluginStates} from '../reducers/pluginStates'; import type {PluginNotification} from '../reducers/notifications.js'; import type {ClientExport} from '../Client.js'; import type {State as PluginStatesState} from '../reducers/pluginStates'; +import type {State} from '../reducers/index'; import {FlipperDevicePlugin} from '../plugin.js'; import {default as BaseDevice} from '../devices/BaseDevice'; @@ -118,21 +119,24 @@ export const processStore = ( return null; }; -export const exportStoreToFile = (data: Store): Promise => { - const state = data.getState(); +export function serializeStore(state: State): ?ExportType { const {activeNotifications} = state.notifications; const {selectedDevice, clients} = state.connections; const {pluginStates} = state; const {devicePlugins} = state.plugins; // TODO: T39612653 Make Client mockable. Currently rsocket logic is tightly coupled. // Not passing the entire state as currently Client is not mockable. - const json = processStore( + return processStore( activeNotifications, selectedDevice, pluginStates, clients.map(client => client.toJSON()), devicePlugins, ); +} + +export const exportStoreToFile = (store: Store): Promise => { + const json = serializeStore(store.getState()); if (json) { return new Promise((resolve, reject) => { fs.writeFile(exportFilePath, JSON.stringify(json), err => {