microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

241lines · 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 /**
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 async getReactNativeVersion(): Promise<string> {
86 const versions = await ProjectVersionHelper.getReactNativeVersions(
87 this.currentWorkingDirectory,
88 );
89 return versions.reactNativeVersion;
90 }
91
92 /**
93 * Kills the React Native packager in a child process.
94 */
95 public async killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> {
96 if (packagerProcess) {
97 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
98 const res = await this.childProcess.exec(
99 `taskkill /pid ${packagerProcess.pid} /T /F`,
100 );
101 await res.outcome;
102 } else {
103 packagerProcess.kill();
104 }
105 this.logger.info(localize("PackagerStopped", "Packager stopped"));
106 } else {
107 this.logger.warning(localize("PackagerNotFound", "Packager not found"));
108 }
109 }
110
111 /**
112 * Executes a react native command and waits for its completion.
113 */
114 public spawnReactCommand(
115 command: string,
116 args: string[] = [],
117 options: Options = {},
118 ): ISpawnResult {
119 const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI());
120 return this.spawnChildProcess(reactCommand, [command, ...args], options);
121 }
122
123 /**
124 * Spawns a child process with the params passed
125 * This method has logic to do while the command is executing
126 * {command} - The command to be invoked in the child process
127 * {args} - Arguments to be passed to the command
128 * {options} - additional options with which the child process needs to be spawned
129 */
130 public async spawnWithProgress(
131 command: string,
132 args: string[],
133 options: Options = { verbosity: CommandVerbosity.OUTPUT },
134 ): Promise<void> {
135 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
136 const commandWithArgs = `${command} ${args.join(" ")}`;
137 const timeBetweenDots = 1500;
138 let lastDotTime = 0;
139
140 const printDot = () => {
141 const now = Date.now();
142 if (now - lastDotTime > timeBetweenDots) {
143 lastDotTime = now;
144 this.logger.logStream(".", process.stdout);
145 }
146 };
147
148 if (options.verbosity === CommandVerbosity.OUTPUT) {
149 this.logger.debug(
150 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
151 );
152 }
153
154 const result = this.childProcess.spawn(command, args, spawnOptions);
155
156 result.stdout.on("data", (data: Buffer) => {
157 if (options.verbosity === CommandVerbosity.OUTPUT) {
158 this.logger.logStream(data, process.stdout);
159 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
160 printDot();
161 }
162 });
163
164 result.stderr.on("data", (data: Buffer) => {
165 if (options.verbosity === CommandVerbosity.OUTPUT) {
166 this.logger.logStream(data, process.stderr);
167 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
168 printDot();
169 }
170 });
171
172 try {
173 await result.outcome;
174 if (options.verbosity === CommandVerbosity.OUTPUT) {
175 this.logger.debug(
176 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End),
177 );
178 }
179 this.logger.logStream("\n", process.stdout);
180 } catch (reason) {
181 return this.generateRejectionForCommand(commandWithArgs, reason);
182 }
183 }
184
185 public selectReactNativeCLI(): string {
186 return (
187 CommandExecutor.ReactNativeCommand ||
188 path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native")
189 );
190 }
191
192 private spawnChildProcess(
193 command: string,
194 args: string[],
195 options: Options = {},
196 ): ISpawnResult {
197 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
198 const commandWithArgs = `${command} ${args.join(" ")}`;
199
200 this.logger.debug(
201 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
202 );
203 const result = this.childProcess.spawn(command, args, spawnOptions);
204
205 result.stderr.on("data", (data: Buffer) => {
206 this.logger.logStream(data, process.stderr);
207 });
208
209 result.stdout.on("data", (data: Buffer) => {
210 this.logger.logStream(data, process.stdout);
211 });
212
213 result.outcome = result.outcome.then(
214 () =>
215 this.logger.debug(
216 CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End),
217 ),
218 reason => this.generateRejectionForCommand(commandWithArgs, reason),
219 );
220 return result;
221 }
222
223 private generateRejectionForCommand(command: string, reason: any): Promise<void> {
224 return Promise.reject<void>(
225 reason.errorCode === InternalErrorCode.CommandFailed
226 ? reason
227 : ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command),
228 );
229 }
230
231 private static getCommandStatusString(command: string, status: CommandStatus) {
232 switch (status) {
233 case CommandStatus.Start:
234 return `Executing command: ${command}`;
235 case CommandStatus.End:
236 return `Finished executing: ${command}`;
237 default:
238 throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus);
239 }
240 }
241}