microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e574eae01cbbbc457f1add5e8e23d40ea0549275

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 * as path from "path";
6import {ChildProcess} from "child_process";
7import {ILogger} from "../extension/log/LogHelper";
8import {NullLogger} from "../extension/log/NullLogger";
9import {ReactNativeProjectHelper} from "../common/reactNativeProjectHelper";
10import {Node} from "./node/node";
11import {ISpawnResult} from "./node/childProcess";
12import {HostPlatform, HostPlatformId} from "./hostPlatform";
13import {ErrorHelper} from "./error/errorHelper";
14import {InternalErrorCode} from "./error/internalErrorCode";
15import * as nls from "vscode-nls";
16const localize = nls.loadMessageBundle();
17
18export enum CommandVerbosity {
19 OUTPUT,
20 SILENT,
21 PROGRESS,
22}
23
24interface EnvironmentOptions {
25 REACT_DEBUGGER?: string;
26}
27
28interface Options {
29 env?: EnvironmentOptions;
30 verbosity?: CommandVerbosity;
31 cwd?: string;
32}
33
34export enum CommandStatus {
35 Start = 0,
36 End = 1,
37}
38
39export class CommandExecutor {
40
41 public static ReactNativeCommand: string | null;
42 private childProcess = new Node.ChildProcess();
43
44 constructor(
45 private currentWorkingDirectory: string = process.cwd(),
46 private logger: ILogger = new NullLogger()
47 ) { }
48
49 public execute(command: string, options: Options = {}): Q.Promise<void> {
50 this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start));
51 return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
52 .then(stdout => {
53 this.logger.info(stdout);
54 this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End));
55 },
56 (reason: Error) =>
57 this.generateRejectionForCommand(command, reason));
58 }
59
60 /**
61 * Spawns a child process with the params passed
62 * This method waits until the spawned process finishes execution
63 * {command} - The command to be invoked in the child process
64 * {args} - Arguments to be passed to the command
65 * {options} - additional options with which the child process needs to be spawned
66 */
67 public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
68 return this.spawnChildProcess(command, args, options).outcome;
69 }
70
71 /**
72 * Spawns the React Native packager in a child process.
73 */
74 public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
75 return this.spawnReactCommand("start", args, options);
76 }
77
78 public getReactNativeVersion(): Q.Promise<string> {
79 return ReactNativeProjectHelper.getReactNativeVersions(this.currentWorkingDirectory)
80 .then(versions => versions.reactNativeVersion);
81 }
82
83 /**
84 * Kills the React Native packager in a child process.
85 */
86 public killReactPackager(packagerProcess?: ChildProcess): Q.Promise<void> {
87 if (packagerProcess) {
88 return Q({}).then(() => {
89 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
90 return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
91 } else {
92 packagerProcess.kill();
93 return Q.resolve(void 0);
94 }
95 }).then(() => {
96 this.logger.info(localize("PackagerStopped", "Packager stopped"));
97 });
98
99 } else {
100 this.logger.warning(localize("PackagerNotFound", "Packager not found"));
101 return Q.resolve<void>(void 0);
102 }
103 }
104
105 /**
106 * Executes a react native command and waits for its completion.
107 */
108 public spawnReactCommand(command: string, args: string[] = [], options: Options = {}): ISpawnResult {
109 const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI());
110 return this.spawnChildProcess(reactCommand, [command, ...args], options);
111 }
112
113 /**
114 * Spawns a child process with the params passed
115 * This method has logic to do while the command is executing
116 * {command} - The command to be invoked in the child process
117 * {args} - Arguments to be passed to the command
118 * {options} - additional options with which the child process needs to be spawned
119 */
120 public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
121 let deferred = Q.defer<void>();
122 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
123 const commandWithArgs = command + " " + args.join(" ");
124 const timeBetweenDots = 1500;
125 let lastDotTime = 0;
126
127 const printDot = () => {
128 const now = Date.now();
129 if (now - lastDotTime > timeBetweenDots) {
130 lastDotTime = now;
131 this.logger.logStream(".", process.stdout);
132 }
133 };
134
135 if (options.verbosity === CommandVerbosity.OUTPUT) {
136 this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start));
137 }
138
139 const result = this.childProcess.spawn(command, args, spawnOptions);
140
141 result.stdout.on("data", (data: Buffer) => {
142 if (options.verbosity === CommandVerbosity.OUTPUT) {
143 this.logger.logStream(data, process.stdout);
144 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
145 printDot();
146 }
147 });
148
149 result.stderr.on("data", (data: Buffer) => {
150 if (options.verbosity === CommandVerbosity.OUTPUT) {
151 this.logger.logStream(data, process.stderr);
152 } else if (options.verbosity === CommandVerbosity.PROGRESS) {
153 printDot();
154 }
155 });
156
157 result.outcome = result.outcome.then(
158 () => {
159 if (options.verbosity === CommandVerbosity.OUTPUT) {
160 this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End));
161 }
162 this.logger.logStream("\n", process.stdout);
163 deferred.resolve(void 0);
164 },
165 reason => {
166 deferred.reject(reason);
167 return this.generateRejectionForCommand(commandWithArgs, reason);
168 });
169 return deferred.promise;
170 }
171
172 public selectReactNativeCLI(): string {
173 return CommandExecutor.ReactNativeCommand || path.resolve(this.currentWorkingDirectory, "node_modules", ".bin", "react-native");
174 }
175
176 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
177 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
178 const commandWithArgs = command + " " + args.join(" ");
179
180 this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start));
181 const result = this.childProcess.spawn(command, args, spawnOptions);
182
183 result.stderr.on("data", (data: Buffer) => {
184 this.logger.logStream(data, process.stderr);
185 });
186
187 result.stdout.on("data", (data: Buffer) => {
188 this.logger.logStream(data, process.stdout);
189 });
190
191 result.outcome = result.outcome.then(
192 () =>
193 this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End)),
194 reason =>
195 this.generateRejectionForCommand(commandWithArgs, reason));
196 return result;
197 }
198
199 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
200 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
201 }
202
203 private static getCommandStatusString(command: string, status: CommandStatus) {
204 switch (status) {
205 case CommandStatus.Start:
206 return `Executing command: ${command}`;
207 case CommandStatus.End:
208 return `Finished executing: ${command}`;
209 default:
210 throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus);
211 }
212 }
213}
214