microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/windows/windowsPlatform.ts
138lines · modeblame
299883f9Artem Egorov8 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
7bc455e9Artem Egorov8 years ago | 4 | import * as semver from "semver"; |
4cd25962JiglioNero4 years ago | 5 | import { GeneralPlatform, MobilePlatformDeps, TargetType } from "../generalPlatform"; |
4dfc9ffdRedMickey5 years ago | 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"; | |
d7d405aeYuri Skorokhodov7 years ago | 10 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
cfbe5cc9etatanova5 years ago | 11 | import { AppLauncher } from "../appLauncher"; |
47927908RedMickey4 years ago | 12 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; |
299883f9Artem Egorov8 years ago | 13 | |
| 14 | /** | |
| 15 | * Windows specific platform implementation for debugging RN applications. | |
| 16 | */ | |
4cd25962JiglioNero4 years ago | 17 | export class WindowsPlatform extends GeneralPlatform { |
7bc455e9Artem Egorov8 years ago | 18 | protected static NO_PACKAGER_VERSION = "0.53.0"; |
299883f9Artem Egorov8 years ago | 19 | |
47927908RedMickey4 years ago | 20 | private static readonly RNW_CLI_EXISTS_VERSION = "0.63.0"; |
34472878RedMickey5 years ago | 21 | private static SUCCESS_PATTERNS = ["Starting the app"]; |
299883f9Artem Egorov8 years ago | 22 | private static FAILURE_PATTERNS: PatternToFailure[] = [ |
| 23 | { | |
| 24 | pattern: "Unrecognized command 'run-windows'", | |
d7d405aeYuri Skorokhodov7 years ago | 25 | errorCode: InternalErrorCode.WinRNMPPluginIsNotInstalled, |
299883f9Artem Egorov8 years ago | 26 | }, |
60ad4ec0JiglioNero5 years ago | 27 | { |
| 28 | pattern: /×.+$/gm, | |
| 29 | errorCode: InternalErrorCode.WinRunCommandFailed, | |
| 30 | }, | |
299883f9Artem Egorov8 years ago | 31 | ]; |
| 32 | | |
0d77292aJiglioNero4 years ago | 33 | public async reloadApp(appLauncher: AppLauncher): Promise<void> { |
cfbe5cc9etatanova5 years ago | 34 | const worker = appLauncher.getAppWorker(); |
| 35 | if (worker) { | |
| 36 | worker.reloadAppCommand(); | |
| 37 | } | |
| 38 | } | |
| 39 | | |
299883f9Artem Egorov8 years ago | 40 | constructor(protected runOptions: IWindowsRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 41 | super(runOptions, platformDeps); | |
| 42 | } | |
| 43 | | |
0d77292aJiglioNero4 years ago | 44 | public async runApp(enableDebug: boolean = true): Promise<void> { |
008d88e5RedMickey4 years ago | 45 | let extProps: any = { |
031832ffArtem Egorov8 years ago | 46 | platform: { |
259c018fYuri Skorokhodov5 years ago | 47 | value: PlatformType.Windows, |
031832ffArtem Egorov8 years ago | 48 | isPii: false, |
| 49 | }, | |
| 50 | }; | |
| 51 | | |
e7a2c40dRedMickey4 years ago | 52 | this.projectObserver?.updateRNWindowsProjectState(true); |
008d88e5RedMickey4 years ago | 53 | if (this.runOptions.isDirect) { |
| 54 | extProps.isDirect = { | |
| 55 | value: true, | |
| 56 | isPii: false, | |
| 57 | }; | |
e7a2c40dRedMickey4 years ago | 58 | this.projectObserver?.updateRNWindowsHermesProjectState(true); |
008d88e5RedMickey4 years ago | 59 | } |
| 60 | | |
34472878RedMickey5 years ago | 61 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( |
| 62 | this.runOptions, | |
| 63 | this.runOptions.reactNativeVersions, | |
| 64 | extProps, | |
| 65 | ); | |
ba953e9fRedMickey6 years ago | 66 | |
0d77292aJiglioNero4 years ago | 67 | await TelemetryHelper.generate("WindowsPlatform.runApp", extProps, async () => { |
4cd25962JiglioNero4 years ago | 68 | const env = GeneralPlatform.getEnvArgument( |
34472878RedMickey5 years ago | 69 | process.env, |
| 70 | this.runOptions.env, | |
| 71 | this.runOptions.envFile, | |
| 72 | ); | |
299883f9Artem Egorov8 years ago | 73 | |
34472878RedMickey5 years ago | 74 | if ( |
47927908RedMickey4 years ago | 75 | semver.gte( |
| 76 | this.runOptions.reactNativeVersions.reactNativeWindowsVersion, | |
| 77 | WindowsPlatform.RNW_CLI_EXISTS_VERSION, | |
| 78 | ) || | |
| 79 | ProjectVersionHelper.isCanaryVersion( | |
| 80 | this.runOptions.reactNativeVersions.reactNativeWindowsVersion, | |
| 81 | ) | |
34472878RedMickey5 years ago | 82 | ) { |
4dfc9ffdRedMickey5 years ago | 83 | this.runArguments.push("--logging"); |
| 84 | if (enableDebug) { | |
008d88e5RedMickey4 years ago | 85 | this.runOptions.isDirect |
| 86 | ? this.runArguments.push("--direct-debugging") | |
| 87 | : this.runArguments.push("--remote-debugging"); | |
4dfc9ffdRedMickey5 years ago | 88 | } |
299883f9Artem Egorov8 years ago | 89 | } |
| 90 | | |
34472878RedMickey5 years ago | 91 | if ( |
| 92 | !semver.valid( | |
| 93 | this.runOptions.reactNativeVersions.reactNativeVersion, | |
09f6024fHeniker4 years ago | 94 | ) /* Custom RN implementations should support this flag*/ || |
34472878RedMickey5 years ago | 95 | semver.gte( |
| 96 | this.runOptions.reactNativeVersions.reactNativeVersion, | |
| 97 | WindowsPlatform.NO_PACKAGER_VERSION, | |
47927908RedMickey4 years ago | 98 | ) || |
| 99 | ProjectVersionHelper.isCanaryVersion( | |
| 100 | this.runOptions.reactNativeVersions.reactNativeVersion, | |
34472878RedMickey5 years ago | 101 | ) |
| 102 | ) { | |
7fa90b3bRedMickey6 years ago | 103 | this.runArguments.push("--no-packager"); |
| 104 | } | |
299883f9Artem Egorov8 years ago | 105 | |
34472878RedMickey5 years ago | 106 | const runWindowsSpawn = new CommandExecutor( |
4dfb1c4cetatanova5 years ago | 107 | this.runOptions.nodeModulesRoot, |
34472878RedMickey5 years ago | 108 | this.projectPath, |
| 109 | this.logger, | |
| 110 | ).spawnReactCommand(`run-${this.platformName}`, this.runArguments, { env }); | |
0d77292aJiglioNero4 years ago | 111 | await new OutputVerifier( |
34472878RedMickey5 years ago | 112 | () => Promise.resolve(WindowsPlatform.SUCCESS_PATTERNS), |
| 113 | () => Promise.resolve(WindowsPlatform.FAILURE_PATTERNS), | |
| 114 | this.platformName, | |
| 115 | ).process(runWindowsSpawn); | |
299883f9Artem Egorov8 years ago | 116 | }); |
| 117 | } | |
| 118 | | |
ce5e88eeYuri Skorokhodov5 years ago | 119 | public prewarmBundleCache(): Promise<void> { |
259c018fYuri Skorokhodov5 years ago | 120 | return this.packager.prewarmBundleCache(PlatformType.Windows); |
299883f9Artem Egorov8 years ago | 121 | } |
| 122 | | |
cbc7ac5bArtem Egorov7 years ago | 123 | public getRunArguments(): string[] { |
09f6024fHeniker4 years ago | 124 | const runArguments: string[] = []; |
299883f9Artem Egorov8 years ago | 125 | |
4dfc9ffdRedMickey5 years ago | 126 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
299883f9Artem Egorov8 years ago | 127 | runArguments.push(...this.runOptions.runArguments); |
| 128 | } else { | |
09f6024fHeniker4 years ago | 129 | const target = |
4cd25962JiglioNero4 years ago | 130 | this.runOptions.target === TargetType.Simulator ? "" : this.runOptions.target; |
dd90a856Artem Egorov8 years ago | 131 | if (target) { |
| 132 | runArguments.push(`--${target}`); | |
299883f9Artem Egorov8 years ago | 133 | } |
| 134 | } | |
| 135 | | |
| 136 | return runArguments; | |
| 137 | } | |
| 138 | } |