microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/packager.ts
75lines · modecode
| 1 | import {Request} from "../utils/node/request"; |
| 2 | import {StopWatch} from "../utils/node/stopWatch"; |
| 3 | import {CommandExecutor} from "../utils/commands/commandExecutor"; |
| 4 | import {Log} from "../utils/commands/log"; |
| 5 | import * as Q from "q"; |
| 6 | import * as _ from "lodash"; |
| 7 | |
| 8 | export class Packager { |
| 9 | private projectPath: string; |
| 10 | private packagerOptions = this.getPackagerOptions(); |
| 11 | |
| 12 | constructor(projectPath: string) { |
| 13 | this.projectPath = projectPath; |
| 14 | } |
| 15 | |
| 16 | private status(): Q.Promise<string> { |
| 17 | return new Request().request("http://localhost:8081/status").then( |
| 18 | (body: string) => { return body === "packager-status:running" ? "running" : "unrecognized"; }, |
| 19 | (error: any) => { return "not_running"; }); |
| 20 | } |
| 21 | |
| 22 | private awaitUntilRunning(callback: () => void, millisecondsUntilRetry: number): void { |
| 23 | this.status().done(status => { |
| 24 | if (status === "running") { |
| 25 | callback(); |
| 26 | } else { |
| 27 | setTimeout(() => this.awaitUntilRunning(callback, millisecondsUntilRetry), millisecondsUntilRetry); |
| 28 | } |
| 29 | }, reason => { |
| 30 | setTimeout(() => this.awaitUntilRunning(callback, millisecondsUntilRetry), millisecondsUntilRetry); |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | private awaitStart(millisecondsUntilRetry: number = 1000): Q.Promise<number> { |
| 35 | let result = Q.defer<number>(); |
| 36 | let stopWatch = new StopWatch(); |
| 37 | this.awaitUntilRunning(() => result.resolve(stopWatch.stopAsSeconds()), millisecondsUntilRetry); |
| 38 | return result.promise; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | // TODO: Remove either the old or the new createStrategy version |
| 43 | /* tslint:disable:no-unused-variable */ |
| 44 | private getPackagerOptions(): IPackagerOptions { |
| 45 | let platform = process.platform; |
| 46 | switch (platform) { |
| 47 | case "darwin": |
| 48 | return { executableName: "react-native", packagerStartExtraParameters: []}; |
| 49 | case "win32": |
| 50 | default: |
| 51 | return { executableName: "react-native.cmd", packagerStartExtraParameters: ["--nonPersistent"]}; |
| 52 | } |
| 53 | } |
| 54 | /* tslint:enable:no-unused-variable */ |
| 55 | |
| 56 | public start(): Q.Promise<number> { |
| 57 | |
| 58 | this.status().done(status => { |
| 59 | if (status !== "running") { |
| 60 | let mandatoryArgs = ["start"]; |
| 61 | let args = mandatoryArgs.concat(this.packagerOptions.packagerStartExtraParameters); |
| 62 | let childEnv = _.extend({}, process.env, { REACT_DEBUGGER: "echo A debugger is not needed: " }); |
| 63 | |
| 64 | // The packager will continue running while we debug the application, so we can"t |
| 65 | // wait for this command to finish |
| 66 | new CommandExecutor(this.projectPath).spawn("Packager", this.packagerOptions.executableName, args, { env: childEnv }).done(); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | return this.awaitStart().then(timeToStart => { |
| 71 | Log.logMessage("Packager was started after " + timeToStart + " secs"); |
| 72 | return timeToStart; |
| 73 | }); |
| 74 | } |
| 75 | } |