microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.15.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

189lines · 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 * as Q from "q";
5import * as fs from "fs";
6
7import {IRunOptions} from "./launchArgs";
8import {Packager} from "../common/packager";
9import {PackagerStatusIndicator, PackagerStatus} from "./packagerStatusIndicator";
10import {SettingsHelper} from "./settingsHelper";
11import {OutputChannelLogger} from "./log/OutputChannelLogger";
12import * as nls from "vscode-nls";
13const localize = nls.loadMessageBundle();
14
15export interface MobilePlatformDeps {
16 packager?: Packager;
17}
18
19export type TargetType = "device" | "simulator";
20
21export class GeneralMobilePlatform {
22 protected projectPath: string;
23 protected platformName: string;
24 protected packager: Packager;
25 protected logger: OutputChannelLogger;
26
27 protected static deviceString: TargetType = "device";
28 protected static simulatorString: TargetType = "simulator";
29 protected static NO_PACKAGER_VERSION = "0.42.0";
30
31 public runArguments: string[];
32
33 constructor(protected runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) {
34 this.platformName = this.runOptions.platform;
35 this.projectPath = this.runOptions.projectRoot;
36 this.packager = platformDeps.packager || new Packager(this.runOptions.workspaceRoot, this.projectPath, SettingsHelper.getPackagerPort(this.runOptions.workspaceRoot), new PackagerStatusIndicator());
37 this.packager.setRunOptions(runOptions);
38 this.logger = OutputChannelLogger.getChannel(localize("ReactNativeRunPlatform", "React Native: Run {0}", this.platformName), true);
39 this.logger.clear();
40 this.runArguments = this.getRunArguments();
41 }
42
43 public runApp(): Q.Promise<void> {
44 this.logger.info(localize("ConnectedToPackager", "Connected to packager. You can now open your app in the simulator."));
45 return Q.resolve<void>(void 0);
46 }
47
48 public enableJSDebuggingMode(): Q.Promise<void> {
49 this.logger.info(localize("DebuggerReadyEnableRemoteDebuggingInApp", "Debugger ready. Enable remote debugging in app."));
50 return Q.resolve<void>(void 0);
51 }
52
53 public disableJSDebuggingMode(): Q.Promise<void> {
54 this.logger.info(localize("DebuggerReadyDisableRemoteDebuggingInApp", "Debugger ready. Disable remote debugging in app."));
55 return Q.resolve<void>(void 0);
56 }
57
58 public beforeStartPackager(): Q.Promise<void> {
59 return Q.resolve<void>(void 0);
60 }
61
62 public startPackager(): Q.Promise<void> {
63 this.logger.info(localize("StartingReactNativePackager", "Starting React Native Packager."));
64 return this.packager.isRunning()
65 .then((running) => {
66 if (running) {
67 if (this.packager.getPackagerStatus() !== PackagerStatus.PACKAGER_STARTED) {
68 return this.packager.stop();
69 }
70
71 this.logger.info(localize("AttachingToRunningReactNativePackager", "Attaching to running React Native packager"));
72 }
73 return void 0;
74 })
75 .then(() => {
76 return this.packager.start();
77 });
78 }
79
80 public prewarmBundleCache(): Q.Promise<void> {
81 // generalMobilePlatform should do nothing here. Method should be overriden by children for specific behavior.
82 return Q.resolve<void>(void 0);
83 }
84
85 public static getOptFromRunArgs(runArguments: any[], optName: string, binary: boolean = false): any {
86 if (runArguments.length > 0) {
87 const optIdx = runArguments.indexOf(optName);
88 let result: any = undefined;
89
90 if (optIdx > -1) {
91 result = binary ? true : runArguments[optIdx + 1];
92 } else {
93 for (let i = 0; i < runArguments.length; i++) {
94 const arg = runArguments[i];
95 if (arg.indexOf(optName) > -1) {
96 if (binary) {
97 result = true;
98 } else {
99 const tokens = arg.split("=");
100 if (tokens.length > 1) {
101 result = tokens[1].trim();
102 } else {
103 result = undefined;
104 }
105 }
106 }
107 }
108 }
109
110 // Binary parameters can either exists (e.g. be true) or be absent. You can not pass false binary parameter.
111 if (binary) {
112 if (result === undefined) {
113 return undefined;
114 } else {
115 return true;
116 }
117 }
118
119 if (result) {
120 try {
121 return JSON.parse(result);
122 } catch (err) {
123 // simple string value, return as is
124 return result;
125 }
126 }
127 }
128
129 return undefined;
130 }
131
132 public getRunArguments(): string[] {
133 throw new Error("Not yet implemented: GeneralMobilePlatform.getRunArguments");
134 }
135
136 public static getEnvArgument(processEnv: any, env?: any, envFile?: string): any {
137 let modifyEnv = Object.assign({}, processEnv);
138
139 if (envFile) {
140 // .env variables never overwrite existing variables
141 const argsFromEnvFile = this.readEnvFile(envFile);
142 if (argsFromEnvFile != null) {
143 for (let key in argsFromEnvFile) {
144 if (!modifyEnv[key] && argsFromEnvFile.hasOwnProperty(key)) {
145 modifyEnv[key] = argsFromEnvFile[key];
146 }
147 }
148 }
149 }
150
151 if (env) {
152 // launch config env vars overwrite .env vars
153 for (let key in env) {
154 if (env.hasOwnProperty(key)) {
155 modifyEnv[key] = env[key];
156 }
157 }
158 }
159 return modifyEnv;
160 }
161
162 private static readEnvFile(filePath: string): any {
163 if (fs.existsSync(filePath)) {
164 let buffer = fs.readFileSync(filePath, "utf8");
165 let result = {};
166
167 // Strip BOM
168 if (buffer && buffer[0] === "\uFEFF") {
169 buffer = buffer.substr(1);
170 }
171
172 buffer.split("\n").forEach((line: string) => {
173 const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
174 if (r !== null) {
175 const key = r[1];
176 let value = r[2] || "";
177 if (value.length > 0 && value.charAt(0) === "\"" && value.charAt(value.length - 1) === "\"") {
178 value = value.replace(/\\n/gm, "\n");
179 }
180 result[key] = value.replace(/(^['"]|['"]$)/g, "");
181 }
182 });
183
184 return result;
185 } else {
186 return null;
187 }
188 }
189}
190