microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/windows/wpfPlatform.ts
81lines · 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 | import * as semver from "semver"; |
| 6 | import * as path from "path"; |
| 7 | |
| 8 | import {MobilePlatformDeps} from "../generalMobilePlatform"; |
| 9 | import {IWindowsRunOptions} from "../launchArgs"; |
| 10 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
| 11 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 12 | import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper"; |
| 13 | import {WindowsPlatform} from "./windowsPlatform"; |
| 14 | |
| 15 | /** |
| 16 | * WPF specific platform implementation for debugging RN applications. |
| 17 | */ |
| 18 | export class WpfPlatform extends WindowsPlatform { |
| 19 | private static WPF_SUPPORTED = "0.55.0"; |
| 20 | constructor(protected runOptions: IWindowsRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 21 | super(runOptions, platformDeps); |
| 22 | } |
| 23 | |
| 24 | public runApp(enableDebug: boolean = true): Q.Promise<void> { |
| 25 | const extProps = { |
| 26 | platform: { |
| 27 | value: "wpf", |
| 28 | isPii: false, |
| 29 | }, |
| 30 | }; |
| 31 | |
| 32 | return TelemetryHelper.generate("WpfPlatform.runApp", extProps, () => { |
| 33 | const runArguments = this.getRunArgument(); |
| 34 | const env = this.getEnvArgument(); |
| 35 | |
| 36 | if (enableDebug) { |
| 37 | runArguments.push("--proxy"); |
| 38 | } |
| 39 | |
| 40 | return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot) |
| 41 | .then(version => { |
| 42 | if (!semver.gt(version, WpfPlatform.WPF_SUPPORTED)) { |
| 43 | throw new Error(`Debugging WPF platform is not supported for this react-native-windows version(${version})`); |
| 44 | } |
| 45 | |
| 46 | if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, WpfPlatform.NO_PACKAGER_VERSION)) { |
| 47 | runArguments.push("--no-packager"); |
| 48 | } |
| 49 | |
| 50 | const exec = new CommandExecutor(this.projectPath, this.logger); |
| 51 | return Q.Promise((resolve, reject) => { |
| 52 | const appName = this.projectPath.split(path.sep).pop(); |
| 53 | // Killing another instances of the app which were run earlier |
| 54 | return exec.execute(`cmd /C Taskkill /IM ${appName}.exe /F`) |
| 55 | .finally(() => { |
| 56 | const runWpfSpawn = exec.spawnReactCommand(`run-${this.platformName}`, runArguments, {env}); |
| 57 | let resolved = false; |
| 58 | let output = ""; |
| 59 | runWpfSpawn.stdout.on("data", (data: Buffer) => { |
| 60 | output += data.toString(); |
| 61 | if (!resolved && output.indexOf("Starting the app") > -1) { |
| 62 | resolved = true; |
| 63 | resolve(void 0); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | runWpfSpawn.stderr.on("data", (error: Buffer) => { |
| 68 | if (error.toString().trim()) { |
| 69 | reject(error.toString()); |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | runWpfSpawn.outcome.then(() => { |
| 74 | reject(void 0); // If WPF process ended then app run fault |
| 75 | }); |
| 76 | }); |
| 77 | }); |
| 78 | }); |
| 79 | }); |
| 80 | } |
| 81 | } |
| 82 | |