microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

160lines · 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";
8411fd8dDaniel Lebu10 years ago5import * as path from "path";
62d9cdd3dlebu10 years ago6import * as Q from "q";
8411fd8dDaniel Lebu10 years ago7
8/**
9* Interface defining the host (desktop) platform specific operations.
10*/
a1005420dlebu10 years ago11interface IHostPlatform {
8411fd8dDaniel Lebu10 years ago12getUserHomePath(): string;
13getSettingsHome(): string;
a9955be6dlebu10 years ago14getNpmCliCommand(packageName: string): string;
8411fd8dDaniel Lebu10 years ago15getExtensionPipePath(): string;
16getPlatformId(): HostPlatformId;
62d9cdd3dlebu10 years ago17setEnvironmentVariable(name: string, value: string): Q.Promise<void>;
8411fd8dDaniel Lebu10 years ago18}
19
20/**
21* Defines the identifiers of all the platforms we support.
22*/
23export enum HostPlatformId {
24WINDOWS,
bbdbbb3bdlebu10 years ago25OSX,
26LINUX
8411fd8dDaniel Lebu10 years ago27}
28
29/**
30* IHostPlatform implemenation for the Windows platform.
31*/
32class WindowsHostPlatform implements IHostPlatform {
33public getUserHomePath(): string {
34return process.env.USERPROFILE;
35}
36
62d9cdd3dlebu10 years ago37public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago38return new ChildProcess().exec(`setx ${name} ${value}`).outcome;
8411fd8dDaniel Lebu10 years ago39}
40
41public getSettingsHome(): string {
42return path.join(process.env.APPDATA, "vscode-react-native");
43}
44
a9955be6dlebu10 years ago45public getNpmCliCommand(cliName: string): string {
46return `${cliName}.cmd`;
8411fd8dDaniel Lebu10 years ago47}
48
49public getExtensionPipePath(): string {
50return "\\\\?\\pipe\\vscodereactnative";
51}
52
53public getPlatformId(): HostPlatformId {
54return HostPlatformId.WINDOWS;
55}
56}
57
bbdbbb3bdlebu10 years ago58abstract class UnixHostPlatform implements IHostPlatform {
8411fd8dDaniel Lebu10 years ago59public getUserHomePath(): string {
60return process.env.HOME;
61}
62
bbdbbb3bdlebu10 years ago63public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>;
8411fd8dDaniel Lebu10 years ago64
65public getSettingsHome(): string {
66return path.join(process.env.HOME, ".vscode-react-native");
67}
68
a9955be6dlebu10 years ago69public getNpmCliCommand(packageName: string): string {
bbdbbb3bdlebu10 years ago70return packageName;
8411fd8dDaniel Lebu10 years ago71}
72
73public getExtensionPipePath(): string {
74return "/tmp/vscodereactnative.sock";
75}
76
bbdbbb3bdlebu10 years ago77public abstract getPlatformId(): HostPlatformId;
8411fd8dDaniel Lebu10 years ago78}
79
bbdbbb3bdlebu10 years ago80/**
81* IHostPlatform implemenation for the OSX platform.
82*/
83class OSXHostPlatform extends UnixHostPlatform {
84public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago85return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome;
bbdbbb3bdlebu10 years ago86}
87
88public getPlatformId(): HostPlatformId {
89return HostPlatformId.OSX;
90}
91}
92
93/**
94* IHostPlatform implemenation for the Linux platform.
95*/
96class LinuxHostPlatform extends UnixHostPlatform {
97public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago98return new ChildProcess().exec(`export ${name}=${value}`).outcome;
bbdbbb3bdlebu10 years ago99}
100
101public getPlatformId(): HostPlatformId {
102return HostPlatformId.LINUX;
103}
104}
105
8411fd8dDaniel Lebu10 years ago106/**
a1005420dlebu10 years ago107* Allows platform specific operations based on the user's OS.
8411fd8dDaniel Lebu10 years ago108*/
a1005420dlebu10 years ago109export class HostPlatform {
8411fd8dDaniel Lebu10 years ago110
a1005420dlebu10 years ago111private static platformInstance: IHostPlatform;
8411fd8dDaniel Lebu10 years ago112
113/**
114* Resolves the dev machine, desktop platform.
115*/
a1005420dlebu10 years ago116private static get platform(): IHostPlatform {
117if (!HostPlatform.platformInstance) {
118switch (process.platform) {
119case "win32":
120HostPlatform.platformInstance = new WindowsHostPlatform();
9fe4ca93digeff10 years ago121break;
a1005420dlebu10 years ago122case "darwin":
123HostPlatform.platformInstance = new OSXHostPlatform();
9fe4ca93digeff10 years ago124break;
a1005420dlebu10 years ago125case "linux":
c6d7ee27dlebu10 years ago126HostPlatform.platformInstance = new LinuxHostPlatform();
127break;
a1005420dlebu10 years ago128default:
129HostPlatform.platformInstance = new LinuxHostPlatform();
aab2095edigeff10 years ago130break;
a1005420dlebu10 years ago131}
8411fd8dDaniel Lebu10 years ago132}
a1005420dlebu10 years ago133
134return HostPlatform.platformInstance;
135}
136
137public static getUserHomePath(): string {
138return HostPlatform.platform.getUserHomePath();
139}
140
141public static getSettingsHome(): string {
142return HostPlatform.platform.getSettingsHome();
143}
144
145public static getNpmCliCommand(packageName: string): string {
a9955be6dlebu10 years ago146return HostPlatform.platform.getNpmCliCommand(packageName);
a1005420dlebu10 years ago147}
148
149public static getExtensionPipePath(): string {
150return HostPlatform.platform.getExtensionPipePath();
151}
152
153public static getPlatformId(): HostPlatformId {
154return HostPlatform.platform.getPlatformId();
155}
156
157public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> {
158return HostPlatform.platform.setEnvironmentVariable(name, value);
8411fd8dDaniel Lebu10 years ago159}
160}