microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/packager.ts
103lines · 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 {Node} from "../utils/node/node"; |
| 8 | import {OutputChannel} from "vscode"; |
| 9 | import {PromiseUtil} from "../utils/node/promise"; |
| 10 | import {Request} from "../utils/node/request"; |
| 11 | |
| 12 | import * as Q from "q"; |
| 13 | import * as path from "path"; |
| 14 | |
| 15 | export class Packager { |
| 16 | // TODO: Make the port configurable via a launch argument |
| 17 | public static PORT = "8081"; |
| 18 | public static HOST = `localhost:${Packager.PORT}`; |
| 19 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 20 | public static DEBUGGER_WORKER_FILENAME = Packager.DEBUGGER_WORKER_FILE_BASENAME + ".js"; |
| 21 | private projectPath: string; |
| 22 | private packagerProcess: ChildProcess; |
| 23 | private sourcesStoragePath: string; |
| 24 | |
| 25 | constructor(projectPath: string, sourcesStoragePath?: string) { |
| 26 | this.projectPath = projectPath; |
| 27 | this.sourcesStoragePath = sourcesStoragePath; |
| 28 | } |
| 29 | |
| 30 | private isRunning(): Q.Promise<boolean> { |
| 31 | let statusURL = `http://${Packager.HOST}/status`; |
| 32 | |
| 33 | return new Request().request(statusURL) |
| 34 | .then((body: string) => { |
| 35 | return body === "packager-status:running"; |
| 36 | }, |
| 37 | (error: any) => { |
| 38 | return false; |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | private awaitStart(retryCount = 30, delay = 2000): Q.Promise<boolean> { |
| 43 | let pu: PromiseUtil = new PromiseUtil(); |
| 44 | return pu.retryAsync(() => this.isRunning(), (running) => running, retryCount, delay, "Could not start the packager."); |
| 45 | } |
| 46 | |
| 47 | private downloadDebuggerWorker(): Q.Promise<void> { |
| 48 | let debuggerWorkerURL = `http://${Packager.HOST}/${Packager.DEBUGGER_WORKER_FILENAME}`; |
| 49 | let debuggerWorkerLocalPath = path.join(this.sourcesStoragePath, Packager.DEBUGGER_WORKER_FILENAME); |
| 50 | Log.logInternalMessage("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); |
| 51 | return new Request().request(debuggerWorkerURL, true).then((body: string) => { |
| 52 | return new Node.FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | public start(outputChannel?: OutputChannel): Q.Promise<void> { |
| 57 | this.isRunning().done(running => { |
| 58 | if (!running) { |
| 59 | let args = ["--port", Packager.PORT]; |
| 60 | let childEnvForDebugging = Object.assign({}, process.env, { REACT_DEBUGGER: "echo A debugger is not needed: " }); |
| 61 | |
| 62 | Log.logMessage("Starting Packager", outputChannel); |
| 63 | // The packager will continue running while we debug the application, so we can"t |
| 64 | // wait for this command to finish |
| 65 | |
| 66 | let spawnOptions = { env: childEnvForDebugging }; |
| 67 | |
| 68 | new CommandExecutor(this.projectPath).spawnReactCommand("start", args, spawnOptions, outputChannel).then((packagerProcess) => { |
| 69 | this.packagerProcess = packagerProcess; |
| 70 | }).done(); |
| 71 | } |
| 72 | }); |
| 73 | |
| 74 | return this.awaitStart().then(() => { |
| 75 | Log.logMessage("Packager started.", outputChannel); |
| 76 | if (this.sourcesStoragePath) { |
| 77 | return this.downloadDebuggerWorker().then(() => { |
| 78 | Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager."); |
| 79 | }); |
| 80 | } |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | public stop(outputChannel?: OutputChannel): void { |
| 85 | Log.logMessage("Stopping Packager", outputChannel); |
| 86 | |
| 87 | if (this.packagerProcess) { |
| 88 | this.packagerProcess.kill(); |
| 89 | this.packagerProcess = null; |
| 90 | Log.logMessage("Packager stopped", outputChannel); |
| 91 | } else { |
| 92 | Log.logMessage("Packager not found", outputChannel); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public prewarmBundleCache(platform: string) { |
| 97 | let bundleURL = `http://${Packager.HOST}/index.${platform}.bundle`; |
| 98 | Log.logInternalMessage("About to get: " + bundleURL); |
| 99 | return new Request().request(bundleURL, true).then(() => { |
| 100 | Log.logMessage("The Bundle Cache was prewarmed."); |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | |