microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/packager.ts
56lines · 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 {PlatformResolver} from "./platformResolver"; |
| 5 | import {PromiseUtil} from "../utils/node/promise"; |
| 6 | import {Request} from "../utils/node/request"; |
| 7 | import {CommandExecutor} from "../utils/commands/commandExecutor"; |
| 8 | import {Log} from "../utils/commands/log"; |
| 9 | import * as Q from "q"; |
| 10 | |
| 11 | export class Packager { |
| 12 | public static HOST = "localhost:8081"; |
| 13 | private projectPath: string; |
| 14 | |
| 15 | constructor(projectPath: string) { |
| 16 | this.projectPath = projectPath; |
| 17 | } |
| 18 | |
| 19 | private isRunning(): Q.Promise<boolean> { |
| 20 | let statusURL = `http://${Packager.HOST}/status`; |
| 21 | |
| 22 | return new Request().request(statusURL) |
| 23 | .then((body: string) => { |
| 24 | return body === "packager-status:running"; |
| 25 | }, |
| 26 | (error: any) => { |
| 27 | return false; |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | private awaitStart(retryCount = 30, delay = 2000): Q.Promise<boolean> { |
| 32 | let pu: PromiseUtil = new PromiseUtil(); |
| 33 | return pu.retryAsync(() => this.isRunning(), (running) => running, retryCount, delay, "Could not start the packager."); |
| 34 | } |
| 35 | |
| 36 | public start(): Q.Promise<void> { |
| 37 | let resolver = new PlatformResolver(); |
| 38 | let desktopPlatform = resolver.resolveDesktopPlatform(); |
| 39 | |
| 40 | this.isRunning().done(running => { |
| 41 | if (!running) { |
| 42 | let mandatoryArgs = ["start"]; |
| 43 | let args = mandatoryArgs.concat(desktopPlatform.reactPackagerExtraParameters); |
| 44 | let childEnv = Object.assign({}, process.env, { REACT_DEBUGGER: "echo A debugger is not needed: " }); |
| 45 | |
| 46 | // The packager will continue running while we debug the application, so we can"t |
| 47 | // wait for this command to finish |
| 48 | new CommandExecutor(this.projectPath).spawn(desktopPlatform.reactNativeCommandName, args, { env: childEnv }).done(); |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | return this.awaitStart().then(() => { |
| 53 | Log.logMessage("Packager started."); |
| 54 | }); |
| 55 | } |
| 56 | } |
| 57 | |