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