microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
78lines · 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"; |
| 5 | import {Log, LogLevel} from "../common/log"; | |
3fb37ad5unknown10 years ago | 6 | import path = require("path"); |
| 7 | import Q = require("q"); | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 8 | import {Request} from "../common/node/request"; |
| 9 | import {SourceMapUtil} from "./sourceMap"; | |
3fb37ad5unknown10 years ago | 10 | import url = require("url"); |
| 11 | | |
4677921cdigeff10 years ago | 12 | interface DownloadedScript { |
| 13 | contents: string; | |
| 14 | filepath: string; | |
| 15 | } | |
| 16 | | |
3fb37ad5unknown10 years ago | 17 | export class ScriptImporter { |
4677921cdigeff10 years ago | 18 | private sourcesStoragePath: string; |
5547a16fJimmy Thomson10 years ago | 19 | private debugAdapterPort: number; |
4bd0c669dlebu10 years ago | 20 | private sourceMapUtil: SourceMapUtil; |
3fb37ad5unknown10 years ago | 21 | |
5547a16fJimmy Thomson10 years ago | 22 | constructor(sourcesStoragePath: string, debugAdapterPort: number) { |
4677921cdigeff10 years ago | 23 | this.sourcesStoragePath = sourcesStoragePath; |
5547a16fJimmy Thomson10 years ago | 24 | this.debugAdapterPort = debugAdapterPort; |
4bd0c669dlebu10 years ago | 25 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 26 | } |
| 27 | | |
4677921cdigeff10 years ago | 28 | public download(scriptUrlString: string): Q.Promise<DownloadedScript> { |
e00f7325unknown10 years ago | 29 | |
| 30 | // We'll get the source code, and store it locally to have a better debugging experience | |
| 31 | return new Request().request(scriptUrlString, true).then(scriptBody => { | |
| 32 | // Extract sourceMappingURL from body | |
| 33 | let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" | |
4bd0c669dlebu10 years ago | 34 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
e00f7325unknown10 years ago | 35 | |
4677921cdigeff10 years ago | 36 | let waitForSourceMapping = Q<void>(null); |
e00f7325unknown10 years ago | 37 | if (sourceMappingUrl) { |
| 38 | /* handle source map - request it and store it locally */ | |
4677921cdigeff10 years ago | 39 | waitForSourceMapping = this.writeSourceMap(sourceMappingUrl, scriptUrl) |
3af9a124unknown10 years ago | 40 | .then(() => { |
4bd0c669dlebu10 years ago | 41 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl); |
4677921cdigeff10 years ago | 42 | }); |
e00f7325unknown10 years ago | 43 | } |
4677921cdigeff10 years ago | 44 | |
| 45 | return waitForSourceMapping | |
| 46 | .then(() => this.writeScript(scriptBody, scriptUrl)) | |
| 47 | .then((scriptFilePath: string) => { | |
ea8a5f88digeff10 years ago | 48 | Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`); |
4677921cdigeff10 years ago | 49 | return { contents: scriptBody, filepath: scriptFilePath }; |
87e1f776Jimmy Thomson10 years ago | 50 | }).finally(() => { |
| 51 | // Request that the debug adapter update breakpoints and sourcemaps now that we have written them | |
| 52 | return new Request().request(`http://localhost:${this.debugAdapterPort}/refreshBreakpoints`); | |
876df2a0dlebu10 years ago | 53 | }); |
e00f7325unknown10 years ago | 54 | }); |
| 55 | } | |
| 56 | | |
| 57 | /** | |
| 58 | * Writes the script file to the project temporary location. | |
| 59 | */ | |
| 60 | private writeScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> { | |
f218397cdlebu10 years ago | 61 | let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 62 | return new FileSystem().writeFile(scriptFilePath, scriptBody) | |
| 63 | .then(() => scriptFilePath); | |
3fb37ad5unknown10 years ago | 64 | } |
| 65 | | |
e00f7325unknown10 years ago | 66 | /** |
| 67 | * Writes the source map file to the project temporary location. | |
| 68 | */ | |
| 69 | private writeSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> { | |
| 70 | return new Request().request(sourceMapUrl.href, true) | |
| 71 | .then((sourceMapBody: string) => { | |
4677921cdigeff10 years ago | 72 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
e00f7325unknown10 years ago | 73 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
f218397cdlebu10 years ago | 74 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath); |
| 75 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); | |
e00f7325unknown10 years ago | 76 | }); |
3fb37ad5unknown10 years ago | 77 | } |
| 78 | } |