microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/packager.ts
73lines · 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 {IDesktopPlatform} 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 {Node} from "../utils/node/node"; |
| 10 | import * as Q from "q"; |
| 11 | import * as path from "path"; |
| 12 | |
| 13 | export class Packager { |
| 14 | public static HOST = "localhost:8081"; |
| 15 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 16 | public static DEBUGGER_WORKER_FILENAME = Packager.DEBUGGER_WORKER_FILE_BASENAME + ".js"; |
| 17 | private projectPath: string; |
| 18 | private sourcesStoragePath: string; |
| 19 | private desktopPlatform: IDesktopPlatform; |
| 20 | |
| 21 | constructor(projectPath: string, desktopPlatform: IDesktopPlatform, sourcesStoragePath: string) { |
| 22 | this.projectPath = projectPath; |
| 23 | this.desktopPlatform = desktopPlatform; |
| 24 | this.sourcesStoragePath = sourcesStoragePath; |
| 25 | } |
| 26 | |
| 27 | private isRunning(): Q.Promise<boolean> { |
| 28 | let statusURL = `http://${Packager.HOST}/status`; |
| 29 | |
| 30 | return new Request().request(statusURL) |
| 31 | .then((body: string) => { |
| 32 | return body === "packager-status:running"; |
| 33 | }, |
| 34 | (error: any) => { |
| 35 | return false; |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | private awaitStart(retryCount = 30, delay = 2000): Q.Promise<boolean> { |
| 40 | let pu: PromiseUtil = new PromiseUtil(); |
| 41 | return pu.retryAsync(() => this.isRunning(), (running) => running, retryCount, delay, "Could not start the packager."); |
| 42 | } |
| 43 | |
| 44 | private downloadDebuggerWorker(): Q.Promise<void> { |
| 45 | let debuggerWorkerURL = `http://${Packager.HOST}/${Packager.DEBUGGER_WORKER_FILENAME}`; |
| 46 | let debuggerWorkerLocalPath = path.join(this.sourcesStoragePath, Packager.DEBUGGER_WORKER_FILENAME); |
| 47 | Log.logInternalMessage("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); |
| 48 | return new Request().request(debuggerWorkerURL, true).then((body: string) => { |
| 49 | return new Node.FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | public start(): Q.Promise<void> { |
| 54 | this.isRunning().done(running => { |
| 55 | if (!running) { |
| 56 | let mandatoryArgs = ["start"]; |
| 57 | let args = mandatoryArgs.concat(this.desktopPlatform.reactPackagerExtraParameters); |
| 58 | let childEnv = Object.assign({}, process.env, { REACT_DEBUGGER: "echo A debugger is not needed: " }); |
| 59 | |
| 60 | // The packager will continue running while we debug the application, so we can"t |
| 61 | // wait for this command to finish |
| 62 | new CommandExecutor(this.projectPath).spawn(this.desktopPlatform.reactNativeCommandName, args, { env: childEnv }).done(); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | return this.awaitStart().then(() => { |
| 67 | Log.logMessage("Packager started."); |
| 68 | return this.downloadDebuggerWorker(); |
| 69 | }).then(() => { |
| 70 | Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager."); |
| 71 | }); |
| 72 | } |
| 73 | } |
| 74 | |