microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70f7cae4a697f9868d22dfdd08089a2cd2076772

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

216lines · 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 let lastDotTime = 0;
140
141 const printDot = () => {
142 const now = Date.now();
143 if (now - lastDotTime > timeBetweenDots) {
144 lastDotTime = now;
145 Log.logString(".");
146 }
147 };
148
149 if (options.verbosity === CommandVerbosity.OUTPUT) {
150 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
151 }
152
153 const result = this.childProcess.spawn(command, args, spawnOptions);
154
155 result.stdout.on("data", (data: Buffer) => {
156 if (options.verbosity === CommandVerbosity.OUTPUT) {
157 Log.logStreamData(data, process.stdout);
158 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
159 printDot();
160 }
161 });
162
163 result.stderr.on("data", (data: Buffer) => {
164 if (options.verbosity === CommandVerbosity.OUTPUT) {
165 Log.logStreamData(data, process.stderr);
166 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
167 printDot();
168 }
169 });
170
171 result.outcome = result.outcome.then(
172 () => {
173 if (options.verbosity === CommandVerbosity.OUTPUT) {
174 Log.logCommandStatus(commandWithArgs, CommandStatus.End);
175 }
176 Log.logString("\n");
177 deferred.resolve(void 0);
178 },
179 reason => {
180 deferred.reject(reason);
181 return this.generateRejectionForCommand(commandWithArgs, reason);
182 });
183 return deferred.promise;
184 }
185
186 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
187 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
188 const commandWithArgs = command + " " + args.join(" ");
189
190 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
191 const result = this.childProcess.spawn(command, args, spawnOptions);
192
193 result.stderr.on("data", (data: Buffer) => {
194 Log.logStreamData(data, process.stderr);
195 });
196
197 result.stdout.on("data", (data: Buffer) => {
198 Log.logStreamData(data, process.stdout);
199 });
200
201 result.outcome = result.outcome.then(
202 () =>
203 Log.logCommandStatus(commandWithArgs, CommandStatus.End),
204 reason =>
205 this.generateRejectionForCommand(commandWithArgs, reason));
206 return result;
207 }
208
209 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
210 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
211 }
212
213 private combineArguments(firstArgument: string, otherArguments: string[] = []) {
214 return [firstArgument].concat(otherArguments);
215 }
216}
217