microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
78lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {FileSystem} from "../common/node/fileSystem"; |
| 5 | import {Log, LogLevel} from "../common/log"; |
| 6 | import path = require("path"); |
| 7 | import Q = require("q"); |
| 8 | import {Request} from "../common/node/request"; |
| 9 | import {SourceMapUtil} from "./sourceMap"; |
| 10 | import url = require("url"); |
| 11 | |
| 12 | interface DownloadedScript { |
| 13 | contents: string; |
| 14 | filepath: string; |
| 15 | } |
| 16 | |
| 17 | export class ScriptImporter { |
| 18 | private sourcesStoragePath: string; |
| 19 | private debugAdapterPort: number; |
| 20 | private sourceMapUtil: SourceMapUtil; |
| 21 | |
| 22 | constructor(sourcesStoragePath: string, debugAdapterPort: number) { |
| 23 | this.sourcesStoragePath = sourcesStoragePath; |
| 24 | this.debugAdapterPort = debugAdapterPort; |
| 25 | this.sourceMapUtil = new SourceMapUtil(); |
| 26 | } |
| 27 | |
| 28 | public download(scriptUrlString: string): Q.Promise<DownloadedScript> { |
| 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" |
| 34 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
| 35 | |
| 36 | let waitForSourceMapping = Q<void>(null); |
| 37 | if (sourceMappingUrl) { |
| 38 | /* handle source map - request it and store it locally */ |
| 39 | waitForSourceMapping = this.writeSourceMap(sourceMappingUrl, scriptUrl) |
| 40 | .then(() => { |
| 41 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl); |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | return waitForSourceMapping |
| 46 | .then(() => this.writeScript(scriptBody, scriptUrl)) |
| 47 | .then((scriptFilePath: string) => { |
| 48 | Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`); |
| 49 | return { contents: scriptBody, filepath: scriptFilePath }; |
| 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`); |
| 53 | }); |
| 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> { |
| 61 | let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 62 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 63 | .then(() => scriptFilePath); |
| 64 | } |
| 65 | |
| 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) => { |
| 72 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 73 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
| 74 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath); |
| 75 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | |