microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
52bf0a75506c1b3b1cecc9ad89937e596d1bc22a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/packager.ts

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