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