microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0904a0d7ddf15d888e1d494ac66aa4f27e49aefb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/packager.ts

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