microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
876df2a0c1922138b98749a03b17d6aebb0d5d7a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/packager.ts

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