microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/iOSDebugModeManager.ts
57lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as Q from "q"; |
| 5 | |
| 6 | import {PromiseUtil} from "../../common/node/promise"; |
| 7 | import {PlistBuddy} from "./plistBuddy"; |
| 8 | import {SimulatorPlist} from "./simulatorPlist"; |
| 9 | |
| 10 | export class IOSDebugModeManager { |
| 11 | private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass"; |
| 12 | private static MAX_RETRIES = 5; |
| 13 | private static DELAY_UNTIL_RETRY = 2000; |
| 14 | |
| 15 | private projectRoot: string; |
| 16 | private simulatorPlist: SimulatorPlist; |
| 17 | |
| 18 | constructor(projectRoot: string) { |
| 19 | this.projectRoot = projectRoot; |
| 20 | this.simulatorPlist = new SimulatorPlist(this.projectRoot); |
| 21 | } |
| 22 | |
| 23 | public setSimulatorJSDebuggingModeSetting(enable: boolean): Q.Promise<void> { |
| 24 | const plistBuddy = new PlistBuddy(); |
| 25 | |
| 26 | // Find the plistFile with the configuration setting |
| 27 | // There is a race here between us checking for the plist file, and the application starting up. |
| 28 | return this.findPListFile(enable) |
| 29 | .then((plistFile: string) => { |
| 30 | // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode |
| 31 | // This is approximately equivalent to clicking the "Debug in Chrome" button |
| 32 | return enable |
| 33 | ? plistBuddy.setPlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, "RCTWebSocketExecutor") |
| 34 | : plistBuddy.deletePlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | private tryOneAttemptToFindPListFile() { |
| 39 | return this.simulatorPlist.findPlistFile().catch((): string => null); |
| 40 | } |
| 41 | |
| 42 | private findPListFile(enable: boolean): Q.Promise<string> { |
| 43 | const pu = new PromiseUtil(); |
| 44 | const actionText = enable ? "enable" : "disable"; |
| 45 | |
| 46 | const failureString = `Unable to find plist file to ${actionText} debugging`; |
| 47 | |
| 48 | return pu.retryAsync( |
| 49 | () => |
| 50 | this.tryOneAttemptToFindPListFile(), // Operation to retry until succesful |
| 51 | (file: string) => |
| 52 | file !== null, // Condition to check if the operation was succesful, and this logic is done |
| 53 | IOSDebugModeManager.MAX_RETRIES, |
| 54 | IOSDebugModeManager.DELAY_UNTIL_RETRY, |
| 55 | failureString); // Error to show in case all retries fail |
| 56 | } |
| 57 | } |
| 58 | |