microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0d827d9b424481979deb5f868a141d64c307491b

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

222lines · 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
13export enum CommandVerbosity {
14 OUTPUT,
15 SILENT,
16 PROGRESS,
17}
18
19interface EnvironmentOptions {
20 REACT_DEBUGGER?: string;
21}
22
23interface Options {
24 env?: EnvironmentOptions;
25 verbosity?: CommandVerbosity;
26 cwd?: string;
27}
28
29export enum CommandStatus {
30 Start = 0,
31 End = 1
32}
33
34export class CommandExecutor {
35 private static ReactNativeCommand = "react-native";
36 private static ReactNativeVersionCommand = "-v";
37 private currentWorkingDirectory: string;
38 private childProcess = new Node.ChildProcess();
39
40 constructor(currentWorkingDirectory?: string) {
41 this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
42 }
43
44 public execute(command: string, options: Options = {}): Q.Promise<void> {
45 Log.logCommandStatus(command, CommandStatus.Start);
46 return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
47 .then(stdout => {
48 Log.logMessage(stdout);
49 Log.logCommandStatus(command, CommandStatus.End);
50 },
51 (reason: Error) =>
52 this.generateRejectionForCommand(command, reason));
53 }
54
55 /**
56 * Spawns a child process with the params passed
57 * This method waits until the spawned process finishes execution
58 * {command} - The command to be invoked in the child process
59 * {args} - Arguments to be passed to the command
60 * {options} - additional options with which the child process needs to be spawned
61 */
62 public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
63 return this.spawnChildProcess(command, args, options).outcome;
64 }
65
66 /**
67 * Spawns the React Native packager in a child process.
68 */
69 public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
70 return this.spawnReactCommand("start", args, options);
71 }
72
73 /**
74 * Uses the `react-native -v` command to get the version used on the project.
75 * Returns null if the workspace is not a react native project
76 */
77 public getReactNativeVersion(): Q.Promise<string> {
78 let deferred = Q.defer<string>();
79 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
80 let output = "";
81
82 const result = this.childProcess.spawn(reactCommand,
83 [CommandExecutor.ReactNativeVersionCommand],
84 { cwd: this.currentWorkingDirectory });
85
86 result.stdout.on("data", (data: Buffer) => {
87 output += data.toString();
88 });
89
90 result.stdout.on("end", () => {
91 const match = output.match(/react-native: ([\d\.]+)/);
92 deferred.resolve(match && match[1]);
93 });
94
95 return deferred.promise;
96 }
97
98 /**
99 * Kills the React Native packager in a child process.
100 */
101 public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
102 if (packagerProcess) {
103 return Q({}).then(() => {
104 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
105 return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
106 } else {
107 packagerProcess.kill();
108 }
109 }).then(() => {
110 Log.logMessage("Packager stopped");
111 });
112
113 } else {
114 Log.logMessage("Packager not found");
115 return Q.resolve<void>(void 0);
116 }
117 }
118
119 /**
120 * Executes a react native command and waits for its completion.
121 */
122 public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult {
123 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
124 return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options);
125 }
126
127 /**
128 * Spawns a child process with the params passed
129 * This method has logic to do while the command is executing
130 * {command} - The command to be invoked in the child process
131 * {args} - Arguments to be passed to the command
132 * {options} - additional options with which the child process needs to be spawned
133 */
134 public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
135 let deferred = Q.defer<void>();
136 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
137 const commandWithArgs = command + " " + args.join(" ");
138 const timeBetweenDots = 1500;
139
140 let dotPrinted = false;
141 let printingProgressTimer: NodeJS.Timer;
142
143 const printDot = () => {
144 Log.logString(".");
145 dotPrinted = true;
146 clearInterval(printingProgressTimer);
147 printingProgressTimer = setInterval(() => {
148 dotPrinted = false;
149 }, timeBetweenDots);
150 };
151
152 if (options.verbosity === CommandVerbosity.OUTPUT) {
153 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
154 }
155
156 const result = this.childProcess.spawn(command, args, spawnOptions);
157 printDot();
158
159 result.stdout.on("data", (data: Buffer) => {
160 if (options.verbosity === CommandVerbosity.OUTPUT) {
161 Log.logStreamData(data, process.stdout);
162 } else if (options.verbosity === CommandVerbosity.PROGRESS && !dotPrinted) {
163 printDot();
164 }
165 });
166
167 result.stderr.on("data", (data: Buffer) => {
168 if (options.verbosity === CommandVerbosity.OUTPUT) {
169 Log.logStreamData(data, process.stderr);
170 } else if (options.verbosity === CommandVerbosity.PROGRESS && !dotPrinted) {
171 printDot();
172 }
173 });
174
175 result.outcome = result.outcome.then(
176 () => {
177 if (options.verbosity === CommandVerbosity.OUTPUT) {
178 Log.logCommandStatus(commandWithArgs, CommandStatus.End);
179 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
180 clearInterval(printingProgressTimer);
181 }
182 Log.logString("\n");
183 deferred.resolve(void 0);
184 },
185 reason => {
186 deferred.reject(reason);
187 return this.generateRejectionForCommand(commandWithArgs, reason);
188 });
189 return deferred.promise;
190 }
191
192 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
193 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
194 const commandWithArgs = command + " " + args.join(" ");
195
196 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
197 const result = this.childProcess.spawn(command, args, spawnOptions);
198
199 result.stderr.on("data", (data: Buffer) => {
200 Log.logStreamData(data, process.stderr);
201 });
202
203 result.stdout.on("data", (data: Buffer) => {
204 Log.logStreamData(data, process.stdout);
205 });
206
207 result.outcome = result.outcome.then(
208 () =>
209 Log.logCommandStatus(commandWithArgs, CommandStatus.End),
210 reason =>
211 this.generateRejectionForCommand(commandWithArgs, reason));
212 return result;
213 }
214
215 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
216 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
217 }
218
219 private combineArguments(firstArgument: string, otherArguments: string[] = []) {
220 return [firstArgument].concat(otherArguments);
221 }
222}
223