microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9f75364d9ab474cb5ea5041b52ac55efb269012e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

213lines · 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
36 private static ReactNativeCommand = "react-native";
37 private static ReactNativeVersionCommand = "-v";
38 private currentWorkingDirectory: string;
39 private childProcess = new Node.ChildProcess();
40
41 constructor(currentWorkingDirectory?: string) {
42 this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
43 }
44
45 public execute(command: string, options: Options = {}): Q.Promise<void> {
46 Log.logCommandStatus(command, CommandStatus.Start);
47 return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
48 .then(stdout => {
49 Log.logMessage(stdout);
50 Log.logCommandStatus(command, CommandStatus.End);
51 },
52 (reason: Error) =>
53 this.generateRejectionForCommand(command, reason));
54 }
55
56 /**
57 * Spawns a child process with the params passed
58 * This method waits until the spawned process finishes execution
59 * {command} - The command to be invoked in the child process
60 * {args} - Arguments to be passed to the command
61 * {options} - additional options with which the child process needs to be spawned
62 */
63 public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
64 return this.spawnChildProcess(command, args, options).outcome;
65 }
66
67 /**
68 * Spawns the React Native packager in a child process.
69 */
70 public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
71 return this.spawnReactCommand("start", args, options);
72 }
73
74 /**
75 * Uses the `react-native -v` command to get the version used on the project.
76 * Returns null if the workspace is not a react native project
77 */
78 public getReactNativeVersion(): Q.Promise<string> {
79 let deferred = Q.defer<string>();
80 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
81 let output = "";
82
83 const result = this.childProcess.spawn(reactCommand,
84 [CommandExecutor.ReactNativeVersionCommand],
85 { cwd: this.currentWorkingDirectory });
86
87 result.stdout.on("data", (data: Buffer) => {
88 output += data.toString();
89 });
90
91 result.stdout.on("end", () => {
92 const match = output.match(/react-native: ([\d\.]+)/);
93 deferred.resolve(match && match[1]);
94 });
95
96 return deferred.promise;
97 }
98
99 /**
100 * Kills the React Native packager in a child process.
101 */
102 public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
103 if (packagerProcess) {
104 return Q({}).then(() => {
105 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
106 return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
107 } else {
108 packagerProcess.kill();
109 }
110 }).then(() => {
111 Log.logMessage("Packager stopped");
112 });
113
114 } else {
115 Log.logMessage("Packager not found");
116 return Q.resolve<void>(void 0);
117 }
118 }
119
120 /**
121 * Executes a react native command and waits for its completion.
122 */
123 public spawnReactCommand(command: string, args: string[] = [], options: Options = {}): ISpawnResult {
124 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
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(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
136 let deferred = Q.defer<void>();
137 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
138 const commandWithArgs = command + " " + args.join(" ");
139 const timeBetweenDots = 1500;
140 let lastDotTime = 0;
141
142 const printDot = () => {
143 const now = Date.now();
144 if (now - lastDotTime > timeBetweenDots) {
145 lastDotTime = now;
146 Log.logString(".");
147 }
148 };
149
150 if (options.verbosity === CommandVerbosity.OUTPUT) {
151 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
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 Log.logStreamData(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 Log.logStreamData(data, process.stderr);
167 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
168 printDot();
169 }
170 });
171
172 result.outcome = result.outcome.then(
173 () => {
174 if (options.verbosity === CommandVerbosity.OUTPUT) {
175 Log.logCommandStatus(commandWithArgs, CommandStatus.End);
176 }
177 Log.logString("\n");
178 deferred.resolve(void 0);
179 },
180 reason => {
181 deferred.reject(reason);
182 return this.generateRejectionForCommand(commandWithArgs, reason);
183 });
184 return deferred.promise;
185 }
186
187 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
188 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
189 const commandWithArgs = command + " " + args.join(" ");
190
191 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
192 const result = this.childProcess.spawn(command, args, spawnOptions);
193
194 result.stderr.on("data", (data: Buffer) => {
195 Log.logStreamData(data, process.stderr);
196 });
197
198 result.stdout.on("data", (data: Buffer) => {
199 Log.logStreamData(data, process.stdout);
200 });
201
202 result.outcome = result.outcome.then(
203 () =>
204 Log.logCommandStatus(commandWithArgs, CommandStatus.End),
205 reason =>
206 this.generateRejectionForCommand(commandWithArgs, reason));
207 return result;
208 }
209
210 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
211 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
212 }
213}
214