microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8df5011e47ada44be3951868ba32e5fae98f48bb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

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