microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9fc07913967868b1545f83c005e792a1778bce4b

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

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