microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1c2424f4d9af42a0cefcacb0fcb9670b5aa292cd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

255lines · 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 path from "path";
5import * as cp from "child_process";
6import { ILogger } from "../extension/log/LogHelper";
7import { NullLogger } from "../extension/log/NullLogger";
8import { ProjectVersionHelper } from "../common/projectVersionHelper";
9import { ISpawnResult } from "./node/childProcess";
10import { HostPlatform, HostPlatformId } from "./hostPlatform";
11import { ErrorHelper } from "./error/errorHelper";
12import { InternalErrorCode } from "./error/internalErrorCode";
13import * as nls from "vscode-nls";
14import { Node } from "./node/node";
15nls.config({
16 messageFormat: nls.MessageFormat.bundle,
17 bundleFormat: nls.BundleFormat.standalone,
18})();
19const localize = nls.loadMessageBundle();
20
21export enum CommandVerbosity {
22 OUTPUT,
23 SILENT,
24 PROGRESS,
25}
26
27interface EnvironmentOptions {
28 REACT_DEBUGGER?: string;
29}
30
31interface Options {
32 env?: EnvironmentOptions;
33 verbosity?: CommandVerbosity;
34 cwd?: string;
35}
36
37export enum CommandStatus {
38 Start = 0,
39 End = 1,
40}
41
42export class CommandExecutor {
43 public static ReactNativeCommand: string | null;
44 private childProcess = new Node.ChildProcess();
45
46 constructor(
47 private nodeModulesRoot: string,
48 private currentWorkingDirectory: string = process.cwd(),
49 private logger: ILogger = new NullLogger(),
50 ) {}
51
52 public execute(command: string, options: Options = {}): Promise<void> {
53 this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start));
54 return this.childProcess
55 .execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
56 .then(
57 stdout => {
58 this.logger.info(stdout);
59 this.logger.debug(
60 CommandExecutor.getCommandStatusString(command, CommandStatus.End),
61 );
62 },
63 (reason: Error) => this.generateRejectionForCommand(command, reason),
64 );
65 }
66
67 /**
68 * Spawns a child process with the params passed
69 * This method waits until the spawned process finishes execution
70 * {command} - The command to be invoked in the child process
71 * {args} - Arguments to be passed to the command
72 * {options} - additional options with which the child process needs to be spawned
73 */
74 public spawn(command: string, args: string[], options: Options = {}): Promise<any> {
75 return this.spawnChildProcess(command, args, options).outcome;
76 }
77
78 /**
79 * Spawns the React Native packager in a child process.
80 */
81 public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
82 return this.spawnReactCommand("start", args, options);
83 }
84
85 public getReactNativeVersion(): Promise<string> {
86 return ProjectVersionHelper.getReactNativeVersions(this.currentWorkingDirectory).then(
87 versions => versions.reactNativeVersion,
88 );
89 }
90
91 /**
92 * Kills the React Native packager in a child process.
93 */
94 public killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> {
95 if (packagerProcess) {
96 return new Promise(resolve => {
97 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
98 return resolve(
99 this.childProcess
100 .exec("taskkill /pid " + packagerProcess.pid + " /T /F")
101 .then(res => res.outcome),
102 );
103 } else {
104 packagerProcess.kill();
105 return resolve(void 0);
106 }
107 }).then(() => {
108 this.logger.info(localize("PackagerStopped", "Packager stopped"));
109 });
110 } else {
111 this.logger.warning(localize("PackagerNotFound", "Packager not found"));
112 return Promise.resolve();
113 }
114 }
115
116 /**
117 * Executes a react native command and waits for its completion.
118 */
119 public spawnReactCommand(
120 command: string,
121 args: string[] = [],
122 options: Options = {},
123 ): ISpawnResult {
124 const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI());
125 return this.spawnChildProcess(reactCommand, [command, ...args], options);
126 }
127
128 /**
129 * Spawns a child process with the params passed
130 * This method has logic to do while the command is executing
131 * {command} - The command to be invoked in the child process
132 * {args} - Arguments to be passed to the command
133 * {options} - additional options with which the child process needs to be spawned
134 */
135 public spawnWithProgress(
136 command: string,
137 args: string[],
138 options: Options = { verbosity: CommandVerbosity.OUTPUT },
139 ): Promise<void> {
140 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
141 const commandWithArgs = command + " " + args.join(" ");
142 const timeBetweenDots = 1500;
143 let lastDotTime = 0;
144
145 const printDot = () => {
146 const now = Date.now();
147 if (now - lastDotTime > timeBetweenDots) {
148 lastDotTime = now;
149 this.logger.logStream(".", process.stdout);
150 }
151 };
152
153 if (options.verbosity === CommandVerbosity.OUTPUT) {
154 this.logger.debug(
155 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
156 );
157 }
158
159 const result = this.childProcess.spawn(command, args, spawnOptions);
160
161 result.stdout.on("data", (data: Buffer) => {
162 if (options.verbosity === CommandVerbosity.OUTPUT) {
163 this.logger.logStream(data, process.stdout);
164 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
165 printDot();
166 }
167 });
168
169 result.stderr.on("data", (data: Buffer) => {
170 if (options.verbosity === CommandVerbosity.OUTPUT) {
171 this.logger.logStream(data, process.stderr);
172 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
173 printDot();
174 }
175 });
176
177 return new Promise((resolve, reject) => {
178 result.outcome = result.outcome.then(
179 () => {
180 if (options.verbosity === CommandVerbosity.OUTPUT) {
181 this.logger.debug(
182 CommandExecutor.getCommandStatusString(
183 commandWithArgs,
184 CommandStatus.End,
185 ),
186 );
187 }
188 this.logger.logStream("\n", process.stdout);
189 resolve();
190 },
191 reason => {
192 reject(reason);
193 return this.generateRejectionForCommand(commandWithArgs, reason);
194 },
195 );
196 });
197 }
198
199 public selectReactNativeCLI(): string {
200 return (
201 CommandExecutor.ReactNativeCommand ||
202 path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native")
203 );
204 }
205
206 private spawnChildProcess(
207 command: string,
208 args: string[],
209 options: Options = {},
210 ): ISpawnResult {
211 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
212 const commandWithArgs = command + " " + args.join(" ");
213
214 this.logger.debug(
215 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
216 );
217 const result = this.childProcess.spawn(command, args, spawnOptions);
218
219 result.stderr.on("data", (data: Buffer) => {
220 this.logger.logStream(data, process.stderr);
221 });
222
223 result.stdout.on("data", (data: Buffer) => {
224 this.logger.logStream(data, process.stdout);
225 });
226
227 result.outcome = result.outcome.then(
228 () =>
229 this.logger.debug(
230 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End),
231 ),
232 reason => this.generateRejectionForCommand(commandWithArgs, reason),
233 );
234 return result;
235 }
236
237 private generateRejectionForCommand(command: string, reason: any): Promise<void> {
238 return Promise.reject<void>(
239 reason.errorCode === InternalErrorCode.CommandFailed
240 ? reason
241 : ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command),
242 );
243 }
244
245 private static getCommandStatusString(command: string, status: CommandStatus) {
246 switch (status) {
247 case CommandStatus.Start:
248 return `Executing command: ${command}`;
249 case CommandStatus.End:
250 return `Finished executing: ${command}`;
251 default:
252 throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus);
253 }
254 }
255}
256