microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c764d5e2bcaf8e8de611998b26b56ffef0f1d583

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 * as path from "path";
6import * as Q from "q";
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 getPipePath(pipeName: string): string;
16 getPlatformId(): HostPlatformId;
17 setEnvironmentVariable(name: string, value: string): Q.Promise<void>;
18 getUserID(): string;
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): Q.Promise<any> {
39 return new ChildProcess().exec(`setx ${name} ${value}`).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 getPipePath(pipeName: string): string {
51 return `\\\\?\\pipe\\${pipeName}`;
52 }
53
54 public getPlatformId(): HostPlatformId {
55 return HostPlatformId.WINDOWS;
56 }
57
58 public getUserID(): string {
59 return process.env.USERNAME;
60 }
61}
62
63abstract class UnixHostPlatform implements IHostPlatform {
64 public getUserHomePath(): string {
65 return process.env.HOME;
66 }
67
68 public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>;
69
70 public getSettingsHome(): string {
71 return path.join(process.env.HOME, ".vscode-react-native");
72 }
73
74 public getNpmCliCommand(packageName: string): string {
75 return packageName;
76 }
77
78 public getPipePath(pipeName: string): string {
79 return `/tmp/${pipeName}.sock`;
80 }
81
82 public abstract getPlatformId(): HostPlatformId;
83
84 public abstract getUserID(): string;
85}
86
87/**
88 * IHostPlatform implemenation for the OSX platform.
89 */
90class OSXHostPlatform extends UnixHostPlatform {
91 public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
92 return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome;
93 }
94
95 public getPlatformId(): HostPlatformId {
96 return HostPlatformId.OSX;
97 }
98
99 public getUserID(): string {
100 return process.env.LOGNAME;
101 }
102}
103
104/**
105 * IHostPlatform implemenation for the Linux platform.
106 */
107class LinuxHostPlatform extends UnixHostPlatform {
108 public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
109 return new ChildProcess().exec(`export ${name}=${value}`).outcome;
110 }
111
112 public getPlatformId(): HostPlatformId {
113 return HostPlatformId.LINUX;
114 }
115
116 public getUserID(): string {
117 return process.env.USER;
118 }
119}
120
121/**
122 * Allows platform specific operations based on the user's OS.
123 */
124export class HostPlatform {
125
126 private static platformInstance: IHostPlatform;
127
128 /**
129 * Resolves the dev machine, desktop platform.
130 */
131 private static get platform(): IHostPlatform {
132 if (!HostPlatform.platformInstance) {
133 switch (process.platform) {
134 case "win32":
135 HostPlatform.platformInstance = new WindowsHostPlatform();
136 break;
137 case "darwin":
138 HostPlatform.platformInstance = new OSXHostPlatform();
139 break;
140 case "linux":
141 HostPlatform.platformInstance = new LinuxHostPlatform();
142 break;
143 default:
144 HostPlatform.platformInstance = new LinuxHostPlatform();
145 break;
146 }
147 }
148
149 return HostPlatform.platformInstance;
150 }
151
152 public static getUserHomePath(): string {
153 return HostPlatform.platform.getUserHomePath();
154 }
155
156 public static getSettingsHome(): string {
157 return HostPlatform.platform.getSettingsHome();
158 }
159
160 public static getNpmCliCommand(packageName: string): string {
161 return HostPlatform.platform.getNpmCliCommand(packageName);
162 }
163
164 public static getPipePath(pipeName: string): string {
165 return HostPlatform.platform.getPipePath(pipeName);
166 }
167
168 public static getPlatformId(): HostPlatformId {
169 return HostPlatform.platform.getPlatformId();
170 }
171
172 public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> {
173 return HostPlatform.platform.setEnvironmentVariable(name, value);
174 }
175
176 /* Returns a value that is unique for each user of this computer */
177 public static getUserID(): string {
178 return HostPlatform.platform.getUserID();
179 }
180}