microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-peq/upgradeTypeScriptTo7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

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