microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4abd784953e5fc372c2a69fb34e32c8363aa25a9

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

166lines · 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 * as Q from "q";
5import {ChildProcess} from "child_process";
6import {Log} from "./log/log";
7import {Node} from "./node/node";
8import {ISpawnResult} from "./node/childProcess";
9import {HostPlatform, HostPlatformId} from "../common/hostPlatform";
10import {ErrorHelper} from "./error/errorHelper";
11import {InternalErrorCode} from "./error/internalErrorCode";
12
13interface EnvironmentOptions {
14 REACT_DEBUGGER?: string;
15}
16
17interface Options {
18 env?: EnvironmentOptions;
19}
20
21export enum CommandStatus {
22 Start = 0,
23 End = 1
24}
25
26export class CommandExecutor {
27 private static ReactNativeCommand = "react-native";
28 private currentWorkingDirectory: string;
29
30 constructor(currentWorkingDirectory?: string) {
31 this.currentWorkingDirectory = currentWorkingDirectory;
32 }
33
34 public execute(command: string, options: Options = {}): Q.Promise<void> {
35 Log.logCommandStatus(command, CommandStatus.Start);
36 return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
37 .then(stdout => {
38 Log.logMessage(stdout);
39 Log.logCommandStatus(command, CommandStatus.End);
40 },
41 (reason: Error) =>
42 this.generateRejectionForCommand(command, reason));
43 }
44
45 /**
46 * Spawns a child process with the params passed and returns promise of the spawned ChildProcess
47 * This method does not wait for the spawned process to finish execution
48 * {command} - The command to be invoked in the child process
49 * {args} - Arguments to be passed to the command
50 * {options} - additional options with which the child process needs to be spawned
51 */
52 public spawn(command: string, args: string[], options: Options = {}): ChildProcess {
53 return this.spawnChildProcess(command, args, options).spawnedProcess;
54 }
55
56 /**
57 * Spawns a child process with the params passed
58 * This method waits until the spawned process finishes execution
59 * {command} - The command to be invoked in the child process
60 * {args} - Arguments to be passed to the command
61 * {options} - additional options with which the child process needs to be spawned
62 */
63 public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}): Q.Promise<void> {
64 return this.spawnChildProcess(command, args, options).outcome;
65 }
66
67 /**
68 * Spawns the React Native packager in a child process.
69 */
70 public spawnReactPackager(args?: string[], options: Options = {}): Q.Promise<ChildProcess> {
71 let deferred = Q.defer<ChildProcess>();
72 let command = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
73 let runArguments = ["start"];
74
75 if (args) {
76 runArguments = runArguments.concat(args);
77 }
78
79 let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
80
81 let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions);
82 result.spawnedProcess.once("error", (error: any) => {
83 deferred.reject(ErrorHelper.getNestedError(error, InternalErrorCode.PackagerStartFailed));
84 });
85
86 result.stderr.on("data", (data: Buffer) => {
87 Log.logMessage(data.toString(), /*formatMessage*/false);
88 });
89
90 result.stdout.on("data", (data: Buffer) => {
91 Log.logMessage(data.toString(), /*formatMessage*/false);
92 });
93
94 // TODO #83 - PROMISE: We need to consume result.outcome here
95 Q.delay(300).done(() => deferred.resolve(result.spawnedProcess));
96 return deferred.promise;
97 }
98
99 /**
100 * Kills the React Native packager in a child process.
101 */
102 public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
103 Log.logMessage("Stopping Packager");
104
105 if (packagerProcess) {
106 return Q({}).then(() => {
107 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
108 return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
109 } else {
110 packagerProcess.kill();
111 }
112 }).then(() => {
113 Log.logMessage("Packager stopped");
114 });
115
116 } else {
117 Log.logMessage("Packager not found");
118 return Q.resolve<void>(void 0);
119 }
120 }
121
122 /**
123 * Executes a react native command and waits for its completion.
124 */
125 public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}): Q.Promise<void> {
126 return this.spawnChildReactCommandProcess(command, args, options).outcome;
127 }
128
129 public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}): ISpawnResult {
130 let runArguments = [command];
131 if (args) {
132 runArguments = runArguments.concat(args);
133 }
134
135 let reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
136 return this.spawnChildProcess(reactCommand, runArguments, options);
137 }
138
139 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
140 let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
141 let commandWithArgs = command + " " + args.join(" ");
142
143 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
144 let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions);
145
146 result.stderr.on("data", (data: Buffer) => {
147 Log.logMessage(data.toString(), /*formatMessage*/ false);
148 });
149
150 result.stdout.on("data", (data: Buffer) => {
151 Log.logMessage(data.toString(), /*formatMessage*/ false);
152 });
153
154 result.outcome = result.outcome.then(
155 () =>
156 Log.logCommandStatus(commandWithArgs, CommandStatus.End),
157 reason =>
158 this.generateRejectionForCommand(commandWithArgs, reason));
159
160 return result;
161 }
162
163 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
164 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
165 }
166}
167