microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/macos/macOSPlatform.ts
95lines · 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 { ImacOSRunOptions, 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 | |
| 12 | /** |
| 13 | * macOS specific platform implementation for debugging RN applications. |
| 14 | */ |
| 15 | export class MacOSPlatform extends GeneralMobilePlatform { |
| 16 | private static SUCCESS_PATTERNS = ["Launching app"]; |
| 17 | private static FAILURE_PATTERNS: PatternToFailure[] = [ |
| 18 | { |
| 19 | pattern: "Unrecognized command 'run-macos'", |
| 20 | errorCode: InternalErrorCode.ReactNativemacOSIsNotInstalled, |
| 21 | }, |
| 22 | ]; |
| 23 | |
| 24 | constructor(protected runOptions: ImacOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 25 | super(runOptions, platformDeps); |
| 26 | } |
| 27 | |
| 28 | public runApp(): Promise<void> { |
| 29 | let extProps = { |
| 30 | platform: { |
| 31 | value: PlatformType.macOS, |
| 32 | isPii: false, |
| 33 | }, |
| 34 | }; |
| 35 | |
| 36 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( |
| 37 | this.runOptions, |
| 38 | this.runOptions.reactNativeVersions, |
| 39 | extProps, |
| 40 | ); |
| 41 | |
| 42 | return TelemetryHelper.generate("MacOSPlatform.runApp", extProps, () => { |
| 43 | const env = GeneralMobilePlatform.getEnvArgument( |
| 44 | process.env, |
| 45 | this.runOptions.env, |
| 46 | this.runOptions.envFile, |
| 47 | ); |
| 48 | |
| 49 | if ( |
| 50 | !semver.valid( |
| 51 | this.runOptions.reactNativeVersions.reactNativeVersion, |
| 52 | ) /*Custom RN implementations should support this flag*/ || |
| 53 | semver.gte( |
| 54 | this.runOptions.reactNativeVersions.reactNativeVersion, |
| 55 | MacOSPlatform.NO_PACKAGER_VERSION, |
| 56 | ) |
| 57 | ) { |
| 58 | this.runArguments.push("--no-packager"); |
| 59 | } |
| 60 | |
| 61 | const runmacOSSpawn = new CommandExecutor( |
| 62 | this.runOptions.nodeModulesRoot, |
| 63 | this.projectPath, |
| 64 | this.logger, |
| 65 | ).spawnReactCommand(`run-${this.platformName}`, this.runArguments, { env }); |
| 66 | return new OutputVerifier( |
| 67 | () => Promise.resolve(MacOSPlatform.SUCCESS_PATTERNS), |
| 68 | () => Promise.resolve(MacOSPlatform.FAILURE_PATTERNS), |
| 69 | this.platformName, |
| 70 | ).process(runmacOSSpawn); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | public prewarmBundleCache(): Promise<void> { |
| 75 | return this.packager.prewarmBundleCache(PlatformType.macOS); |
| 76 | } |
| 77 | |
| 78 | public getRunArguments(): string[] { |
| 79 | let runArguments: string[] = []; |
| 80 | |
| 81 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 82 | runArguments.push(...this.runOptions.runArguments); |
| 83 | } else { |
| 84 | let target = |
| 85 | this.runOptions.target === MacOSPlatform.simulatorString |
| 86 | ? "" |
| 87 | : this.runOptions.target; |
| 88 | if (target) { |
| 89 | runArguments.push(`--${target}`); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return runArguments; |
| 94 | } |
| 95 | } |