diff --git a/src/Client.js b/src/Client.js index fe22beb59..f9214d0ce 100644 --- a/src/Client.js +++ b/src/Client.js @@ -10,7 +10,7 @@ import type BaseDevice from './devices/BaseDevice.js'; import type {App} from './App.js'; import type Logger from './fb-stubs/Logger.js'; import type {Store} from './reducers/index.js'; - +import type {OS} from './devices/BaseDevice.js'; import {FlipperDevicePlugin} from './plugin.js'; import {setPluginState} from './reducers/pluginStates.js'; import {ReactiveSocket, PartialResponder} from 'rsocket-core'; @@ -22,7 +22,7 @@ type Plugins = Array; export type ClientQuery = {| app: string, - os: string, + os: OS, device: string, device_id: string, |}; diff --git a/src/devices/BaseDevice.js b/src/devices/BaseDevice.js index bfff8dd65..fba3b4a08 100644 --- a/src/devices/BaseDevice.js +++ b/src/devices/BaseDevice.js @@ -36,6 +36,8 @@ export type DeviceLogListener = (entry: DeviceLogEntry) => void; export type DeviceType = 'emulator' | 'physical'; +export type OS = 'iOS' | 'Android' | 'Windows'; + export default class BaseDevice { constructor(serial: string, deviceType: DeviceType, title: string) { this.serial = serial; @@ -44,7 +46,7 @@ export default class BaseDevice { } // operating system of this device - os: string; + os: OS; // human readable name for this device title: string; @@ -61,7 +63,7 @@ export default class BaseDevice { logListeners: Map = new Map(); logEntries: Array = []; - supportsOS(os: string) { + supportsOS(os: OS) { return os.toLowerCase() === this.os.toLowerCase(); } diff --git a/src/index.js b/src/index.js index f75dc5d35..2358ac295 100644 --- a/src/index.js +++ b/src/index.js @@ -33,3 +33,4 @@ export {default as DetailSidebar} from './chrome/DetailSidebar.js'; export {default as AndroidDevice} from './devices/AndroidDevice.js'; export {default as Device} from './devices/BaseDevice.js'; export {default as IOSDevice} from './devices/IOSDevice.js'; +export type {OS} from './devices/BaseDevice.js'; diff --git a/src/plugins/crash_reporter/index.js b/src/plugins/crash_reporter/index.js index 3b0a24a70..98ecdc1e0 100644 --- a/src/plugins/crash_reporter/index.js +++ b/src/plugins/crash_reporter/index.js @@ -26,7 +26,7 @@ import os from 'os'; import util from 'util'; import path from 'path'; import type {Notification} from '../../plugin'; -import type {Store, DeviceLogEntry} from 'flipper'; +import type {Store, DeviceLogEntry, OS} from 'flipper'; export type Crash = {| notificationID: string, @@ -82,7 +82,7 @@ export function getNewPersisitedStateFromCrashLog( persistedState: ?PersistedState, persistingPlugin: Class | FlipperPlugin<>>, content: string, - os: ?string, + os: ?OS, ): ?PersistedState { const persistedStateReducer = persistingPlugin.persistedStateReducer; if (!os || !persistedStateReducer) { @@ -145,9 +145,9 @@ export function parseCrashLogAndUpdateState( export function shouldShowCrashNotification( baseDevice: ?BaseDevice, content: string, - os: ?string, + os: ?OS, ): boolean { - if (os && os.includes('Android')) { + if (os && os === 'Android') { return true; } const appPath = parsePath(content); @@ -159,49 +159,54 @@ export function shouldShowCrashNotification( 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'; - if (os.includes('iOS')) { - const regex = /Exception Type: *[\w]*/; - const arr = regex.exec(content); - const exceptionString = arr ? arr[0] : ''; - const exceptionRegex = /[\w]*$/; - const tmp = exceptionRegex.exec(exceptionString); - const exception = - tmp && tmp[0].length ? tmp[0] : 'Cannot figure out the cause'; - const crash = { - callstack: content, - name: exception, - reason: exception, - }; - return crash; - } else if (os.includes('Android')) { - 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); + switch (os) { + case 'iOS': { + const regex = /Exception Type: *[\w]*/; + const arr = regex.exec(content); + const exceptionString = arr ? arr[0] : ''; + const exceptionRegex = /[\w]*$/; + const tmp = exceptionRegex.exec(exceptionString); + const exception = + tmp && tmp[0].length ? tmp[0] : 'Cannot figure out the cause'; + const crash = { + callstack: content, + name: exception, + reason: exception, + }; + return crash; } - const reason = - remainingString.length > 0 - ? remainingString.split('\n').pop() - : stubString; - if (name[name.length - 1] === '\n') { - name = name.slice(0, -1); + case 'Android': { + 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 = + 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 {