microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
efd8fbff90ccf6d8b6743b251723b1ee27e9a2bb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

91lines · 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 fs = require("fs");
5import {Log} from "../utils/commands/log";
6import path = require("path");
7import Q = require("q");
8import {Request} from "../utils/node/request";
9import {SourceMapUtil} from "../utils/sourceMap";
10import url = require("url");
11
12interface DownloadedScript {
13 contents: string;
14 filepath: string;
15}
16
17export class ScriptImporter {
18 private sourcesStoragePath: string;
19 private sourceMapUtil: SourceMapUtil;
20
21 constructor(sourcesStoragePath: string) {
22 this.sourcesStoragePath = sourcesStoragePath;
23 this.sourceMapUtil = new SourceMapUtil();
24 }
25
26 public download(scriptUrlString: string): Q.Promise<DownloadedScript> {
27
28 // We'll get the source code, and store it locally to have a better debugging experience
29 return new Request().request(scriptUrlString, true).then(scriptBody => {
30 // Extract sourceMappingURL from body
31 let scriptUrl = url.parse(scriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
32 let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
33
34 let waitForSourceMapping = Q<void>(null);
35 if (sourceMappingUrl) {
36 /* handle source map - request it and store it locally */
37 waitForSourceMapping = this.writeSourceMap(sourceMappingUrl, scriptUrl)
38 .then(() => {
39 scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
40 });
41 }
42
43 return waitForSourceMapping
44 .then(() => this.writeScript(scriptBody, scriptUrl))
45 .then((scriptFilePath: string) => {
46 Log.logInternalMessage(`Script ${scriptUrlString} downloaded to ${scriptFilePath}`);
47 return { contents: scriptBody, filepath: scriptFilePath };
48 });
49 });
50 }
51
52 /**
53 * Writes the script file to the project temporary location.
54 */
55 private writeScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
56 return Q.fcall(() => {
57 let scriptFilePath = path.join(this.sourcesStoragePath, scriptUrl.pathname); // scriptFilePath = "$TMPDIR/index.ios.bundle"
58 this.writeTemporaryFileSync(scriptFilePath, scriptBody);
59 return scriptFilePath;
60 });
61 }
62
63 /**
64 * Writes the source map file to the project temporary location.
65 */
66 private writeSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
67 return new Request().request(sourceMapUrl.href, true)
68 .then((sourceMapBody: string) => {
69 let sourceMappingLocalPath = path.join(this.sourcesStoragePath, sourceMapUrl.pathname); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
70 let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
71 this.writeTemporaryFileSync(sourceMappingLocalPath, this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath));
72 });
73 }
74
75 private writeTemporaryFileSync(filename: string, data: string): Q.Promise<void> {
76 let writeFile = Q.nfbind<void>(fs.writeFile);
77
78 return writeFile(filename, data)
79 .then(() => this.scheduleTemporaryFileCleanUp(filename));
80 }
81
82 private scheduleTemporaryFileCleanUp(filename: string): void {
83 process.on("exit", function() {
84 let unlink = Q.nfbind<void>(fs.unlink);
85 unlink(filename)
86 .then(() => {
87 Log.logMessage("Succesfully cleaned temporary file: " + filename);
88 });
89 });
90 }
91}
92