microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/windows/windowsPlatform.ts
127lines · 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 semver from "semver"; |
| 5 | import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
| 6 | import { IWindowsRunOptions, PlatformType } from "../launchArgs"; |
| 7 | import { OutputVerifier, PatternToFailure } from "../../common/outputVerifier"; |
| 8 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 9 | import { CommandExecutor } from "../../common/commandExecutor"; |
| 10 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 11 | import { AppLauncher } from "../appLauncher"; |
| 12 | |
| 13 | /** |
| 14 | * Windows specific platform implementation for debugging RN applications. |
| 15 | */ |
| 16 | export class WindowsPlatform extends GeneralMobilePlatform { |
| 17 | protected static NO_PACKAGER_VERSION = "0.53.0"; |
| 18 | |
| 19 | private static SUCCESS_PATTERNS = ["Starting the app"]; |
| 20 | private static FAILURE_PATTERNS: PatternToFailure[] = [ |
| 21 | { |
| 22 | pattern: "Unrecognized command 'run-windows'", |
| 23 | errorCode: InternalErrorCode.WinRNMPPluginIsNotInstalled, |
| 24 | }, |
| 25 | { |
| 26 | pattern: /×.+$/gm, |
| 27 | errorCode: InternalErrorCode.WinRunCommandFailed, |
| 28 | }, |
| 29 | ]; |
| 30 | |
| 31 | public async reloadApp(appLauncher: AppLauncher): Promise<void> { |
| 32 | const worker = appLauncher.getAppWorker(); |
| 33 | if (worker) { |
| 34 | worker.reloadAppCommand(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | constructor(protected runOptions: IWindowsRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 39 | super(runOptions, platformDeps); |
| 40 | } |
| 41 | |
| 42 | public async runApp(enableDebug: boolean = true): Promise<void> { |
| 43 | let extProps: any = { |
| 44 | platform: { |
| 45 | value: PlatformType.Windows, |
| 46 | isPii: false, |
| 47 | }, |
| 48 | }; |
| 49 | |
| 50 | if (this.runOptions.isDirect) { |
| 51 | extProps.isDirect = { |
| 52 | value: true, |
| 53 | isPii: false, |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( |
| 58 | this.runOptions, |
| 59 | this.runOptions.reactNativeVersions, |
| 60 | extProps, |
| 61 | ); |
| 62 | |
| 63 | await TelemetryHelper.generate("WindowsPlatform.runApp", extProps, async () => { |
| 64 | const env = GeneralMobilePlatform.getEnvArgument( |
| 65 | process.env, |
| 66 | this.runOptions.env, |
| 67 | this.runOptions.envFile, |
| 68 | ); |
| 69 | |
| 70 | if ( |
| 71 | semver.gte(this.runOptions.reactNativeVersions.reactNativeWindowsVersion, "0.63.0") |
| 72 | ) { |
| 73 | this.runArguments.push("--logging"); |
| 74 | if (enableDebug) { |
| 75 | this.runOptions.isDirect |
| 76 | ? this.runArguments.push("--direct-debugging") |
| 77 | : this.runArguments.push("--remote-debugging"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if ( |
| 82 | !semver.valid( |
| 83 | this.runOptions.reactNativeVersions.reactNativeVersion, |
| 84 | ) /*Custom RN implementations should support this flag*/ || |
| 85 | semver.gte( |
| 86 | this.runOptions.reactNativeVersions.reactNativeVersion, |
| 87 | WindowsPlatform.NO_PACKAGER_VERSION, |
| 88 | ) |
| 89 | ) { |
| 90 | this.runArguments.push("--no-packager"); |
| 91 | } |
| 92 | |
| 93 | const runWindowsSpawn = new CommandExecutor( |
| 94 | this.runOptions.nodeModulesRoot, |
| 95 | this.projectPath, |
| 96 | this.logger, |
| 97 | ).spawnReactCommand(`run-${this.platformName}`, this.runArguments, { env }); |
| 98 | await new OutputVerifier( |
| 99 | () => Promise.resolve(WindowsPlatform.SUCCESS_PATTERNS), |
| 100 | () => Promise.resolve(WindowsPlatform.FAILURE_PATTERNS), |
| 101 | this.platformName, |
| 102 | ).process(runWindowsSpawn); |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | public prewarmBundleCache(): Promise<void> { |
| 107 | return this.packager.prewarmBundleCache(PlatformType.Windows); |
| 108 | } |
| 109 | |
| 110 | public getRunArguments(): string[] { |
| 111 | let runArguments: string[] = []; |
| 112 | |
| 113 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 114 | runArguments.push(...this.runOptions.runArguments); |
| 115 | } else { |
| 116 | let target = |
| 117 | this.runOptions.target === WindowsPlatform.simulatorString |
| 118 | ? "" |
| 119 | : this.runOptions.target; |
| 120 | if (target) { |
| 121 | runArguments.push(`--${target}`); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return runArguments; |
| 126 | } |
| 127 | } |
| 128 | |