microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f5edf87f2af14e37e63c6f6638d9558fc6a5d1ee

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

88lines · 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";
5import {Log, LogLevel} from "../common/log";
87323739dlebu10 years ago6import {Packager} from "../common/packager";
3fb37ad5unknown10 years ago7import path = require("path");
8import Q = require("q");
b0061ac6Meena Kunnathur Balakrishnan10 years ago9import {Request} from "../common/node/request";
10import {SourceMapUtil} from "./sourceMap";
3fb37ad5unknown10 years ago11import url = require("url");
12
4677921cdigeff10 years ago13interface DownloadedScript {
14contents: string;
15filepath: string;
16}
17
3fb37ad5unknown10 years ago18export class ScriptImporter {
2743f19cdlebu10 years ago19public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
20public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js";
4677921cdigeff10 years ago21private sourcesStoragePath: string;
4bd0c669dlebu10 years ago22private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago23
2743f19cdlebu10 years ago24constructor(sourcesStoragePath: string) {
4677921cdigeff10 years ago25this.sourcesStoragePath = sourcesStoragePath;
4bd0c669dlebu10 years ago26this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago27}
28
2743f19cdlebu10 years ago29public downloadAppScript(scriptUrlString: string, debugAdapterPort: number): Q.Promise<DownloadedScript> {
e00f7325unknown10 years ago30
31// We'll get the source code, and store it locally to have a better debugging experience
32return new Request().request(scriptUrlString, true).then(scriptBody => {
33// Extract sourceMappingURL from body
34let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago35let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago36
4677921cdigeff10 years ago37let waitForSourceMapping = Q<void>(null);
e00f7325unknown10 years ago38if (sourceMappingUrl) {
39/* handle source map - request it and store it locally */
2743f19cdlebu10 years ago40waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago41.then(() => {
4bd0c669dlebu10 years ago42scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
4677921cdigeff10 years ago43});
e00f7325unknown10 years ago44}
4677921cdigeff10 years ago45
46return waitForSourceMapping
2743f19cdlebu10 years ago47.then(() => this.writeAppScript(scriptBody, scriptUrl))
4677921cdigeff10 years ago48.then((scriptFilePath: string) => {
ea8a5f88digeff10 years ago49Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago50return { contents: scriptBody, filepath: scriptFilePath };
87e1f776Jimmy Thomson10 years ago51}).finally(() => {
52// Request that the debug adapter update breakpoints and sourcemaps now that we have written them
2743f19cdlebu10 years ago53return new Request().request(`http://localhost:${debugAdapterPort}/refreshBreakpoints`);
876df2a0dlebu10 years ago54});
e00f7325unknown10 years ago55});
56}
57
2743f19cdlebu10 years ago58public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> {
59let debuggerWorkerURL = `http://${Packager.HOST}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`;
60let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
61Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
62return new Request().request(debuggerWorkerURL, true).then((body: string) => {
63return new FileSystem().writeFile(debuggerWorkerLocalPath, body);
64});
65}
66
e00f7325unknown10 years ago67/**
68* Writes the script file to the project temporary location.
69*/
2743f19cdlebu10 years ago70private writeAppScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
f218397cdlebu10 years ago71let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle"
72return new FileSystem().writeFile(scriptFilePath, scriptBody)
73.then(() => scriptFilePath);
3fb37ad5unknown10 years ago74}
75
e00f7325unknown10 years ago76/**
77* Writes the source map file to the project temporary location.
78*/
2743f19cdlebu10 years ago79private writeAppSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
e00f7325unknown10 years ago80return new Request().request(sourceMapUrl.href, true)
81.then((sourceMapBody: string) => {
4677921cdigeff10 years ago82let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
e00f7325unknown10 years ago83let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
f218397cdlebu10 years ago84let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
85return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago86});
3fb37ad5unknown10 years ago87}
88}