microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
91lines · 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 fs = require("fs"); |
| 5 | import {Log} from "../utils/commands/log"; |
| 6 | import path = require("path"); |
| 7 | import Q = require("q"); |
| 8 | import {Request} from "../utils/node/request"; |
| 9 | import {SourceMapUtil} from "../utils/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(`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 | return Q.fcall(() => { |
| 57 | let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 58 | this.writeTemporaryFileSync(scriptFilePath, scriptBody); |
| 59 | return scriptFilePath; |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Writes the source map file to the project temporary location. |
| 65 | */ |
| 66 | private writeSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> { |
| 67 | return new Request().request(sourceMapUrl.href, true) |
| 68 | .then((sourceMapBody: string) => { |
| 69 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 70 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
| 71 | this.writeTemporaryFileSync(sourceMappingLocalPath, this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath)); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | private writeTemporaryFileSync(filename: string, data: string): Q.Promise<void> { |
| 76 | let writeFile = Q.nfbind<void>(fs.writeFile); |
| 77 | |
| 78 | return writeFile(filename, data) |
| 79 | .then(() => this.scheduleTemporaryFileCleanUp(filename)); |
| 80 | } |
| 81 | |
| 82 | private scheduleTemporaryFileCleanUp(filename: string): void { |
| 83 | process.on("exit", function() { |
| 84 | let unlink = Q.nfbind<void>(fs.unlink); |
| 85 | unlink(filename) |
| 86 | .then(() => { |
| 87 | Log.logMessage("Succesfully cleaned temporary file: " + filename); |
| 88 | }); |
| 89 | }); |
| 90 | } |
| 91 | } |
| 92 | |