microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e19edf878c22f465bd26fd685bc24bbd72752f40

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

89lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
b0061ac6Meena Kunnathur Balakrishnan10 years ago4import {FileSystem} from "../common/node/fileSystem";
190e393cMeena Kunnathur Balakrishnan10 years ago5import {Log} from "../common/log/log";
6import {LogLevel} from "../common/log/logHelper";
87323739dlebu10 years ago7import {Packager} from "../common/packager";
3fb37ad5unknown10 years ago8import path = require("path");
9import Q = require("q");
b0061ac6Meena Kunnathur Balakrishnan10 years ago10import {Request} from "../common/node/request";
11import {SourceMapUtil} from "./sourceMap";
3fb37ad5unknown10 years ago12import url = require("url");
13
4677921cdigeff10 years ago14interface DownloadedScript {
15contents: string;
16filepath: string;
17}
18
3fb37ad5unknown10 years ago19export class ScriptImporter {
2743f19cdlebu10 years ago20public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
21public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js";
4677921cdigeff10 years ago22private sourcesStoragePath: string;
4bd0c669dlebu10 years ago23private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago24
2743f19cdlebu10 years ago25constructor(sourcesStoragePath: string) {
4677921cdigeff10 years ago26this.sourcesStoragePath = sourcesStoragePath;
4bd0c669dlebu10 years ago27this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago28}
29
2743f19cdlebu10 years ago30public downloadAppScript(scriptUrlString: string, debugAdapterPort: number): Q.Promise<DownloadedScript> {
e00f7325unknown10 years ago31
32// We'll get the source code, and store it locally to have a better debugging experience
33return new Request().request(scriptUrlString, true).then(scriptBody => {
34// Extract sourceMappingURL from body
35let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago36let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago37
4677921cdigeff10 years ago38let waitForSourceMapping = Q<void>(null);
e00f7325unknown10 years ago39if (sourceMappingUrl) {
40/* handle source map - request it and store it locally */
2743f19cdlebu10 years ago41waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago42.then(() => {
4bd0c669dlebu10 years ago43scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
4677921cdigeff10 years ago44});
e00f7325unknown10 years ago45}
4677921cdigeff10 years ago46
47return waitForSourceMapping
2743f19cdlebu10 years ago48.then(() => this.writeAppScript(scriptBody, scriptUrl))
4677921cdigeff10 years ago49.then((scriptFilePath: string) => {
ea8a5f88digeff10 years ago50Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago51return { contents: scriptBody, filepath: scriptFilePath };
87e1f776Jimmy Thomson10 years ago52}).finally(() => {
53// Request that the debug adapter update breakpoints and sourcemaps now that we have written them
2743f19cdlebu10 years ago54return new Request().request(`http://localhost:${debugAdapterPort}/refreshBreakpoints`);
876df2a0dlebu10 years ago55});
e00f7325unknown10 years ago56});
57}
58
2743f19cdlebu10 years ago59public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> {
60let debuggerWorkerURL = `http://${Packager.HOST}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`;
61let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
62Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
63return new Request().request(debuggerWorkerURL, true).then((body: string) => {
64return new FileSystem().writeFile(debuggerWorkerLocalPath, body);
65});
66}
67
e00f7325unknown10 years ago68/**
69* Writes the script file to the project temporary location.
70*/
2743f19cdlebu10 years ago71private writeAppScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
f218397cdlebu10 years ago72let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle"
73return new FileSystem().writeFile(scriptFilePath, scriptBody)
74.then(() => scriptFilePath);
3fb37ad5unknown10 years ago75}
76
e00f7325unknown10 years ago77/**
78* Writes the source map file to the project temporary location.
79*/
2743f19cdlebu10 years ago80private writeAppSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
e00f7325unknown10 years ago81return new Request().request(sourceMapUrl.href, true)
82.then((sourceMapBody: string) => {
4677921cdigeff10 years ago83let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
e00f7325unknown10 years ago84let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
f218397cdlebu10 years ago85let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
86return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago87});
3fb37ad5unknown10 years ago88}
89}