microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
5d4d4de075da5ed528840d529a22d057e2a7cd89

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/packager.ts

74lines · 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 {PlatformResolver} from "./platformResolver";
5import {PromiseUtil} from "../utils/node/promise";
6import {Request} from "../utils/node/request";
7import {CommandExecutor} from "../utils/commands/commandExecutor";
8import {Log} from "../utils/commands/log";
9import {Node} from "../utils/node/node";
10import * as Q from "q";
11import * as path from "path";
12
13export class Packager {
14 public static HOST = "localhost:8081";
15 public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
16 public static DEBUGGER_WORKER_FILENAME = Packager.DEBUGGER_WORKER_FILE_BASENAME + ".js";
17 private projectPath: string;
18 private sourcesStoragePath: string;
19
20 constructor(projectPath: string, sourcesStoragePath: string) {
21 this.projectPath = projectPath;
22 this.sourcesStoragePath = sourcesStoragePath;
23 }
24
25 private isRunning(): Q.Promise<boolean> {
26 let statusURL = `http://${Packager.HOST}/status`;
27
28 return new Request().request(statusURL)
29 .then((body: string) => {
30 return body === "packager-status:running";
31 },
32 (error: any) => {
33 return false;
34 });
35 }
36
37 private awaitStart(retryCount = 30, delay = 2000): Q.Promise<boolean> {
38 let pu: PromiseUtil = new PromiseUtil();
39 return pu.retryAsync(() => this.isRunning(), (running) => running, retryCount, delay, "Could not start the packager.");
40 }
41
42 private downloadDebuggerWorker(): Q.Promise<void> {
43 let debuggerWorkerURL = `http://${Packager.HOST}/${Packager.DEBUGGER_WORKER_FILENAME}`;
44 let debuggerWorkerLocalPath = path.join(this.sourcesStoragePath, Packager.DEBUGGER_WORKER_FILENAME);
45 Log.logInternalMessage("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
46 return new Request().request(debuggerWorkerURL, true).then((body: string) => {
47 return new Node.FileSystem().writeFile(debuggerWorkerLocalPath, body);
48 });
49 }
50
51 public start(): Q.Promise<void> {
52 let resolver = new PlatformResolver();
53 let desktopPlatform = resolver.resolveDesktopPlatform();
54
55 this.isRunning().done(running => {
56 if (!running) {
57 let mandatoryArgs = ["start"];
58 let args = mandatoryArgs.concat(desktopPlatform.reactPackagerExtraParameters);
59 let childEnv = Object.assign({}, process.env, { REACT_DEBUGGER: "echo A debugger is not needed: " });
60
61 // The packager will continue running while we debug the application, so we can"t
62 // wait for this command to finish
63 new CommandExecutor(this.projectPath).spawn(desktopPlatform.reactNativeCommandName, args, { env: childEnv }).done();
64 }
65 });
66
67 return this.awaitStart().then(() => {
68 Log.logMessage("Packager started.");
69 return this.downloadDebuggerWorker();
70 }).then(() => {
71 Log.logMessage("Downloaded debuggerWorker.js (Logic to run the React Native app) from the Packager.");
72 });
73 }
74}
75