microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c036b0d343245c82886cd5906e37ff50b8b30d34

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

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