microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

159lines · 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.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(): Q.Promise<void> {
43 this.logger.info(localize("ConnectedToPackager", "Connected to packager. You can now open your app in the simulator."));
44 return Q.resolve<void>(void 0);
45 }
46
47 public enableJSDebuggingMode(): Q.Promise<void> {
48 this.logger.info(localize("DebuggerReadyEnableRemoteDebuggingInApp", "Debugger ready. Enable remote debugging in app."));
49 return Q.resolve<void>(void 0);
50 }
51
52 public disableJSDebuggingMode(): Q.Promise<void> {
53 this.logger.info(localize("DebuggerReadyDisableRemoteDebuggingInApp", "Debugger ready. Disable remote debugging in app."));
54 return Q.resolve<void>(void 0);
55 }
56
57 public beforeStartPackager(): Q.Promise<void> {
58 return Q.resolve<void>(void 0);
59 }
60
61 public startPackager(): Q.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(): Q.Promise<void> {
80 // generalMobilePlatform should do nothing here. Method should be overriden by children for specific behavior.
81 return Q.resolve<void>(void 0);
82 }
83
84 protected getOptFromRunArgs(optName: string, binary: boolean = false): any {
85 if (this.runArguments.length > 0) {
86 const optIdx = this.runArguments.indexOf(optName);
87 let result: any = false;
88
89 if (optIdx > -1) {
90 result = binary ? true : this.runArguments[optIdx + 1];
91 } else {
92 for (let i = 0; i < this.runArguments.length; i++) {
93 const arg = this.runArguments[i];
94 if (arg.indexOf(optName) > -1) {
95 result = binary ? true : arg.split("=")[1].trim();
96 }
97 }
98 }
99
100 if (binary) {
101 return !!result;
102 }
103
104 if (result) {
105 try {
106 return JSON.parse(result);
107 } catch (err) {
108 // sipmle string value, return as is
109 return result;
110 }
111 }
112 }
113
114 return undefined;
115 }
116
117 public getRunArguments(): string[] {
118 throw new Error("Not yet implemented: GeneralMobilePlatform.getRunArguments");
119 }
120
121 public getEnvArgument(): any {
122 let args = this.runOptions;
123 let env = process.env;
124
125 if (args.envFile) {
126 let buffer = fs.readFileSync(args.envFile, "utf8");
127
128 // Strip BOM
129 if (buffer && buffer[0] === "\uFEFF") {
130 buffer = buffer.substr(1);
131 }
132
133 buffer.split("\n").forEach((line: string) => {
134 const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
135 if (r !== null) {
136 const key = r[1];
137 if (!env[key]) { // .env variables never overwrite existing variables
138 let value = r[2] || "";
139 if (value.length > 0 && value.charAt(0) === "\"" && value.charAt(value.length - 1) === "\"") {
140 value = value.replace(/\\n/gm, "\n");
141 }
142 env[key] = value.replace(/(^['"]|['"]$)/g, "");
143 }
144 }
145 });
146 }
147
148 if (args.env) {
149 // launch config env vars overwrite .env vars
150 for (let key in args.env) {
151 if (args.env.hasOwnProperty(key)) {
152 env[key] = args.env[key];
153 }
154 }
155 }
156
157 return env;
158 }
159}
160