microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.4.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

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