microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
da86e6fffddfaf7f6ca5a97cf9f1c345394f3bdc

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

78lines · 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";
3fb37ad5unknown10 years ago6import path = require("path");
7import Q = require("q");
b0061ac6Meena Kunnathur Balakrishnan10 years ago8import {Request} from "../common/node/request";
9import {SourceMapUtil} from "./sourceMap";
3fb37ad5unknown10 years ago10import url = require("url");
11
4677921cdigeff10 years ago12interface DownloadedScript {
13contents: string;
14filepath: string;
15}
16
3fb37ad5unknown10 years ago17export class ScriptImporter {
4677921cdigeff10 years ago18private sourcesStoragePath: string;
5547a16fJimmy Thomson10 years ago19private debugAdapterPort: number;
4bd0c669dlebu10 years ago20private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago21
5547a16fJimmy Thomson10 years ago22constructor(sourcesStoragePath: string, debugAdapterPort: number) {
4677921cdigeff10 years ago23this.sourcesStoragePath = sourcesStoragePath;
5547a16fJimmy Thomson10 years ago24this.debugAdapterPort = debugAdapterPort;
4bd0c669dlebu10 years ago25this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago26}
27
4677921cdigeff10 years ago28public download(scriptUrlString: string): Q.Promise<DownloadedScript> {
e00f7325unknown10 years ago29
30// We'll get the source code, and store it locally to have a better debugging experience
31return new Request().request(scriptUrlString, true).then(scriptBody => {
32// Extract sourceMappingURL from body
33let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago34let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago35
4677921cdigeff10 years ago36let waitForSourceMapping = Q<void>(null);
e00f7325unknown10 years ago37if (sourceMappingUrl) {
38/* handle source map - request it and store it locally */
4677921cdigeff10 years ago39waitForSourceMapping = this.writeSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago40.then(() => {
4bd0c669dlebu10 years ago41scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
4677921cdigeff10 years ago42});
e00f7325unknown10 years ago43}
4677921cdigeff10 years ago44
45return waitForSourceMapping
46.then(() => this.writeScript(scriptBody, scriptUrl))
47.then((scriptFilePath: string) => {
ea8a5f88digeff10 years ago48Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago49return { contents: scriptBody, filepath: scriptFilePath };
87e1f776Jimmy Thomson10 years ago50}).finally(() => {
51// Request that the debug adapter update breakpoints and sourcemaps now that we have written them
52return new Request().request(`http://localhost:${this.debugAdapterPort}/refreshBreakpoints`);
876df2a0dlebu10 years ago53});
e00f7325unknown10 years ago54});
55}
56
57/**
58* Writes the script file to the project temporary location.
59*/
60private writeScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
f218397cdlebu10 years ago61let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle"
62return new FileSystem().writeFile(scriptFilePath, scriptBody)
63.then(() => scriptFilePath);
3fb37ad5unknown10 years ago64}
65
e00f7325unknown10 years ago66/**
67* Writes the source map file to the project temporary location.
68*/
69private writeSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
70return new Request().request(sourceMapUrl.href, true)
71.then((sourceMapBody: string) => {
4677921cdigeff10 years ago72let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
e00f7325unknown10 years ago73let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
f218397cdlebu10 years ago74let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
75return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago76});
3fb37ad5unknown10 years ago77}
78}