microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
153lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 4 | import {FileSystem} from "../common/node/fileSystem"; |
0a68f8dbArtem Egorov8 years ago | 5 | import { logger } from "vscode-chrome-debug-core"; |
| 6 | import { ensurePackagerRunning } from "../common/packagerStatus"; | |
3fb37ad5unknown10 years ago | 7 | import path = require("path"); |
| 8 | import Q = require("q"); | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 9 | import {Request} from "../common/node/request"; |
| 10 | import {SourceMapUtil} from "./sourceMap"; | |
3fb37ad5unknown10 years ago | 11 | import url = require("url"); |
a1324704Artem Egorov8 years ago | 12 | import * as semver from "semver"; |
e3706a1cRedMickey6 years ago | 13 | import {ProjectVersionHelper, RNPackageVersions} from "../common/projectVersionHelper"; |
d124bf0eYuri Skorokhodov7 years ago | 14 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 15 | import { InternalErrorCode } from "../common/error/internalErrorCode"; | |
3fb37ad5unknown10 years ago | 16 | |
5c8365a6Artem Egorov8 years ago | 17 | export interface DownloadedScript { |
4677921cdigeff10 years ago | 18 | contents: string; |
| 19 | filepath: string; | |
| 20 | } | |
| 21 | | |
5c8365a6Artem Egorov8 years ago | 22 | interface IStrictUrl extends url.Url { |
| 23 | pathname: string; | |
| 24 | href: string; | |
| 25 | } | |
| 26 | | |
3fb37ad5unknown10 years ago | 27 | export class ScriptImporter { |
2743f19cdlebu10 years ago | 28 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 29 | public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js"; | |
6eeec3c0Serge Svekolnikov8 years ago | 30 | private packagerAddress: string; |
e3ae4227digeff10 years ago | 31 | private packagerPort: number; |
4677921cdigeff10 years ago | 32 | private sourcesStoragePath: string; |
6eeec3c0Serge Svekolnikov8 years ago | 33 | private packagerRemoteRoot?: string; |
| 34 | private packagerLocalRoot?: string; | |
4bd0c669dlebu10 years ago | 35 | private sourceMapUtil: SourceMapUtil; |
3fb37ad5unknown10 years ago | 36 | |
6eeec3c0Serge Svekolnikov8 years ago | 37 | constructor(packagerAddress: string, packagerPort: number, sourcesStoragePath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) { |
| 38 | this.packagerAddress = packagerAddress; | |
e3ae4227digeff10 years ago | 39 | this.packagerPort = packagerPort; |
4677921cdigeff10 years ago | 40 | this.sourcesStoragePath = sourcesStoragePath; |
6eeec3c0Serge Svekolnikov8 years ago | 41 | this.packagerRemoteRoot = packagerRemoteRoot; |
| 42 | this.packagerLocalRoot = packagerLocalRoot; | |
4bd0c669dlebu10 years ago | 43 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 44 | } |
| 45 | | |
979d7bfemax-mironov8 years ago | 46 | public downloadAppScript(scriptUrlString: string, projectRootPath: string): Q.Promise<DownloadedScript> { |
1c32fe84Patricio Beltran9 years ago | 47 | const parsedScriptUrl = url.parse(scriptUrlString); |
| 48 | const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString; | |
e00f7325unknown10 years ago | 49 | // We'll get the source code, and store it locally to have a better debugging experience |
833e37c7Vladimir Kotikov8 years ago | 50 | return Request.request(overriddenScriptUrlString, true).then(scriptBody => { |
e3706a1cRedMickey6 years ago | 51 | return ProjectVersionHelper.getReactNativeVersions(projectRootPath).then(rnVersions => { |
979d7bfemax-mironov8 years ago | 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 | |
f6fb2d2aFrieder Bluemle5 years ago | 54 | // https://github.com/microsoft/vscode-react-native/issues/660 |
e3706a1cRedMickey6 years ago | 55 | if (ProjectVersionHelper.getRNVersionsWithBrokenMetroBundler().indexOf(rnVersions.reactNativeVersion) >= 0) { |
979d7bfemax-mironov8 years ago | 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 | } | |
e00f7325unknown10 years ago | 62 | |
979d7bfemax-mironov8 years ago | 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" | |
4677921cdigeff10 years ago | 66 | |
979d7bfemax-mironov8 years ago | 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); | |
7fa90b3bRedMickey6 years ago | 73 | if (semver.gte(rnVersions.reactNativeVersion, "0.61.0")) { |
2f5c780bYuri Skorokhodov6 years ago | 74 | scriptBody = this.sourceMapUtil.removeSourceURL(scriptBody); |
| 75 | } | |
979d7bfemax-mironov8 years ago | 76 | }); |
| 77 | } | |
| 78 | | |
| 79 | return waitForSourceMapping | |
2743f19cdlebu10 years ago | 80 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
4677921cdigeff10 years ago | 81 | .then((scriptFilePath: string) => { |
0a68f8dbArtem Egorov8 years ago | 82 | logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`); |
4677921cdigeff10 years ago | 83 | return { contents: scriptBody, filepath: scriptFilePath }; |
876df2a0dlebu10 years ago | 84 | }); |
979d7bfemax-mironov8 years ago | 85 | }); |
e00f7325unknown10 years ago | 86 | }); |
| 87 | } | |
| 88 | | |
cf911877Yuri Skorokhodov7 years ago | 89 | public downloadDebuggerWorker(sourcesStoragePath: string, projectRootPath: string, debuggerWorkerUrlPath?: string): Q.Promise<void> { |
d124bf0eYuri Skorokhodov7 years ago | 90 | const errPackagerNotRunning = ErrorHelper.getInternalError(InternalErrorCode.CannotAttachToPackagerCheckPackagerRunningOnPort, this.packagerPort); |
6eeec3c0Serge Svekolnikov8 years ago | 91 | return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning) |
0a68f8dbArtem Egorov8 years ago | 92 | .then(() => { |
e3706a1cRedMickey6 years ago | 93 | return ProjectVersionHelper.getReactNativeVersions(projectRootPath); |
a1324704Artem Egorov8 years ago | 94 | }) |
7fa90b3bRedMickey6 years ago | 95 | .then((rnVersions: RNPackageVersions) => { |
| 96 | let debuggerWorkerURL = this.prepareDebuggerWorkerURL(rnVersions.reactNativeVersion, debuggerWorkerUrlPath); | |
0a68f8dbArtem Egorov8 years ago | 97 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); |
| 98 | logger.verbose("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); | |
| 99 | | |
| 100 | return Request.request(debuggerWorkerURL, true) | |
| 101 | .then((body: string) => { | |
299b0557Patricio Beltran10 years ago | 102 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 103 | }); | |
| 104 | }); | |
2743f19cdlebu10 years ago | 105 | } |
| 106 | | |
cf911877Yuri Skorokhodov7 years ago | 107 | public prepareDebuggerWorkerURL(rnVersion: string, debuggerWorkerUrlPath?: string): string { |
| 108 | let debuggerWorkerURL: string; | |
| 109 | // It can be empty string | |
| 110 | if (debuggerWorkerUrlPath !== undefined) { | |
| 111 | debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${debuggerWorkerUrlPath}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 112 | } else { | |
| 113 | let newPackager = ""; | |
| 114 | if (!semver.valid(rnVersion) /*Custom RN implementations should support new packager*/ || (semver.gte(rnVersion, "0.50.0"))) { | |
| 115 | newPackager = "debugger-ui/"; | |
| 116 | } | |
| 117 | debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${newPackager}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 118 | } | |
| 119 | return debuggerWorkerURL; | |
| 120 | } | |
| 121 | | |
e00f7325unknown10 years ago | 122 | /** |
| 123 | * Writes the script file to the project temporary location. | |
| 124 | */ | |
90d15117JiglioNero6 years ago | 125 | private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Q.Promise<string> { |
bb77358cMark Oswald10 years ago | 126 | let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
f218397cdlebu10 years ago | 127 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 128 | .then(() => scriptFilePath); | |
3fb37ad5unknown10 years ago | 129 | } |
| 130 | | |
e00f7325unknown10 years ago | 131 | /** |
| 132 | * Writes the source map file to the project temporary location. | |
| 133 | */ | |
5c8365a6Artem Egorov8 years ago | 134 | private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Q.Promise<void> { |
833e37c7Vladimir Kotikov8 years ago | 135 | return Request.request(sourceMapUrl.href, true) |
e00f7325unknown10 years ago | 136 | .then((sourceMapBody: string) => { |
bb77358cMark Oswald10 years ago | 137 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 138 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" | |
6eeec3c0Serge Svekolnikov8 years ago | 139 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot); |
f218397cdlebu10 years ago | 140 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
e00f7325unknown10 years ago | 141 | }); |
3fb37ad5unknown10 years ago | 142 | } |
5e651f3edigeff10 years ago | 143 | |
| 144 | /** | |
| 145 | * Changes the port of the url to be the one configured as this.packagerPort | |
| 146 | */ | |
| 147 | private overridePackagerPort(urlToOverride: string): string { | |
| 148 | let components = url.parse(urlToOverride); | |
| 149 | components.port = this.packagerPort.toString(); | |
| 150 | delete components.host; // We delete the host, if not the port change will be ignored | |
| 151 | return url.format(components); | |
| 152 | } | |
3fb37ad5unknown10 years ago | 153 | } |