Serialize Store

Summary:
This diff adds the capability to export the flipper data to a file. With this diff you can click on the "Export Flipper" option from the "Edit" menu in menubar. It will export it in the file at this location

`~/.flipper/MessageLogs.json`

We do not exactly export the store, but just the important part of it. We export in the following format

```
{
fileVersion: '1.0',
device: {
   os: 'iOS',
   title: 'iPhone 7',
   serial: '',
   deviceType: 'physical',
  },
clients: [
   {
    query: {
       app: 'Facebook',
      },
      d: '12345678'
     },
   {
    query: {
       app: 'Instagram',
      },
    id: '12345678'
   }
],
store: {
   pluginState: {},
   notifications: {}
  }
}

```

In next diff I will add the capability to select the folder to export the file too.

Reviewed By: danielbuechele

Differential Revision: D13751963

fbshipit-source-id: 7d3d49c6adf8145b2181d2332c7dbd589155cec3
This commit is contained in:
Pritesh Nandgaonkar
2019-01-28 15:33:56 -08:00
committed by Facebook Github Bot
parent fc1d32a3f9
commit 5ef970e5b5
7 changed files with 118 additions and 11 deletions

76
src/utils/exportData.js Normal file
View File

@@ -0,0 +1,76 @@
/**
* 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 fs from 'fs';
import os from 'os';
import path from 'path';
const exportFilePath = path.join(
os.homedir(),
'.flipper',
'FlipperExport.json',
);
export const exportStoreToFile = (data: Store): Promise<void> => {
const state = data.getState();
const json = {
fileVersion: '1.0.0',
device: {},
clients: [],
store: {
pluginStates: {},
activeNotifications: [],
},
};
const device = state.connections.selectedDevice;
if (device) {
const {serial} = device;
json.device = device.toJSON();
const clients = state.connections.clients
.filter(client => {
return client.query.device_id === serial;
})
.map(client => {
return client.toJSON();
});
json.clients = clients;
const allPluginStates = state.pluginStates;
let pluginStates = {};
for (let key in allPluginStates) {
const filteredClients = clients.filter(client => {
let keyArray = key.split('#');
keyArray.pop(); // Remove the last entry related to plugin
return client.id.includes(keyArray.join('#'));
});
if (filteredClients.length > 0) {
pluginStates = {...pluginStates, [key]: allPluginStates[key]};
json.store.pluginStates = pluginStates;
}
}
const allActiveNotifications = state.notifications.activeNotifications;
let activeNotifications = allActiveNotifications.filter(notif => {
const filteredClients = clients.filter(client =>
client.id.includes(notif.client),
);
return filteredClients.length > 0;
});
json.store.activeNotifications = activeNotifications;
return new Promise((resolve, reject) => {
fs.writeFile(exportFilePath, JSON.stringify(json), err => {
if (err) {
reject(err);
}
resolve();
});
});
}
console.error('Make sure a device is connected');
return new Promise.reject(new Error('No device is selected'));
};