microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
102lines · 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"; |
190e393cMeena Kunnathur Balakrishnan10 years ago | 5 | import {Log} from "../common/log/log"; |
| 6 | import {LogLevel} from "../common/log/logHelper"; | |
87323739dlebu10 years ago | 7 | import {Packager} from "../common/packager"; |
3fb37ad5unknown10 years ago | 8 | import path = require("path"); |
| 9 | import Q = require("q"); | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 10 | import {Request} from "../common/node/request"; |
| 11 | import {SourceMapUtil} from "./sourceMap"; | |
3fb37ad5unknown10 years ago | 12 | import url = require("url"); |
| 13 | | |
4677921cdigeff10 years ago | 14 | interface DownloadedScript { |
| 15 | contents: string; | |
| 16 | filepath: string; | |
| 17 | } | |
| 18 | | |
3fb37ad5unknown10 years ago | 19 | export class ScriptImporter { |
2743f19cdlebu10 years ago | 20 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 21 | public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js"; | |
e3ae4227digeff10 years ago | 22 | private packagerPort: number; |
4677921cdigeff10 years ago | 23 | private sourcesStoragePath: string; |
4bd0c669dlebu10 years ago | 24 | private sourceMapUtil: SourceMapUtil; |
3fb37ad5unknown10 years ago | 25 | |
e3ae4227digeff10 years ago | 26 | constructor(packagerPort: number, sourcesStoragePath: string) { |
| 27 | this.packagerPort = packagerPort; | |
4677921cdigeff10 years ago | 28 | this.sourcesStoragePath = sourcesStoragePath; |
4bd0c669dlebu10 years ago | 29 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 30 | } |
| 31 | | |
2743f19cdlebu10 years ago | 32 | public downloadAppScript(scriptUrlString: string, debugAdapterPort: number): Q.Promise<DownloadedScript> { |
5e651f3edigeff10 years ago | 33 | const overridenScriptUrlString = this.overridePackagerPort(scriptUrlString); |
| 34 | console.log("overriden " + overridenScriptUrlString); | |
e00f7325unknown10 years ago | 35 | // We'll get the source code, and store it locally to have a better debugging experience |
5e651f3edigeff10 years ago | 36 | return new Request().request(overridenScriptUrlString, true).then(scriptBody => { |
e00f7325unknown10 years ago | 37 | // Extract sourceMappingURL from body |
5e651f3edigeff10 years ago | 38 | let scriptUrl = url.parse(overridenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" |
4bd0c669dlebu10 years ago | 39 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
e00f7325unknown10 years ago | 40 | |
4677921cdigeff10 years ago | 41 | let waitForSourceMapping = Q<void>(null); |
e00f7325unknown10 years ago | 42 | if (sourceMappingUrl) { |
| 43 | /* handle source map - request it and store it locally */ | |
2743f19cdlebu10 years ago | 44 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl) |
3af9a124unknown10 years ago | 45 | .then(() => { |
4bd0c669dlebu10 years ago | 46 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl); |
4677921cdigeff10 years ago | 47 | }); |
e00f7325unknown10 years ago | 48 | } |
4677921cdigeff10 years ago | 49 | |
| 50 | return waitForSourceMapping | |
2743f19cdlebu10 years ago | 51 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
4677921cdigeff10 years ago | 52 | .then((scriptFilePath: string) => { |
5e651f3edigeff10 years ago | 53 | Log.logInternalMessage(LogLevel.Info, `Script ${overridenScriptUrlString} downloaded to ${scriptFilePath}`); |
4677921cdigeff10 years ago | 54 | return { contents: scriptBody, filepath: scriptFilePath }; |
87e1f776Jimmy Thomson10 years ago | 55 | }).finally(() => { |
| 56 | // Request that the debug adapter update breakpoints and sourcemaps now that we have written them | |
2743f19cdlebu10 years ago | 57 | return new Request().request(`http://localhost:${debugAdapterPort}/refreshBreakpoints`); |
876df2a0dlebu10 years ago | 58 | }); |
e00f7325unknown10 years ago | 59 | }); |
| 60 | } | |
| 61 | | |
2743f19cdlebu10 years ago | 62 | public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> { |
5e651f3edigeff10 years ago | 63 | let debuggerWorkerURL = `http://${Packager.getHostForPort(this.packagerPort)}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; |
2743f19cdlebu10 years ago | 64 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); |
| 65 | Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); | |
| 66 | return new Request().request(debuggerWorkerURL, true).then((body: string) => { | |
| 67 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); | |
| 68 | }); | |
| 69 | } | |
| 70 | | |
e00f7325unknown10 years ago | 71 | /** |
| 72 | * Writes the script file to the project temporary location. | |
| 73 | */ | |
2743f19cdlebu10 years ago | 74 | private writeAppScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> { |
f218397cdlebu10 years ago | 75 | let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 76 | return new FileSystem().writeFile(scriptFilePath, scriptBody) | |
| 77 | .then(() => scriptFilePath); | |
3fb37ad5unknown10 years ago | 78 | } |
| 79 | | |
e00f7325unknown10 years ago | 80 | /** |
| 81 | * Writes the source map file to the project temporary location. | |
| 82 | */ | |
2743f19cdlebu10 years ago | 83 | private writeAppSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> { |
e00f7325unknown10 years ago | 84 | return new Request().request(sourceMapUrl.href, true) |
| 85 | .then((sourceMapBody: string) => { | |
4677921cdigeff10 years ago | 86 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
e00f7325unknown10 years ago | 87 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
f218397cdlebu10 years ago | 88 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath); |
| 89 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); | |
e00f7325unknown10 years ago | 90 | }); |
3fb37ad5unknown10 years ago | 91 | } |
5e651f3edigeff10 years ago | 92 | |
| 93 | /** | |
| 94 | * Changes the port of the url to be the one configured as this.packagerPort | |
| 95 | */ | |
| 96 | private overridePackagerPort(urlToOverride: string): string { | |
| 97 | let components = url.parse(urlToOverride); | |
| 98 | components.port = this.packagerPort.toString(); | |
| 99 | delete components.host; // We delete the host, if not the port change will be ignored | |
| 100 | return url.format(components); | |
| 101 | } | |
3fb37ad5unknown10 years ago | 102 | } |