microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
89lines · 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"; | |
4677921cdigeff10 years ago | 22 | private sourcesStoragePath: string; |
4bd0c669dlebu10 years ago | 23 | private sourceMapUtil: SourceMapUtil; |
3fb37ad5unknown10 years ago | 24 | |
2743f19cdlebu10 years ago | 25 | constructor(sourcesStoragePath: string) { |
4677921cdigeff10 years ago | 26 | this.sourcesStoragePath = sourcesStoragePath; |
4bd0c669dlebu10 years ago | 27 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 28 | } |
| 29 | | |
2743f19cdlebu10 years ago | 30 | public downloadAppScript(scriptUrlString: string, debugAdapterPort: number): Q.Promise<DownloadedScript> { |
e00f7325unknown10 years ago | 31 | |
| 32 | // We'll get the source code, and store it locally to have a better debugging experience | |
| 33 | return new Request().request(scriptUrlString, true).then(scriptBody => { | |
| 34 | // Extract sourceMappingURL from body | |
| 35 | let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" | |
4bd0c669dlebu10 years ago | 36 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
e00f7325unknown10 years ago | 37 | |
4677921cdigeff10 years ago | 38 | let waitForSourceMapping = Q<void>(null); |
e00f7325unknown10 years ago | 39 | if (sourceMappingUrl) { |
| 40 | /* handle source map - request it and store it locally */ | |
2743f19cdlebu10 years ago | 41 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl) |
3af9a124unknown10 years ago | 42 | .then(() => { |
4bd0c669dlebu10 years ago | 43 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl); |
4677921cdigeff10 years ago | 44 | }); |
e00f7325unknown10 years ago | 45 | } |
4677921cdigeff10 years ago | 46 | |
| 47 | return waitForSourceMapping | |
2743f19cdlebu10 years ago | 48 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
4677921cdigeff10 years ago | 49 | .then((scriptFilePath: string) => { |
ea8a5f88digeff10 years ago | 50 | Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`); |
4677921cdigeff10 years ago | 51 | return { contents: scriptBody, filepath: scriptFilePath }; |
87e1f776Jimmy Thomson10 years ago | 52 | }).finally(() => { |
| 53 | // Request that the debug adapter update breakpoints and sourcemaps now that we have written them | |
2743f19cdlebu10 years ago | 54 | return new Request().request(`http://localhost:${debugAdapterPort}/refreshBreakpoints`); |
876df2a0dlebu10 years ago | 55 | }); |
e00f7325unknown10 years ago | 56 | }); |
| 57 | } | |
| 58 | | |
2743f19cdlebu10 years ago | 59 | public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> { |
| 60 | let debuggerWorkerURL = `http://${Packager.HOST}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 61 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); | |
| 62 | Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); | |
| 63 | return new Request().request(debuggerWorkerURL, true).then((body: string) => { | |
| 64 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); | |
| 65 | }); | |
| 66 | } | |
| 67 | | |
e00f7325unknown10 years ago | 68 | /** |
| 69 | * Writes the script file to the project temporary location. | |
| 70 | */ | |
2743f19cdlebu10 years ago | 71 | private writeAppScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> { |
f218397cdlebu10 years ago | 72 | let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 73 | return new FileSystem().writeFile(scriptFilePath, scriptBody) | |
| 74 | .then(() => scriptFilePath); | |
3fb37ad5unknown10 years ago | 75 | } |
| 76 | | |
e00f7325unknown10 years ago | 77 | /** |
| 78 | * Writes the source map file to the project temporary location. | |
| 79 | */ | |
2743f19cdlebu10 years ago | 80 | private writeAppSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> { |
e00f7325unknown10 years ago | 81 | return new Request().request(sourceMapUrl.href, true) |
| 82 | .then((sourceMapBody: string) => { | |
4677921cdigeff10 years ago | 83 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
e00f7325unknown10 years ago | 84 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
f218397cdlebu10 years ago | 85 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath); |
| 86 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); | |
e00f7325unknown10 years ago | 87 | }); |
3fb37ad5unknown10 years ago | 88 | } |
| 89 | } |