microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

206lines · 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().exec(`launchctl setenv ${name} ${value}`).then(res => res.outcome);
98 }
99
100 public getPlatformId(): HostPlatformId {
101 return HostPlatformId.OSX;
102 }
103
104 public getUserID(): string {
105 return process.env.LOGNAME || "";
106 }
107
108 public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
109 switch (targetPlatformId) {
110 case TargetPlatformId.ANDROID:
111 case TargetPlatformId.EXPONENT:
112 case TargetPlatformId.IOS:
113 return true;
114 default:
115 return false;
116 }
117 }
118}
119
120/**
121 * IHostPlatform implemenation for the Linux platform.
122 */
123class LinuxHostPlatform extends UnixHostPlatform {
124 public setEnvironmentVariable(name: string, value: string): Promise<any> {
125 return new ChildProcess().exec(`export ${name}=${value}`).then(res => res.outcome);
126 }
127
128 public getPlatformId(): HostPlatformId {
129 return HostPlatformId.LINUX;
130 }
131
132 public getUserID(): string {
133 return process.env.USER || "";
134 }
135
136 public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
137 switch (targetPlatformId) {
138 case TargetPlatformId.ANDROID:
139 case TargetPlatformId.EXPONENT:
140 return true;
141 default:
142 return false;
143 }
144 }
145}
146
147/**
148 * Allows platform specific operations based on the user's OS.
149 */
150export class HostPlatform {
151
152 private static platformInstance: IHostPlatform;
153
154 /**
155 * Resolves the dev machine, desktop platform.
156 */
157 private static get platform(): IHostPlatform {
158 if (!HostPlatform.platformInstance) {
159 switch (process.platform) {
160 case "win32":
161 HostPlatform.platformInstance = new WindowsHostPlatform();
162 break;
163 case "darwin":
164 HostPlatform.platformInstance = new OSXHostPlatform();
165 break;
166 case "linux":
167 HostPlatform.platformInstance = new LinuxHostPlatform();
168 break;
169 default:
170 HostPlatform.platformInstance = new LinuxHostPlatform();
171 break;
172 }
173 }
174
175 return HostPlatform.platformInstance;
176 }
177
178 public static getUserHomePath(): string {
179 return HostPlatform.platform.getUserHomePath();
180 }
181
182 public static getSettingsHome(): string {
183 return HostPlatform.platform.getSettingsHome();
184 }
185
186 public static getNpmCliCommand(packageName: string): string {
187 return HostPlatform.platform.getNpmCliCommand(packageName);
188 }
189
190 public static getPlatformId(): HostPlatformId {
191 return HostPlatform.platform.getPlatformId();
192 }
193
194 public static setEnvironmentVariable(name: string, value: string): Promise<void> {
195 return HostPlatform.platform.setEnvironmentVariable(name, value);
196 }
197
198 /* Returns a value that is unique for each user of this computer */
199 public static getUserID(): string {
200 return HostPlatform.platform.getUserID();
201 }
202
203 public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
204 return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId);
205 }
206}
207