microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
140lines · 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 {FileSystem} from "../common/node/fileSystem"; |
| 5 | import { logger } from "vscode-chrome-debug-core"; |
| 6 | import { ensurePackagerRunning } from "../common/packagerStatus"; |
| 7 | import path = require("path"); |
| 8 | import Q = require("q"); |
| 9 | import {Request} from "../common/node/request"; |
| 10 | import {SourceMapUtil} from "./sourceMap"; |
| 11 | import url = require("url"); |
| 12 | import * as semver from "semver"; |
| 13 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; |
| 14 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 15 | import { InternalErrorCode } from "../common/error/internalErrorCode"; |
| 16 | |
| 17 | export interface DownloadedScript { |
| 18 | contents: string; |
| 19 | filepath: string; |
| 20 | } |
| 21 | |
| 22 | interface IStrictUrl extends url.Url { |
| 23 | pathname: string; |
| 24 | href: string; |
| 25 | } |
| 26 | |
| 27 | export class ScriptImporter { |
| 28 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 29 | public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js"; |
| 30 | private packagerAddress: string; |
| 31 | private packagerPort: number; |
| 32 | private sourcesStoragePath: string; |
| 33 | private packagerRemoteRoot?: string; |
| 34 | private packagerLocalRoot?: string; |
| 35 | private sourceMapUtil: SourceMapUtil; |
| 36 | |
| 37 | constructor(packagerAddress: string, packagerPort: number, sourcesStoragePath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) { |
| 38 | this.packagerAddress = packagerAddress; |
| 39 | this.packagerPort = packagerPort; |
| 40 | this.sourcesStoragePath = sourcesStoragePath; |
| 41 | this.packagerRemoteRoot = packagerRemoteRoot; |
| 42 | this.packagerLocalRoot = packagerLocalRoot; |
| 43 | this.sourceMapUtil = new SourceMapUtil(); |
| 44 | } |
| 45 | |
| 46 | public downloadAppScript(scriptUrlString: string, projectRootPath: string): Q.Promise<DownloadedScript> { |
| 47 | const parsedScriptUrl = url.parse(scriptUrlString); |
| 48 | const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString; |
| 49 | // We'll get the source code, and store it locally to have a better debugging experience |
| 50 | return Request.request(overriddenScriptUrlString, true).then(scriptBody => { |
| 51 | return ReactNativeProjectHelper.getReactNativeVersion(projectRootPath).then(rnVersion => { |
| 52 | // unfortunatelly Metro Bundler is broken in RN 0.54.x versions, so use this workaround unless it will be fixed |
| 53 | // https://github.com/facebook/metro/issues/147 |
| 54 | // https://github.com/Microsoft/vscode-react-native/issues/660 |
| 55 | if (ReactNativeProjectHelper.getRNVersionsWithBrokenMetroBundler().indexOf(rnVersion) >= 0) { |
| 56 | let noSourceMappingUrlGenerated = scriptBody.match(/sourceMappingURL=/g) === null; |
| 57 | if (noSourceMappingUrlGenerated) { |
| 58 | let sourceMapPathUrl = overriddenScriptUrlString.replace("bundle", "map"); |
| 59 | scriptBody = this.sourceMapUtil.appendSourceMapPaths(scriptBody, sourceMapPathUrl); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Extract sourceMappingURL from body |
| 64 | let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" |
| 65 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
| 66 | |
| 67 | let waitForSourceMapping = Q<void>(void 0); |
| 68 | if (sourceMappingUrl) { |
| 69 | /* handle source map - request it and store it locally */ |
| 70 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl) |
| 71 | .then(() => { |
| 72 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, <IStrictUrl>sourceMappingUrl); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | return waitForSourceMapping |
| 77 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
| 78 | .then((scriptFilePath: string) => { |
| 79 | logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`); |
| 80 | return { contents: scriptBody, filepath: scriptFilePath }; |
| 81 | }); |
| 82 | }); |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | public downloadDebuggerWorker(sourcesStoragePath: string, projectRootPath: string): Q.Promise<void> { |
| 87 | const errPackagerNotRunning = ErrorHelper.getInternalError(InternalErrorCode.CannotAttachToPackagerCheckPackagerRunningOnPort, this.packagerPort); |
| 88 | return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning) |
| 89 | .then(() => { |
| 90 | return ReactNativeProjectHelper.getReactNativeVersion(projectRootPath); |
| 91 | }) |
| 92 | .then((rnVersion: string) => { |
| 93 | let newPackager = ""; |
| 94 | const isHaulProject = ReactNativeProjectHelper.isHaulProject(projectRootPath); |
| 95 | if (!semver.valid(rnVersion) /*Custom RN implementations should support new packager*/ || (semver.gte(rnVersion, "0.50.0") && !isHaulProject)) { |
| 96 | newPackager = "debugger-ui/"; |
| 97 | } |
| 98 | let debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${newPackager}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; |
| 99 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); |
| 100 | logger.verbose("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); |
| 101 | |
| 102 | return Request.request(debuggerWorkerURL, true) |
| 103 | .then((body: string) => { |
| 104 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 105 | }); |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Writes the script file to the project temporary location. |
| 111 | */ |
| 112 | private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Q.Promise<String> { |
| 113 | let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 114 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 115 | .then(() => scriptFilePath); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Writes the source map file to the project temporary location. |
| 120 | */ |
| 121 | private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Q.Promise<void> { |
| 122 | return Request.request(sourceMapUrl.href, true) |
| 123 | .then((sourceMapBody: string) => { |
| 124 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 125 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
| 126 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot); |
| 127 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Changes the port of the url to be the one configured as this.packagerPort |
| 133 | */ |
| 134 | private overridePackagerPort(urlToOverride: string): string { |
| 135 | let components = url.parse(urlToOverride); |
| 136 | components.port = this.packagerPort.toString(); |
| 137 | delete components.host; // We delete the host, if not the port change will be ignored |
| 138 | return url.format(components); |
| 139 | } |
| 140 | } |
| 141 | |