microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ca18576f7484d0e88c25abeb465b1a3337b839d9

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/packager.ts

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