microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

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