microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8408d326facd026be34282fa91cc873b2abd7246

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

180lines · 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
4import {ChildProcess} from "./node/childProcess";
5import {TargetPlatformId} from "./targetPlatformHelper";
6import * as path from "path";
7import * as Q from "q";
8
9/**
10 * Interface defining the host (desktop) platform specific operations.
11 */
12interface IHostPlatform {
13 getUserHomePath(): string;
14 getSettingsHome(): string;
15 getNpmCliCommand(packageName: string): string;
16 getExtensionPipePath(): string;
17 getPlatformId(): HostPlatformId;
18 setEnvironmentVariable(name: string, value: string): Q.Promise<void>;
19 isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean;
20}
21
22/**
23 * Defines the identifiers of all the platforms we support.
24 */
25export enum HostPlatformId {
26 WINDOWS,
27 OSX,
28 LINUX
29}
30
31/**
32 * IHostPlatform implemenation for the Windows platform.
33 */
34class 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 new ChildProcess().exec(`setx ${name} ${value}`).outcome;
41 }
42
43 public getSettingsHome(): string {
44 return path.join(process.env.APPDATA, "vscode-react-native");
45 }
46
47 public getNpmCliCommand(cliName: string): string {
48 return `${cliName}.cmd`;
49 }
50
51 public getExtensionPipePath(): string {
52 return "\\\\?\\pipe\\vscodereactnative";
53 }
54
55 public getPlatformId(): HostPlatformId {
56 return HostPlatformId.WINDOWS;
57 }
58
59 public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
60 return targetPlatformId === TargetPlatformId.ANDROID;
61 }
62}
63
64abstract class UnixHostPlatform implements IHostPlatform {
65 public getUserHomePath(): string {
66 return process.env.HOME;
67 }
68
69 public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>;
70
71 public getSettingsHome(): string {
72 return path.join(process.env.HOME, ".vscode-react-native");
73 }
74
75 public getNpmCliCommand(packageName: string): string {
76 return packageName;
77 }
78
79 public getExtensionPipePath(): string {
80 return "/tmp/vscodereactnative.sock";
81 }
82
83 public abstract getPlatformId(): HostPlatformId;
84
85 public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean;
86}
87
88/**
89 * IHostPlatform implemenation for the OSX platform.
90 */
91class OSXHostPlatform extends UnixHostPlatform {
92 public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
93 return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome;
94 }
95
96 public getPlatformId(): HostPlatformId {
97 return HostPlatformId.OSX;
98 }
99
100 public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
101 return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.IOS;
102 }
103}
104
105/**
106 * IHostPlatform implemenation for the Linux platform.
107 */
108class LinuxHostPlatform extends UnixHostPlatform {
109 public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
110 return new ChildProcess().exec(`export ${name}=${value}`).outcome;
111 }
112
113 public getPlatformId(): HostPlatformId {
114 return HostPlatformId.LINUX;
115 }
116
117 public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
118 return targetPlatformId === TargetPlatformId.ANDROID;
119 }
120}
121
122/**
123 * Allows platform specific operations based on the user's OS.
124 */
125export class HostPlatform {
126
127 private static platformInstance: IHostPlatform;
128
129 /**
130 * Resolves the dev machine, desktop platform.
131 */
132 private static get platform(): IHostPlatform {
133 if (!HostPlatform.platformInstance) {
134 switch (process.platform) {
135 case "win32":
136 HostPlatform.platformInstance = new WindowsHostPlatform();
137 break;
138 case "darwin":
139 HostPlatform.platformInstance = new OSXHostPlatform();
140 break;
141 case "linux":
142 HostPlatform.platformInstance = new LinuxHostPlatform();
143 break;
144 default:
145 HostPlatform.platformInstance = new LinuxHostPlatform();
146 break;
147 }
148 }
149
150 return HostPlatform.platformInstance;
151 }
152
153 public static getUserHomePath(): string {
154 return HostPlatform.platform.getUserHomePath();
155 }
156
157 public static getSettingsHome(): string {
158 return HostPlatform.platform.getSettingsHome();
159 }
160
161 public static getNpmCliCommand(packageName: string): string {
162 return HostPlatform.platform.getNpmCliCommand(packageName);
163 }
164
165 public static getExtensionPipePath(): string {
166 return HostPlatform.platform.getExtensionPipePath();
167 }
168
169 public static getPlatformId(): HostPlatformId {
170 return HostPlatform.platform.getPlatformId();
171 }
172
173 public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> {
174 return HostPlatform.platform.setEnvironmentVariable(name, value);
175 }
176
177 public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
178 return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId);
179 }
180}
181