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