microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
db80cd4e10b83524bc77c15018effab72cbabeb8

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

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