microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3a0bfdb2c230b6319fe09bfd70e3bae29d0fe088

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

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