microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
65bb0c85df6a89b406b370fd653d87f0a7043304

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
4import {FileSystem} from "../common/node/fileSystem";
5import {Log, LogLevel} from "../common/log";
6import path = require("path");
7import Q = require("q");
8import {Request} from "../common/node/request";
9import {SourceMapUtil} from "./sourceMap";
10import url = require("url");
11
12interface DownloadedScript {
13 contents: string;
14 filepath: string;
15}
16
17export 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