microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
legacy

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/generalMobilePlatform.ts

189lines · modeblame

0a68f8dbArtem Egorov8 years ago1// 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";
e26a1f43Artem Egorov8 years ago5import * as fs from "fs";
0a68f8dbArtem Egorov8 years ago6
7import {IRunOptions} from "./launchArgs";
4787ec09Artem Egorov8 years ago8import {Packager} from "../common/packager";
9import {PackagerStatusIndicator, PackagerStatus} from "./packagerStatusIndicator";
0a68f8dbArtem Egorov8 years ago10import {SettingsHelper} from "./settingsHelper";
11import {OutputChannelLogger} from "./log/OutputChannelLogger";
d7d405aeYuri Skorokhodov7 years ago12import * as nls from "vscode-nls";
13const localize = nls.loadMessageBundle();
0a68f8dbArtem Egorov8 years ago14
15export interface MobilePlatformDeps {
16packager?: Packager;
17}
18
19export type TargetType = "device" | "simulator";
20
21export class GeneralMobilePlatform {
22protected projectPath: string;
23protected platformName: string;
24protected packager: Packager;
25protected logger: OutputChannelLogger;
26
27protected static deviceString: TargetType = "device";
28protected static simulatorString: TargetType = "simulator";
df8c800dArtem Egorov8 years ago29protected static NO_PACKAGER_VERSION = "0.42.0";
0a68f8dbArtem Egorov8 years ago30
db6fd42aRuslan Bikkinin7 years ago31public runArguments: string[];
32
0a68f8dbArtem Egorov8 years ago33constructor(protected runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) {
34this.platformName = this.runOptions.platform;
35this.projectPath = this.runOptions.projectRoot;
38b844ddJiglioNero5 years ago36this.packager = platformDeps.packager || new Packager(this.runOptions.workspaceRoot, this.projectPath, SettingsHelper.getPackagerPort(this.runOptions.workspaceRoot), new PackagerStatusIndicator(this.projectPath));
5f0a4a46JiglioNero6 years ago37this.packager.setRunOptions(runOptions);
aca27f7fYuri Skorokhodov7 years ago38this.logger = OutputChannelLogger.getChannel(localize("ReactNativeRunPlatform", "React Native: Run {0}", this.platformName), true);
0a68f8dbArtem Egorov8 years ago39this.logger.clear();
db6fd42aRuslan Bikkinin7 years ago40this.runArguments = this.getRunArguments();
0a68f8dbArtem Egorov8 years ago41}
42
43public runApp(): Q.Promise<void> {
d7d405aeYuri Skorokhodov7 years ago44this.logger.info(localize("ConnectedToPackager", "Connected to packager. You can now open your app in the simulator."));
0a68f8dbArtem Egorov8 years ago45return Q.resolve<void>(void 0);
46}
47
48public enableJSDebuggingMode(): Q.Promise<void> {
d7d405aeYuri Skorokhodov7 years ago49this.logger.info(localize("DebuggerReadyEnableRemoteDebuggingInApp", "Debugger ready. Enable remote debugging in app."));
0a68f8dbArtem Egorov8 years ago50return Q.resolve<void>(void 0);
51}
52
53public disableJSDebuggingMode(): Q.Promise<void> {
d7d405aeYuri Skorokhodov7 years ago54this.logger.info(localize("DebuggerReadyDisableRemoteDebuggingInApp", "Debugger ready. Disable remote debugging in app."));
0a68f8dbArtem Egorov8 years ago55return Q.resolve<void>(void 0);
56}
57
4787ec09Artem Egorov8 years ago58public beforeStartPackager(): Q.Promise<void> {
59return Q.resolve<void>(void 0);
60}
61
0a68f8dbArtem Egorov8 years ago62public startPackager(): Q.Promise<void> {
d7d405aeYuri Skorokhodov7 years ago63this.logger.info(localize("StartingReactNativePackager", "Starting React Native Packager."));
4787ec09Artem Egorov8 years ago64return this.packager.isRunning()
65.then((running) => {
0a68f8dbArtem Egorov8 years ago66if (running) {
4787ec09Artem Egorov8 years ago67if (this.packager.getPackagerStatus() !== PackagerStatus.PACKAGER_STARTED) {
68return this.packager.stop();
0a68f8dbArtem Egorov8 years ago69}
70
d7d405aeYuri Skorokhodov7 years ago71this.logger.info(localize("AttachingToRunningReactNativePackager", "Attaching to running React Native packager"));
0a68f8dbArtem Egorov8 years ago72}
73return void 0;
74})
4787ec09Artem Egorov8 years ago75.then(() => {
76return this.packager.start();
77});
0a68f8dbArtem Egorov8 years ago78}
79
80public prewarmBundleCache(): Q.Promise<void> {
81// generalMobilePlatform should do nothing here. Method should be overriden by children for specific behavior.
82return Q.resolve<void>(void 0);
83}
84
116c3cb0Ruslan Bikkinin7 years ago85public static getOptFromRunArgs(runArguments: any[], optName: string, binary: boolean = false): any {
86if (runArguments.length > 0) {
87const optIdx = runArguments.indexOf(optName);
88let result: any = undefined;
db6fd42aRuslan Bikkinin7 years ago89
90if (optIdx > -1) {
116c3cb0Ruslan Bikkinin7 years ago91result = binary ? true : runArguments[optIdx + 1];
db6fd42aRuslan Bikkinin7 years ago92} else {
116c3cb0Ruslan Bikkinin7 years ago93for (let i = 0; i < runArguments.length; i++) {
94const arg = runArguments[i];
db6fd42aRuslan Bikkinin7 years ago95if (arg.indexOf(optName) > -1) {
116c3cb0Ruslan Bikkinin7 years ago96if (binary) {
97result = true;
98} else {
99const tokens = arg.split("=");
100if (tokens.length > 1) {
101result = tokens[1].trim();
102} else {
103result = undefined;
104}
105}
db6fd42aRuslan Bikkinin7 years ago106}
107}
108}
109
116c3cb0Ruslan Bikkinin7 years ago110// Binary parameters can either exists (e.g. be true) or be absent. You can not pass false binary parameter.
db6fd42aRuslan Bikkinin7 years ago111if (binary) {
116c3cb0Ruslan Bikkinin7 years ago112if (result === undefined) {
113return undefined;
114} else {
115return true;
116}
db6fd42aRuslan Bikkinin7 years ago117}
118
119if (result) {
120try {
121return JSON.parse(result);
122} catch (err) {
116c3cb0Ruslan Bikkinin7 years ago123// simple string value, return as is
db6fd42aRuslan Bikkinin7 years ago124return result;
125}
126}
127}
128
129return undefined;
130}
131
cbc7ac5bArtem Egorov7 years ago132public getRunArguments(): string[] {
db6fd42aRuslan Bikkinin7 years ago133throw new Error("Not yet implemented: GeneralMobilePlatform.getRunArguments");
134}
135
5f0a4a46JiglioNero6 years ago136public static getEnvArgument(processEnv: any, env?: any, envFile?: string): any {
137let modifyEnv = Object.assign({}, processEnv);
e26a1f43Artem Egorov8 years ago138
5f0a4a46JiglioNero6 years ago139if (envFile) {
140// .env variables never overwrite existing variables
141const argsFromEnvFile = this.readEnvFile(envFile);
142if (argsFromEnvFile != null) {
143for (let key in argsFromEnvFile) {
144if (!modifyEnv[key] && argsFromEnvFile.hasOwnProperty(key)) {
145modifyEnv[key] = argsFromEnvFile[key];
146}
147}
148}
149}
150
151if (env) {
152// launch config env vars overwrite .env vars
153for (let key in env) {
154if (env.hasOwnProperty(key)) {
155modifyEnv[key] = env[key];
156}
157}
158}
159return modifyEnv;
160}
161
162private static readEnvFile(filePath: string): any {
163if (fs.existsSync(filePath)) {
164let buffer = fs.readFileSync(filePath, "utf8");
165let result = {};
e26a1f43Artem Egorov8 years ago166
167// Strip BOM
168if (buffer && buffer[0] === "\uFEFF") {
169buffer = buffer.substr(1);
170}
171
172buffer.split("\n").forEach((line: string) => {
173const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
174if (r !== null) {
175const key = r[1];
5f0a4a46JiglioNero6 years ago176let value = r[2] || "";
177if (value.length > 0 && value.charAt(0) === "\"" && value.charAt(value.length - 1) === "\"") {
178value = value.replace(/\\n/gm, "\n");
e26a1f43Artem Egorov8 years ago179}
5f0a4a46JiglioNero6 years ago180result[key] = value.replace(/(^['"]|['"]$)/g, "");
e26a1f43Artem Egorov8 years ago181}
182});
183
5f0a4a46JiglioNero6 years ago184return result;
185} else {
186return null;
e26a1f43Artem Egorov8 years ago187}
188}
0a68f8dbArtem Egorov8 years ago189}