microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3314a820561f672a145a365db5c47896e6286fa2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/packager.ts

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