microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.15

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

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