microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
73lines · 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 sourceMapUtil: SourceMapUtil; |
| 20 | |
| 21 | constructor(sourcesStoragePath: string) { |
| 22 | this.sourcesStoragePath = sourcesStoragePath; |
| 23 | this.sourceMapUtil = new SourceMapUtil(); |
| 24 | } |
| 25 | |
| 26 | public download(scriptUrlString: string): Q.Promise<DownloadedScript> { |
| 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" |
| 32 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
| 33 | |
| 34 | let waitForSourceMapping = Q<void>(null); |
| 35 | if (sourceMappingUrl) { |
| 36 | /* handle source map - request it and store it locally */ |
| 37 | waitForSourceMapping = this.writeSourceMap(sourceMappingUrl, scriptUrl) |
| 38 | .then(() => { |
| 39 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl); |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | return waitForSourceMapping |
| 44 | .then(() => this.writeScript(scriptBody, scriptUrl)) |
| 45 | .then((scriptFilePath: string) => { |
| 46 | Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`); |
| 47 | return { contents: scriptBody, filepath: scriptFilePath }; |
| 48 | }); |
| 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> { |
| 56 | let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 57 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 58 | .then(() => scriptFilePath); |
| 59 | } |
| 60 | |
| 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) => { |
| 67 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 68 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
| 69 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath); |
| 70 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
| 71 | }); |
| 72 | } |
| 73 | } |
| 74 | |