microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/packager.ts
167lines · 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 "./commandExecutor"; |
| 6 | import {Log, LogLevel} from "./log"; |
| 7 | import {Node} from "./node/node"; |
| 8 | import {OutputChannel} from "vscode"; |
| 9 | import {Package} from "./node/package"; |
| 10 | import {PromiseUtil} from "./node/promise"; |
| 11 | import {Request} from "./node/request"; |
| 12 | |
| 13 | import * as Q from "q"; |
| 14 | import * as path from "path"; |
| 15 | |
| 16 | export class Packager { |
| 17 | // TODO: Make the port configurable via a launch argument |
| 18 | public static PORT = "8081"; |
| 19 | public static HOST = `localhost:${Packager.PORT}`; |
| 20 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 21 | public static DEBUGGER_WORKER_FILENAME = Packager.DEBUGGER_WORKER_FILE_BASENAME + ".js"; |
| 22 | |
| 23 | private projectPath: string; |
| 24 | private packagerProcess: ChildProcess; |
| 25 | private sourcesStoragePath: string; |
| 26 | |
| 27 | private static JS_INJECTOR_FILENAME = "opn-main.js"; |
| 28 | private static JS_INJECTOR_FILEPATH = path.resolve(path.dirname(path.dirname(__dirname)), "js-patched", Packager.JS_INJECTOR_FILENAME); |
| 29 | private static NODE_MODULES_FODLER_NAME = "node_modules"; |
| 30 | private static OPN_PACKAGE_NAME = "opn"; |
| 31 | private static REACT_NATIVE_PACKAGE_NAME = "react-native"; |
| 32 | private static OPN_PACKAGE_MAIN_FILENAME = "index.js"; |
| 33 | |
| 34 | constructor(projectPath: string, sourcesStoragePath?: string) { |
| 35 | this.projectPath = projectPath; |
| 36 | this.sourcesStoragePath = sourcesStoragePath; |
| 37 | } |
| 38 | |
| 39 | public start(outputChannel?: OutputChannel): Q.Promise<void> { |
| 40 | let executedStartPackagerCmd = false; |
| 41 | this.isRunning().done(running => { |
| 42 | if (!running) { |
| 43 | return this.monkeyPatchOpnForRNPackager() |
| 44 | .then(() => { |
| 45 | let args = ["--port", Packager.PORT]; |
| 46 | let childEnvForDebugging = Object.assign({}, process.env, { REACT_DEBUGGER: "echo A debugger is not needed: " }); |
| 47 | |
| 48 | Log.logMessage("Starting Packager", outputChannel); |
| 49 | // The packager will continue running while we debug the application, so we can"t |
| 50 | // wait for this command to finish |
| 51 | |
| 52 | let spawnOptions = { env: childEnvForDebugging }; |
| 53 | |
| 54 | new CommandExecutor(this.projectPath).spawnReactPackager(args, spawnOptions, outputChannel).then((packagerProcess) => { |
| 55 | this.packagerProcess = packagerProcess; |
| 56 | executedStartPackagerCmd = true; |
| 57 | }); |
| 58 | }).done(); |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | return this.awaitStart().then(() => { |
| 63 | if (executedStartPackagerCmd) { |
| 64 | Log.logMessage("Packager started.", outputChannel); |
| 65 | } else { |
| 66 | Log.logMessage("Packager is already running.", outputChannel); |
| 67 | if (!outputChannel) { |
| 68 | Log.logMessage("Warning: Debugging is not supported if the React Native Packager is not started within VS Code. If debugging fails, please kill other active React Native packager processes and retry.", outputChannel); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (this.sourcesStoragePath) { |
| 73 | return this.downloadDebuggerWorker().then(() => { |
| 74 | Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager."); |
| 75 | }); |
| 76 | } |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | public stop(outputChannel?: OutputChannel): void { |
| 81 | new CommandExecutor(this.projectPath).killReactPackager(this.packagerProcess, outputChannel); |
| 82 | this.packagerProcess = null; |
| 83 | } |
| 84 | |
| 85 | public prewarmBundleCache(platform: string) { |
| 86 | let bundleURL = `http://${Packager.HOST}/index.${platform}.bundle`; |
| 87 | Log.logInternalMessage(LogLevel.Info, "About to get: " + bundleURL); |
| 88 | return new Request().request(bundleURL, true).then(() => { |
| 89 | Log.logMessage("The Bundle Cache was prewarmed."); |
| 90 | }).catch(() => { |
| 91 | // The attempt to prefetch the bundle failed. |
| 92 | // This may be because the bundle is not index.* so we shouldn't treat this as fatal. |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | private isRunning(): Q.Promise<boolean> { |
| 97 | let statusURL = `http://${Packager.HOST}/status`; |
| 98 | |
| 99 | return new Request().request(statusURL) |
| 100 | .then((body: string) => { |
| 101 | return body === "packager-status:running"; |
| 102 | }, |
| 103 | (error: any) => { |
| 104 | return false; |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | private awaitStart(retryCount = 30, delay = 2000): Q.Promise<boolean> { |
| 109 | let pu: PromiseUtil = new PromiseUtil(); |
| 110 | return pu.retryAsync(() => this.isRunning(), (running) => running, retryCount, delay, "Could not start the packager."); |
| 111 | } |
| 112 | |
| 113 | private downloadDebuggerWorker(): Q.Promise<void> { |
| 114 | let debuggerWorkerURL = `http://${Packager.HOST}/${Packager.DEBUGGER_WORKER_FILENAME}`; |
| 115 | let debuggerWorkerLocalPath = path.join(this.sourcesStoragePath, Packager.DEBUGGER_WORKER_FILENAME); |
| 116 | Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); |
| 117 | return new Request().request(debuggerWorkerURL, true).then((body: string) => { |
| 118 | return new Node.FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | private findOpnPackage(): Q.Promise<string> { |
| 123 | try { |
| 124 | let flatDependencyPackagePath = path.resolve(this.projectPath, Packager.NODE_MODULES_FODLER_NAME, |
| 125 | Packager.OPN_PACKAGE_NAME, Packager.OPN_PACKAGE_MAIN_FILENAME); |
| 126 | |
| 127 | let nestedDependencyPackagePath = path.resolve(this.projectPath, Packager.NODE_MODULES_FODLER_NAME, |
| 128 | Packager.REACT_NATIVE_PACKAGE_NAME, Packager.NODE_MODULES_FODLER_NAME, Packager.OPN_PACKAGE_NAME, Packager.OPN_PACKAGE_MAIN_FILENAME); |
| 129 | |
| 130 | let fsHelper = new Node.FileSystem(); |
| 131 | |
| 132 | // Attempt to find the 'opn' package directly under the project's node_modules folder (node4 +) |
| 133 | // Else, attempt to find the package within the dependent node_modules of react-native package |
| 134 | let possiblePaths = [flatDependencyPackagePath, nestedDependencyPackagePath]; |
| 135 | return Q.any(possiblePaths.map(path => |
| 136 | fsHelper.exists(path).then(exists => |
| 137 | exists |
| 138 | ? Q.resolve(path) |
| 139 | : Q.reject<string>("opn package location not found")))); |
| 140 | } catch (err) { |
| 141 | console.error("The package \'opn\' was not found." + err); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private monkeyPatchOpnForRNPackager(): Q.Promise<void> { |
| 146 | let opnPackage: Package; |
| 147 | let destnFilePath: string; |
| 148 | |
| 149 | // Finds the 'opn' package |
| 150 | return this.findOpnPackage() |
| 151 | .then((opnIndexFilePath) => { |
| 152 | destnFilePath = opnIndexFilePath; |
| 153 | // Read the package's "package.json" |
| 154 | opnPackage = new Package(path.resolve(path.dirname(destnFilePath))); |
| 155 | return opnPackage.parsePackageInformation(); |
| 156 | }).then((packageJson) => { |
| 157 | if (packageJson.main !== Packager.JS_INJECTOR_FILENAME) { |
| 158 | // Copy over the patched 'opn' main file |
| 159 | return new Node.FileSystem().copyFile(Packager.JS_INJECTOR_FILEPATH, path.resolve(path.dirname(destnFilePath), Packager.JS_INJECTOR_FILENAME)) |
| 160 | .then(() => { |
| 161 | // Write/over-write the "main" attribute with the new file |
| 162 | return opnPackage.setMainFile(Packager.JS_INJECTOR_FILENAME); |
| 163 | }); |
| 164 | } |
| 165 | }); |
| 166 | } |
| 167 | } |
| 168 | |