microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/hostPlatform.ts
129lines · 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 child_process from "child_process"; |
| 5 | import {Node} from "./node/node"; |
| 6 | import * as path from "path"; |
| 7 | import * as Q from "q"; |
| 8 | |
| 9 | /** |
| 10 | * Interface defining the host (desktop) platform specific operations. |
| 11 | */ |
| 12 | export interface IHostPlatform { |
| 13 | getUserHomePath(): string; |
| 14 | getSettingsHome(): string; |
| 15 | getNpmCommand(): string; |
| 16 | getReactNativeCommand(): string; |
| 17 | getExtensionPipePath(): string; |
| 18 | getPlatformId(): HostPlatformId; |
| 19 | killProcess(process: child_process.ChildProcess): Q.Promise<void>; |
| 20 | setEnvironmentVariable(name: string, value: string): Q.Promise<void>; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Defines the identifiers of all the platforms we support. |
| 25 | */ |
| 26 | export enum HostPlatformId { |
| 27 | WINDOWS, |
| 28 | OSX |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * IHostPlatform implemenation for the Windows platform. |
| 33 | */ |
| 34 | class WindowsHostPlatform implements IHostPlatform { |
| 35 | public getUserHomePath(): string { |
| 36 | return process.env.USERPROFILE; |
| 37 | } |
| 38 | |
| 39 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
| 40 | return Q.nfcall(child_process.exec, `setx ${name} ${value}`); |
| 41 | } |
| 42 | |
| 43 | public getReactNativeCommand() { |
| 44 | return "react-native.cmd"; |
| 45 | } |
| 46 | |
| 47 | public getSettingsHome(): string { |
| 48 | return path.join(process.env.APPDATA, "vscode-react-native"); |
| 49 | } |
| 50 | |
| 51 | public getNpmCommand(): string { |
| 52 | return "npm.cmd"; |
| 53 | } |
| 54 | |
| 55 | public getExtensionPipePath(): string { |
| 56 | return "\\\\?\\pipe\\vscodereactnative"; |
| 57 | } |
| 58 | |
| 59 | public getPlatformId(): HostPlatformId { |
| 60 | return HostPlatformId.WINDOWS; |
| 61 | } |
| 62 | |
| 63 | public killProcess(process: child_process.ChildProcess): Q.Promise<void> { |
| 64 | return new Node.ChildProcess().exec("taskkill /pid " + process.pid + " /T /F").outcome.then(() => { |
| 65 | return Q.resolve<void>(void 0); |
| 66 | }); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * IHostPlatform implemenation for the OSX platform. |
| 72 | */ |
| 73 | class OSXHostPlatform implements IHostPlatform { |
| 74 | public getUserHomePath(): string { |
| 75 | return process.env.HOME; |
| 76 | } |
| 77 | |
| 78 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
| 79 | return Q.nfcall(child_process.exec, `launchctl setenv ${name} ${value}`); |
| 80 | } |
| 81 | |
| 82 | public getReactNativeCommand() { |
| 83 | return "react-native"; |
| 84 | } |
| 85 | |
| 86 | public getSettingsHome(): string { |
| 87 | return path.join(process.env.HOME, ".vscode-react-native"); |
| 88 | } |
| 89 | |
| 90 | public getNpmCommand(): string { |
| 91 | return "npm"; |
| 92 | } |
| 93 | |
| 94 | public getExtensionPipePath(): string { |
| 95 | return "/tmp/vscodereactnative.sock"; |
| 96 | } |
| 97 | |
| 98 | public getPlatformId(): HostPlatformId { |
| 99 | return HostPlatformId.OSX; |
| 100 | } |
| 101 | |
| 102 | public killProcess(process: child_process.ChildProcess): Q.Promise<void> { |
| 103 | process.kill(); |
| 104 | return Q.resolve<void>(void 0); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Resolves the host platform. |
| 110 | */ |
| 111 | export class HostPlatformResolver { |
| 112 | |
| 113 | private static WinPlatformInstance = new WindowsHostPlatform(); |
| 114 | private static OSXPlatformInstance = new OSXHostPlatform(); |
| 115 | |
| 116 | /** |
| 117 | * Resolves the dev machine, desktop platform. |
| 118 | */ |
| 119 | public static getHostPlatform(): IHostPlatform { |
| 120 | let platform = process.platform; |
| 121 | switch (platform) { |
| 122 | case "win32": |
| 123 | return HostPlatformResolver.WinPlatformInstance; |
| 124 | case "darwin": |
| 125 | default: |
| 126 | return HostPlatformResolver.OSXPlatformInstance; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |