microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.17

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

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