microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.17

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

220lines · modeblame

8411fd8dDaniel Lebu10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
a1005420dlebu10 years ago4import {ChildProcess} from "./node/childProcess";
aeaf46bcMeena Kunnathur Balakrishnan10 years ago5import {TargetPlatformId} from "./targetPlatformHelper";
8411fd8dDaniel Lebu10 years ago6import * as path from "path";
62d9cdd3dlebu10 years ago7import * as Q from "q";
8411fd8dDaniel Lebu10 years ago8
9/**
10* Interface defining the host (desktop) platform specific operations.
11*/
a1005420dlebu10 years ago12interface IHostPlatform {
8411fd8dDaniel Lebu10 years ago13getUserHomePath(): string;
14getSettingsHome(): string;
a9955be6dlebu10 years ago15getNpmCliCommand(packageName: string): string;
d3950d7edigeff10 years ago16getPipePath(pipeName: string): string;
8411fd8dDaniel Lebu10 years ago17getPlatformId(): HostPlatformId;
62d9cdd3dlebu10 years ago18setEnvironmentVariable(name: string, value: string): Q.Promise<void>;
d3950d7edigeff10 years ago19getUserID(): string;
a98974c3Daniel Lebu10 years ago20isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean;
8411fd8dDaniel Lebu10 years ago21}
22
23/**
24* Defines the identifiers of all the platforms we support.
25*/
26export enum HostPlatformId {
27WINDOWS,
bbdbbb3bdlebu10 years ago28OSX,
27710197Vladimir Kotikov8 years ago29LINUX,
8411fd8dDaniel Lebu10 years ago30}
31
32/**
33* IHostPlatform implemenation for the Windows platform.
34*/
35class WindowsHostPlatform implements IHostPlatform {
36public getUserHomePath(): string {
3ff1d921annakocheshkova8 years ago37return process.env.USERPROFILE || "";
8411fd8dDaniel Lebu10 years ago38}
39
62d9cdd3dlebu10 years ago40public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago41return new ChildProcess().exec(`setx ${name} ${value}`).outcome;
8411fd8dDaniel Lebu10 years ago42}
43
44public getSettingsHome(): string {
3ff1d921annakocheshkova8 years ago45return path.join(process.env.APPDATA || "", "vscode-react-native");
8411fd8dDaniel Lebu10 years ago46}
47
a9955be6dlebu10 years ago48public getNpmCliCommand(cliName: string): string {
49return `${cliName}.cmd`;
8411fd8dDaniel Lebu10 years ago50}
51
d3950d7edigeff10 years ago52public getPipePath(pipeName: string): string {
7daed3fcArtem Egorov8 years ago53return `//?/pipe/${pipeName}`;
8411fd8dDaniel Lebu10 years ago54}
55
56public getPlatformId(): HostPlatformId {
57return HostPlatformId.WINDOWS;
58}
d3950d7edigeff10 years ago59
60public getUserID(): string {
3ff1d921annakocheshkova8 years ago61return process.env.USERNAME || "";
d3950d7edigeff10 years ago62}
fced706ddigeff10 years ago63
a98974c3Daniel Lebu10 years ago64public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
299883f9Artem Egorov8 years ago65switch (targetPlatformId) {
66case TargetPlatformId.ANDROID:
67case TargetPlatformId.EXPONENT:
68case TargetPlatformId.WINDOWS:
69return true;
70default:
71return false;
72}
42fac1f6Meena Kunnathur Balakrishnan10 years ago73}
8411fd8dDaniel Lebu10 years ago74}
75
bbdbbb3bdlebu10 years ago76abstract class UnixHostPlatform implements IHostPlatform {
8411fd8dDaniel Lebu10 years ago77public getUserHomePath(): string {
3ff1d921annakocheshkova8 years ago78return process.env.HOME || "";
8411fd8dDaniel Lebu10 years ago79}
80
bbdbbb3bdlebu10 years ago81public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>;
8411fd8dDaniel Lebu10 years ago82
83public getSettingsHome(): string {
3ff1d921annakocheshkova8 years ago84return path.join(process.env.HOME || "", ".vscode-react-native");
8411fd8dDaniel Lebu10 years ago85}
86
a9955be6dlebu10 years ago87public getNpmCliCommand(packageName: string): string {
bbdbbb3bdlebu10 years ago88return packageName;
8411fd8dDaniel Lebu10 years ago89}
90
d3950d7edigeff10 years ago91public getPipePath(pipeName: string): string {
92return `/tmp/${pipeName}.sock`;
8411fd8dDaniel Lebu10 years ago93}
94
bbdbbb3bdlebu10 years ago95public abstract getPlatformId(): HostPlatformId;
d3950d7edigeff10 years ago96
97public abstract getUserID(): string;
fced706ddigeff10 years ago98
a98974c3Daniel Lebu10 years ago99public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean;
8411fd8dDaniel Lebu10 years ago100}
101
bbdbbb3bdlebu10 years ago102/**
103* IHostPlatform implemenation for the OSX platform.
104*/
105class OSXHostPlatform extends UnixHostPlatform {
106public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago107return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome;
bbdbbb3bdlebu10 years ago108}
109
110public getPlatformId(): HostPlatformId {
111return HostPlatformId.OSX;
112}
d3950d7edigeff10 years ago113
114public getUserID(): string {
3ff1d921annakocheshkova8 years ago115return process.env.LOGNAME || "";
d3950d7edigeff10 years ago116}
fced706ddigeff10 years ago117
a98974c3Daniel Lebu10 years ago118public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
299883f9Artem Egorov8 years ago119switch (targetPlatformId) {
120case TargetPlatformId.ANDROID:
121case TargetPlatformId.EXPONENT:
122case TargetPlatformId.IOS:
123return true;
124default:
125return false;
126}
e09724a1Meena Kunnathur Balakrishnan10 years ago127}
bbdbbb3bdlebu10 years ago128}
129
130/**
131* IHostPlatform implemenation for the Linux platform.
132*/
133class LinuxHostPlatform extends UnixHostPlatform {
134public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago135return new ChildProcess().exec(`export ${name}=${value}`).outcome;
bbdbbb3bdlebu10 years ago136}
137
138public getPlatformId(): HostPlatformId {
139return HostPlatformId.LINUX;
140}
d3950d7edigeff10 years ago141
142public getUserID(): string {
3ff1d921annakocheshkova8 years ago143return process.env.USER || "";
d3950d7edigeff10 years ago144}
fced706ddigeff10 years ago145
a98974c3Daniel Lebu10 years ago146public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
299883f9Artem Egorov8 years ago147switch (targetPlatformId) {
148case TargetPlatformId.ANDROID:
149case TargetPlatformId.EXPONENT:
150return true;
151default:
152return false;
153}
e09724a1Meena Kunnathur Balakrishnan10 years ago154}
bbdbbb3bdlebu10 years ago155}
156
8411fd8dDaniel Lebu10 years ago157/**
a1005420dlebu10 years ago158* Allows platform specific operations based on the user's OS.
8411fd8dDaniel Lebu10 years ago159*/
a1005420dlebu10 years ago160export class HostPlatform {
8411fd8dDaniel Lebu10 years ago161
a1005420dlebu10 years ago162private static platformInstance: IHostPlatform;
8411fd8dDaniel Lebu10 years ago163
164/**
165* Resolves the dev machine, desktop platform.
166*/
a1005420dlebu10 years ago167private static get platform(): IHostPlatform {
168if (!HostPlatform.platformInstance) {
169switch (process.platform) {
170case "win32":
171HostPlatform.platformInstance = new WindowsHostPlatform();
9fe4ca93digeff10 years ago172break;
a1005420dlebu10 years ago173case "darwin":
174HostPlatform.platformInstance = new OSXHostPlatform();
9fe4ca93digeff10 years ago175break;
a1005420dlebu10 years ago176case "linux":
c6d7ee27dlebu10 years ago177HostPlatform.platformInstance = new LinuxHostPlatform();
178break;
a1005420dlebu10 years ago179default:
180HostPlatform.platformInstance = new LinuxHostPlatform();
aab2095edigeff10 years ago181break;
a1005420dlebu10 years ago182}
8411fd8dDaniel Lebu10 years ago183}
a1005420dlebu10 years ago184
185return HostPlatform.platformInstance;
186}
187
188public static getUserHomePath(): string {
189return HostPlatform.platform.getUserHomePath();
190}
191
192public static getSettingsHome(): string {
193return HostPlatform.platform.getSettingsHome();
194}
195
196public static getNpmCliCommand(packageName: string): string {
a9955be6dlebu10 years ago197return HostPlatform.platform.getNpmCliCommand(packageName);
a1005420dlebu10 years ago198}
199
d3950d7edigeff10 years ago200public static getPipePath(pipeName: string): string {
201return HostPlatform.platform.getPipePath(pipeName);
a1005420dlebu10 years ago202}
203
204public static getPlatformId(): HostPlatformId {
205return HostPlatform.platform.getPlatformId();
206}
207
208public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> {
209return HostPlatform.platform.setEnvironmentVariable(name, value);
8411fd8dDaniel Lebu10 years ago210}
d3950d7edigeff10 years ago211
212/* Returns a value that is unique for each user of this computer */
213public static getUserID(): string {
214return HostPlatform.platform.getUserID();
215}
fced706ddigeff10 years ago216
a98974c3Daniel Lebu10 years ago217public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
218return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId);
42fac1f6Meena Kunnathur Balakrishnan10 years ago219}
8411fd8dDaniel Lebu10 years ago220}