microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commands/reloadApp.ts
72lines · 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 assert from "assert"; |
| 5 | import * as nls from "vscode-nls"; |
| 6 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 7 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 8 | import { ProjectVersionHelper, REACT_NATIVE_PACKAGES } from "../../common/projectVersionHelper"; |
| 9 | import { PlatformType } from "../launchArgs"; |
| 10 | import { AndroidPlatform } from "../android/androidPlatform"; |
| 11 | import { IOSPlatform } from "../ios/iOSPlatform"; |
| 12 | import { WindowsPlatform } from "../windows/windowsPlatform"; |
| 13 | import { getRunOptions } from "./util"; |
| 14 | import { Command } from "./util/command"; |
| 15 | |
| 16 | nls.config({ |
| 17 | messageFormat: nls.MessageFormat.bundle, |
| 18 | bundleFormat: nls.BundleFormat.standalone, |
| 19 | })(); |
| 20 | const localize = nls.loadMessageBundle(); |
| 21 | |
| 22 | export class ReloadApp extends Command { |
| 23 | codeName = "reloadApp"; |
| 24 | requiresTrust = false; |
| 25 | label = "ReloadApp"; |
| 26 | error = ErrorHelper.getInternalError( |
| 27 | InternalErrorCode.CommandFailed, |
| 28 | localize("ReactNativeReloadApp", "React Native: Reload App"), |
| 29 | ); |
| 30 | |
| 31 | async baseFn(): Promise<void> { |
| 32 | assert(this.project); |
| 33 | |
| 34 | const androidPlatform = new AndroidPlatform( |
| 35 | getRunOptions(this.project, PlatformType.Android), |
| 36 | { |
| 37 | packager: this.project.getPackager(), |
| 38 | }, |
| 39 | ); |
| 40 | |
| 41 | androidPlatform.reloadApp().catch(() => {}); |
| 42 | |
| 43 | if (process.platform === "win32") { |
| 44 | const nodeModulesRoot = this.project.getOrUpdateNodeModulesRoot(); |
| 45 | const RNPackageVersions = |
| 46 | await ProjectVersionHelper.getReactNativePackageVersionsFromNodeModules( |
| 47 | nodeModulesRoot, |
| 48 | [REACT_NATIVE_PACKAGES.REACT_NATIVE_WINDOWS], |
| 49 | ); |
| 50 | |
| 51 | const isRNWProject = !ProjectVersionHelper.isVersionError( |
| 52 | RNPackageVersions.reactNativeWindowsVersion, |
| 53 | ); |
| 54 | |
| 55 | if (isRNWProject) { |
| 56 | const windowsPlatform = new WindowsPlatform( |
| 57 | getRunOptions(this.project, PlatformType.Windows), |
| 58 | { |
| 59 | packager: this.project.getPackager(), |
| 60 | }, |
| 61 | ); |
| 62 | windowsPlatform.reloadApp(this.project).catch(() => {}); |
| 63 | } |
| 64 | } else if (process.platform === "darwin") { |
| 65 | const iosPlatform = new IOSPlatform(getRunOptions(this.project, PlatformType.iOS), { |
| 66 | packager: this.project.getPackager(), |
| 67 | }); |
| 68 | |
| 69 | iosPlatform.reloadApp(this.project).catch(() => {}); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |