Refactor the type of os from String to an closest enum kind of thing

Summary: This diff updates the type of os in Store from string to a custom enum kind of type.

Reviewed By: passy

Differential Revision: D13622598

fbshipit-source-id: c57a1f2eedbe9e88d43c681c2fa6ca72b93e8808
This commit is contained in:
Pritesh Nandgaonkar
2019-01-10 09:52:52 -08:00
committed by Facebook Github Bot
parent d868cd6405
commit 3b9253680f
4 changed files with 55 additions and 47 deletions

View File

@@ -10,7 +10,7 @@ import type BaseDevice from './devices/BaseDevice.js';
import type {App} from './App.js'; import type {App} from './App.js';
import type Logger from './fb-stubs/Logger.js'; import type Logger from './fb-stubs/Logger.js';
import type {Store} from './reducers/index.js'; import type {Store} from './reducers/index.js';
import type {OS} from './devices/BaseDevice.js';
import {FlipperDevicePlugin} from './plugin.js'; import {FlipperDevicePlugin} from './plugin.js';
import {setPluginState} from './reducers/pluginStates.js'; import {setPluginState} from './reducers/pluginStates.js';
import {ReactiveSocket, PartialResponder} from 'rsocket-core'; import {ReactiveSocket, PartialResponder} from 'rsocket-core';
@@ -22,7 +22,7 @@ type Plugins = Array<string>;
export type ClientQuery = {| export type ClientQuery = {|
app: string, app: string,
os: string, os: OS,
device: string, device: string,
device_id: string, device_id: string,
|}; |};

View File

@@ -36,6 +36,8 @@ export type DeviceLogListener = (entry: DeviceLogEntry) => void;
export type DeviceType = 'emulator' | 'physical'; export type DeviceType = 'emulator' | 'physical';
export type OS = 'iOS' | 'Android' | 'Windows';
export default class BaseDevice { export default class BaseDevice {
constructor(serial: string, deviceType: DeviceType, title: string) { constructor(serial: string, deviceType: DeviceType, title: string) {
this.serial = serial; this.serial = serial;
@@ -44,7 +46,7 @@ export default class BaseDevice {
} }
// operating system of this device // operating system of this device
os: string; os: OS;
// human readable name for this device // human readable name for this device
title: string; title: string;
@@ -61,7 +63,7 @@ export default class BaseDevice {
logListeners: Map<Symbol, DeviceLogListener> = new Map(); logListeners: Map<Symbol, DeviceLogListener> = new Map();
logEntries: Array<DeviceLogEntry> = []; logEntries: Array<DeviceLogEntry> = [];
supportsOS(os: string) { supportsOS(os: OS) {
return os.toLowerCase() === this.os.toLowerCase(); return os.toLowerCase() === this.os.toLowerCase();
} }

View File

@@ -33,3 +33,4 @@ export {default as DetailSidebar} from './chrome/DetailSidebar.js';
export {default as AndroidDevice} from './devices/AndroidDevice.js'; export {default as AndroidDevice} from './devices/AndroidDevice.js';
export {default as Device} from './devices/BaseDevice.js'; export {default as Device} from './devices/BaseDevice.js';
export {default as IOSDevice} from './devices/IOSDevice.js'; export {default as IOSDevice} from './devices/IOSDevice.js';
export type {OS} from './devices/BaseDevice.js';

View File

@@ -26,7 +26,7 @@ import os from 'os';
import util from 'util'; import util from 'util';
import path from 'path'; import path from 'path';
import type {Notification} from '../../plugin'; import type {Notification} from '../../plugin';
import type {Store, DeviceLogEntry} from 'flipper'; import type {Store, DeviceLogEntry, OS} from 'flipper';
export type Crash = {| export type Crash = {|
notificationID: string, notificationID: string,
@@ -82,7 +82,7 @@ export function getNewPersisitedStateFromCrashLog(
persistedState: ?PersistedState, persistedState: ?PersistedState,
persistingPlugin: Class<FlipperDevicePlugin<> | FlipperPlugin<>>, persistingPlugin: Class<FlipperDevicePlugin<> | FlipperPlugin<>>,
content: string, content: string,
os: ?string, os: ?OS,
): ?PersistedState { ): ?PersistedState {
const persistedStateReducer = persistingPlugin.persistedStateReducer; const persistedStateReducer = persistingPlugin.persistedStateReducer;
if (!os || !persistedStateReducer) { if (!os || !persistedStateReducer) {
@@ -145,9 +145,9 @@ export function parseCrashLogAndUpdateState(
export function shouldShowCrashNotification( export function shouldShowCrashNotification(
baseDevice: ?BaseDevice, baseDevice: ?BaseDevice,
content: string, content: string,
os: ?string, os: ?OS,
): boolean { ): boolean {
if (os && os.includes('Android')) { if (os && os === 'Android') {
return true; return true;
} }
const appPath = parsePath(content); const appPath = parsePath(content);
@@ -159,49 +159,54 @@ export function shouldShowCrashNotification(
return true; return true;
} }
export function parseCrashLog(content: string, os: string): CrashLog { export function parseCrashLog(content: string, os: OS): CrashLog {
const stubString = 'Cannot figure out the cause'; const stubString = 'Cannot figure out the cause';
if (os.includes('iOS')) { switch (os) {
const regex = /Exception Type: *[\w]*/; case 'iOS': {
const arr = regex.exec(content); const regex = /Exception Type: *[\w]*/;
const exceptionString = arr ? arr[0] : ''; const arr = regex.exec(content);
const exceptionRegex = /[\w]*$/; const exceptionString = arr ? arr[0] : '';
const tmp = exceptionRegex.exec(exceptionString); const exceptionRegex = /[\w]*$/;
const exception = const tmp = exceptionRegex.exec(exceptionString);
tmp && tmp[0].length ? tmp[0] : 'Cannot figure out the cause'; const exception =
const crash = { tmp && tmp[0].length ? tmp[0] : 'Cannot figure out the cause';
callstack: content, const crash = {
name: exception, callstack: content,
reason: exception, name: exception,
}; reason: exception,
return crash; };
} else if (os.includes('Android')) { return crash;
const regForName = /.*\n/;
const nameRegArr = regForName.exec(content);
let name = nameRegArr ? nameRegArr[0] : stubString;
const regForCallStack = /\tat[\w\s\n.$&+,:;=?@#|'<>.^*()%!-]*$/;
const callStackArray = regForCallStack.exec(content);
const callStack = callStackArray ? callStackArray[0] : '';
let remainingString =
callStack.length > 0 ? content.replace(callStack, '') : '';
if (remainingString[remainingString.length - 1] === '\n') {
remainingString = remainingString.slice(0, -1);
} }
const reason = case 'Android': {
remainingString.length > 0 const regForName = /.*\n/;
? remainingString.split('\n').pop() const nameRegArr = regForName.exec(content);
: stubString; let name = nameRegArr ? nameRegArr[0] : stubString;
if (name[name.length - 1] === '\n') { const regForCallStack = /\tat[\w\s\n.$&+,:;=?@#|'<>.^*()%!-]*$/;
name = name.slice(0, -1); const callStackArray = regForCallStack.exec(content);
const callStack = callStackArray ? callStackArray[0] : '';
let remainingString =
callStack.length > 0 ? content.replace(callStack, '') : '';
if (remainingString[remainingString.length - 1] === '\n') {
remainingString = remainingString.slice(0, -1);
}
const reason =
remainingString.length > 0
? remainingString.split('\n').pop()
: stubString;
if (name[name.length - 1] === '\n') {
name = name.slice(0, -1);
}
const crash = {
callstack: content,
name: name,
reason: reason,
};
return crash;
}
default: {
throw new Error('Unsupported OS');
} }
const crash = {
callstack: content,
name: name,
reason: reason,
};
return crash;
} }
throw new Error('Unsupported OS');
} }
export function parsePath(content: string): ?string { export function parsePath(content: string): ?string {