microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b031edc774df5bdef810349bdd6d2c40c7acfadd

Branches

Tags

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

Clone

HTTPS

Download ZIP

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
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 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