microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a1348eed5cf901c8d3f16b8ef331779423afc414

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

89lines · 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} from "../common/log/log";
6import {LogLevel} from "../common/log/logHelper";
7import {Packager} from "../common/packager";
8import path = require("path");
9import Q = require("q");
10import {Request} from "../common/node/request";
11import {SourceMapUtil} from "./sourceMap";
12import url = require("url");
13
14interface DownloadedScript {
15 contents: string;
16 filepath: string;
17}
18
19export class ScriptImporter {
20 public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
21 public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js";
22 private sourcesStoragePath: string;
23 private sourceMapUtil: SourceMapUtil;
24
25 constructor(sourcesStoragePath: string) {
26 this.sourcesStoragePath = sourcesStoragePath;
27 this.sourceMapUtil = new SourceMapUtil();
28 }
29
30 public downloadAppScript(scriptUrlString: string, debugAdapterPort: number): Q.Promise<DownloadedScript> {
31
32 // We'll get the source code, and store it locally to have a better debugging experience
33 return new Request().request(scriptUrlString, true).then(scriptBody => {
34 // Extract sourceMappingURL from body
35 let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
36 let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
37
38 let waitForSourceMapping = Q<void>(null);
39 if (sourceMappingUrl) {
40 /* handle source map - request it and store it locally */
41 waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl)
42 .then(() => {
43 scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
44 });
45 }
46
47 return waitForSourceMapping
48 .then(() => this.writeAppScript(scriptBody, scriptUrl))
49 .then((scriptFilePath: string) => {
50 Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`);
51 return { contents: scriptBody, filepath: scriptFilePath };
52 }).finally(() => {
53 // Request that the debug adapter update breakpoints and sourcemaps now that we have written them
54 return new Request().request(`http://localhost:${debugAdapterPort}/refreshBreakpoints`);
55 });
56 });
57 }
58
59 public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> {
60 let debuggerWorkerURL = `http://${Packager.HOST}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`;
61 let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
62 Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
63 return new Request().request(debuggerWorkerURL, true).then((body: string) => {
64 return new FileSystem().writeFile(debuggerWorkerLocalPath, body);
65 });
66 }
67
68 /**
69 * Writes the script file to the project temporary location.
70 */
71 private writeAppScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
72 let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle"
73 return new FileSystem().writeFile(scriptFilePath, scriptBody)
74 .then(() => scriptFilePath);
75 }
76
77 /**
78 * Writes the source map file to the project temporary location.
79 */
80 private writeAppSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
81 return new Request().request(sourceMapUrl.href, true)
82 .then((sourceMapBody: string) => {
83 let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
84 let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
85 let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
86 return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
87 });
88 }
89}
90