microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/generalMobilePlatform.ts
173lines · 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 | |
| 4 | import * as Q from "q"; |
| 5 | import * as fs from "fs"; |
| 6 | |
| 7 | import {IRunOptions} from "./launchArgs"; |
| 8 | import {Packager} from "../common/packager"; |
| 9 | import {PackagerStatusIndicator, PackagerStatus} from "./packagerStatusIndicator"; |
| 10 | import {SettingsHelper} from "./settingsHelper"; |
| 11 | import {OutputChannelLogger} from "./log/OutputChannelLogger"; |
| 12 | import * as nls from "vscode-nls"; |
| 13 | const localize = nls.loadMessageBundle(); |
| 14 | |
| 15 | export interface MobilePlatformDeps { |
| 16 | packager?: Packager; |
| 17 | } |
| 18 | |
| 19 | export type TargetType = "device" | "simulator"; |
| 20 | |
| 21 | export 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 | 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 getEnvArgument(): any { |
| 136 | let args = this.runOptions; |
| 137 | let env = process.env; |
| 138 | |
| 139 | if (args.envFile) { |
| 140 | let buffer = fs.readFileSync(args.envFile, "utf8"); |
| 141 | |
| 142 | // Strip BOM |
| 143 | if (buffer && buffer[0] === "\uFEFF") { |
| 144 | buffer = buffer.substr(1); |
| 145 | } |
| 146 | |
| 147 | buffer.split("\n").forEach((line: string) => { |
| 148 | const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/); |
| 149 | if (r !== null) { |
| 150 | const key = r[1]; |
| 151 | if (!env[key]) { // .env variables never overwrite existing variables |
| 152 | let value = r[2] || ""; |
| 153 | if (value.length > 0 && value.charAt(0) === "\"" && value.charAt(value.length - 1) === "\"") { |
| 154 | value = value.replace(/\\n/gm, "\n"); |
| 155 | } |
| 156 | env[key] = value.replace(/(^['"]|['"]$)/g, ""); |
| 157 | } |
| 158 | } |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | if (args.env) { |
| 163 | // launch config env vars overwrite .env vars |
| 164 | for (let key in args.env) { |
| 165 | if (args.env.hasOwnProperty(key)) { |
| 166 | env[key] = args.env[key]; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return env; |
| 172 | } |
| 173 | } |
| 174 | |