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