microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
126lines · modeblame
a31b007cunknown10 years ago | 1 | // 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 ago | 4 | import {FileSystem} from "../common/node/fileSystem"; |
0a68f8dbArtem Egorov8 years ago | 5 | import { logger } from "vscode-chrome-debug-core"; |
| 6 | import { ensurePackagerRunning } from "../common/packagerStatus"; | |
3fb37ad5unknown10 years ago | 7 | import path = require("path"); |
| 8 | import Q = require("q"); | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 9 | import {Request} from "../common/node/request"; |
| 10 | import {SourceMapUtil} from "./sourceMap"; | |
3fb37ad5unknown10 years ago | 11 | import url = require("url"); |
a1324704Artem Egorov8 years ago | 12 | import * as semver from "semver"; |
| 13 | import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper"; | |
3fb37ad5unknown10 years ago | 14 | |
5c8365a6Artem Egorov8 years ago | 15 | export interface DownloadedScript { |
4677921cdigeff10 years ago | 16 | contents: string; |
| 17 | filepath: string; | |
| 18 | } | |
| 19 | | |
5c8365a6Artem Egorov8 years ago | 20 | interface IStrictUrl extends url.Url { |
| 21 | pathname: string; | |
| 22 | href: string; | |
| 23 | } | |
| 24 | | |
3fb37ad5unknown10 years ago | 25 | export class ScriptImporter { |
2743f19cdlebu10 years ago | 26 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 27 | public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js"; | |
6eeec3c0Serge Svekolnikov8 years ago | 28 | private packagerAddress: string; |
e3ae4227digeff10 years ago | 29 | private packagerPort: number; |
4677921cdigeff10 years ago | 30 | private sourcesStoragePath: string; |
6eeec3c0Serge Svekolnikov8 years ago | 31 | private packagerRemoteRoot?: string; |
| 32 | private packagerLocalRoot?: string; | |
4bd0c669dlebu10 years ago | 33 | private sourceMapUtil: SourceMapUtil; |
3fb37ad5unknown10 years ago | 34 | |
6eeec3c0Serge Svekolnikov8 years ago | 35 | constructor(packagerAddress: string, packagerPort: number, sourcesStoragePath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) { |
| 36 | this.packagerAddress = packagerAddress; | |
e3ae4227digeff10 years ago | 37 | this.packagerPort = packagerPort; |
4677921cdigeff10 years ago | 38 | this.sourcesStoragePath = sourcesStoragePath; |
6eeec3c0Serge Svekolnikov8 years ago | 39 | this.packagerRemoteRoot = packagerRemoteRoot; |
| 40 | this.packagerLocalRoot = packagerLocalRoot; | |
4bd0c669dlebu10 years ago | 41 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 42 | } |
| 43 | | |
e45838cbVladimir Kotikov9 years ago | 44 | public downloadAppScript(scriptUrlString: string): Q.Promise<DownloadedScript> { |
1c32fe84Patricio Beltran9 years ago | 45 | const parsedScriptUrl = url.parse(scriptUrlString); |
| 46 | const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString; | |
e00f7325unknown10 years ago | 47 | // We'll get the source code, and store it locally to have a better debugging experience |
833e37c7Vladimir Kotikov8 years ago | 48 | return Request.request(overriddenScriptUrlString, true).then(scriptBody => { |
e00f7325unknown10 years ago | 49 | // Extract sourceMappingURL from body |
5c8365a6Artem Egorov8 years ago | 50 | let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" |
4bd0c669dlebu10 years ago | 51 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
e00f7325unknown10 years ago | 52 | |
5c8365a6Artem Egorov8 years ago | 53 | let waitForSourceMapping = Q<void>(void 0); |
e00f7325unknown10 years ago | 54 | if (sourceMappingUrl) { |
| 55 | /* handle source map - request it and store it locally */ | |
2743f19cdlebu10 years ago | 56 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl) |
3af9a124unknown10 years ago | 57 | .then(() => { |
5c8365a6Artem Egorov8 years ago | 58 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, <IStrictUrl>sourceMappingUrl); |
4677921cdigeff10 years ago | 59 | }); |
e00f7325unknown10 years ago | 60 | } |
4677921cdigeff10 years ago | 61 | |
| 62 | return waitForSourceMapping | |
2743f19cdlebu10 years ago | 63 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
4677921cdigeff10 years ago | 64 | .then((scriptFilePath: string) => { |
0a68f8dbArtem Egorov8 years ago | 65 | logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`); |
4677921cdigeff10 years ago | 66 | return { contents: scriptBody, filepath: scriptFilePath }; |
876df2a0dlebu10 years ago | 67 | }); |
e00f7325unknown10 years ago | 68 | }); |
| 69 | } | |
| 70 | | |
a1324704Artem Egorov8 years ago | 71 | public downloadDebuggerWorker(sourcesStoragePath: string, projectRootPath: string): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 72 | const errPackagerNotRunning = new RangeError(`Cannot attach to packager. Are you sure there is a packager and it is running in the port ${this.packagerPort}? If your packager is configured to run in another port make sure to add that to the setting.json.`); |
| 73 | | |
6eeec3c0Serge Svekolnikov8 years ago | 74 | return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning) |
0a68f8dbArtem Egorov8 years ago | 75 | .then(() => { |
a1324704Artem Egorov8 years ago | 76 | return ReactNativeProjectHelper.getReactNativeVersion(projectRootPath); |
| 77 | }) | |
| 78 | .then((rnVersion: string) => { | |
| 79 | let newPackager = ""; | |
bb869343Serge Svekolnikov8 years ago | 80 | const isHaulProject = ReactNativeProjectHelper.isHaulProject(projectRootPath); |
| 81 | if (semver.gte(rnVersion, "0.50.0") && !isHaulProject) { | |
a1324704Artem Egorov8 years ago | 82 | newPackager = "debugger-ui/"; |
| 83 | } | |
| 84 | let debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${newPackager}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
0a68f8dbArtem Egorov8 years ago | 85 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); |
| 86 | logger.verbose("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); | |
| 87 | | |
| 88 | return Request.request(debuggerWorkerURL, true) | |
| 89 | .then((body: string) => { | |
299b0557Patricio Beltran10 years ago | 90 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 91 | }); | |
| 92 | }); | |
2743f19cdlebu10 years ago | 93 | } |
| 94 | | |
e00f7325unknown10 years ago | 95 | /** |
| 96 | * Writes the script file to the project temporary location. | |
| 97 | */ | |
5c8365a6Artem Egorov8 years ago | 98 | private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Q.Promise<String> { |
bb77358cMark Oswald10 years ago | 99 | let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
f218397cdlebu10 years ago | 100 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 101 | .then(() => scriptFilePath); | |
3fb37ad5unknown10 years ago | 102 | } |
| 103 | | |
e00f7325unknown10 years ago | 104 | /** |
| 105 | * Writes the source map file to the project temporary location. | |
| 106 | */ | |
5c8365a6Artem Egorov8 years ago | 107 | private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Q.Promise<void> { |
833e37c7Vladimir Kotikov8 years ago | 108 | return Request.request(sourceMapUrl.href, true) |
e00f7325unknown10 years ago | 109 | .then((sourceMapBody: string) => { |
bb77358cMark Oswald10 years ago | 110 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 111 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" | |
6eeec3c0Serge Svekolnikov8 years ago | 112 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot); |
f218397cdlebu10 years ago | 113 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
e00f7325unknown10 years ago | 114 | }); |
3fb37ad5unknown10 years ago | 115 | } |
5e651f3edigeff10 years ago | 116 | |
| 117 | /** | |
| 118 | * Changes the port of the url to be the one configured as this.packagerPort | |
| 119 | */ | |
| 120 | private overridePackagerPort(urlToOverride: string): string { | |
| 121 | let components = url.parse(urlToOverride); | |
| 122 | components.port = this.packagerPort.toString(); | |
| 123 | delete components.host; // We delete the host, if not the port change will be ignored | |
| 124 | return url.format(components); | |
| 125 | } | |
3fb37ad5unknown10 years ago | 126 | } |