microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
52bf0a75506c1b3b1cecc9ad89937e596d1bc22a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

73lines · 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
f218397cdlebu10 years ago4import {FileSystem} from "../utils/node/fileSystem";
ea8a5f88digeff10 years ago5import {Log, LogLevel} from "../utils/commands/log";
3fb37ad5unknown10 years ago6import path = require("path");
7import Q = require("q");
e00f7325unknown10 years ago8import {Request} from "../utils/node/request";
876df2a0dlebu10 years ago9import {SourceMapUtil} from "../utils/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;
4bd0c669dlebu10 years ago19private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago20
4677921cdigeff10 years ago21constructor(sourcesStoragePath: string) {
22this.sourcesStoragePath = sourcesStoragePath;
4bd0c669dlebu10 years ago23this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago24}
25
4677921cdigeff10 years ago26public download(scriptUrlString: string): Q.Promise<DownloadedScript> {
e00f7325unknown10 years ago27
28// We'll get the source code, and store it locally to have a better debugging experience
29return new Request().request(scriptUrlString, true).then(scriptBody => {
30// Extract sourceMappingURL from body
31let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago32let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago33
4677921cdigeff10 years ago34let waitForSourceMapping = Q<void>(null);
e00f7325unknown10 years ago35if (sourceMappingUrl) {
36/* handle source map - request it and store it locally */
4677921cdigeff10 years ago37waitForSourceMapping = this.writeSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago38.then(() => {
4bd0c669dlebu10 years ago39scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
4677921cdigeff10 years ago40});
e00f7325unknown10 years ago41}
4677921cdigeff10 years ago42
43return waitForSourceMapping
44.then(() => this.writeScript(scriptBody, scriptUrl))
45.then((scriptFilePath: string) => {
ea8a5f88digeff10 years ago46Log.logInternalMessage(LogLevel.Info, `Script ${scriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago47return { contents: scriptBody, filepath: scriptFilePath };
876df2a0dlebu10 years ago48});
e00f7325unknown10 years ago49});
50}
51
52/**
53* Writes the script file to the project temporary location.
54*/
55private writeScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
f218397cdlebu10 years ago56let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle"
57return new FileSystem().writeFile(scriptFilePath, scriptBody)
58.then(() => scriptFilePath);
3fb37ad5unknown10 years ago59}
60
e00f7325unknown10 years ago61/**
62* Writes the source map file to the project temporary location.
63*/
64private writeSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
65return new Request().request(sourceMapUrl.href, true)
66.then((sourceMapBody: string) => {
4677921cdigeff10 years ago67let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
e00f7325unknown10 years ago68let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
f218397cdlebu10 years ago69let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
70return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago71});
3fb37ad5unknown10 years ago72}
73}