microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b46c1dea2f408007fb3c3ee6cc537d5f85fb0b2c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

228lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
10873e11digeff10 years ago4import * as Q from "q";
2e15926eMeena Kunnathur Balakrishnan10 years ago5import {ChildProcess} from "child_process";
0a68f8dbArtem Egorov8 years ago6import {ILogger} from "../extension/log/LogHelper";
7import { NullLogger } from "../extension/log/NullLogger";
b0061ac6Meena Kunnathur Balakrishnan10 years ago8import {Node} from "./node/node";
cb6d0922digeff10 years ago9import {ISpawnResult} from "./node/childProcess";
5c8365a6Artem Egorov8 years ago10import {HostPlatform, HostPlatformId} from "./hostPlatform";
190e393cMeena Kunnathur Balakrishnan10 years ago11import {ErrorHelper} from "./error/errorHelper";
12import {InternalErrorCode} from "./error/internalErrorCode";
e2644f37Yuri Skorokhodov7 years ago13import * as nls from "vscode-nls";
14const localize = nls.loadMessageBundle();
3fb37ad5unknown10 years ago15
831f4a85Patricio Beltran9 years ago16export enum CommandVerbosity {
17OUTPUT,
18SILENT,
19PROGRESS,
20}
21
3fb37ad5unknown10 years ago22interface EnvironmentOptions {
23REACT_DEBUGGER?: string;
24}
25
26interface Options {
27env?: EnvironmentOptions;
831f4a85Patricio Beltran9 years ago28verbosity?: CommandVerbosity;
0d827d9bJimmy Thomson9 years ago29cwd?: string;
3fb37ad5unknown10 years ago30}
31
17161993Meena Kunnathur Balakrishnan10 years ago32export enum CommandStatus {
33Start = 0,
27710197Vladimir Kotikov8 years ago34End = 1,
17161993Meena Kunnathur Balakrishnan10 years ago35}
36
3fb37ad5unknown10 years ago37export class CommandExecutor {
94cd5149Artem Egorov8 years ago38
bbdbbb3bdlebu10 years ago39private static ReactNativeCommand = "react-native";
229e4d51Ruslan Bikkinin6 years ago40private static ReactNativeVersionCommand = "--version";
9596aa53digeff10 years ago41private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago42
0a68f8dbArtem Egorov8 years ago43constructor(
44private currentWorkingDirectory: string = process.cwd(),
45private logger: ILogger = new NullLogger()
46) { }
3fb37ad5unknown10 years ago47
fb2bae06Daniel10 years ago48public execute(command: string, options: Options = {}): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago49this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start));
9596aa53digeff10 years ago50return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago51.then(stdout => {
0a68f8dbArtem Egorov8 years ago52this.logger.info(stdout);
53this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End));
3fb37ad5unknown10 years ago54},
cb6d0922digeff10 years ago55(reason: Error) =>
56this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago57}
58
45944d15Meena Kunnathur Balakrishnan10 years ago59/**
60* Spawns a child process with the params passed
61* This method waits until the spawned process finishes execution
62* {command} - The command to be invoked in the child process
63* {args} - Arguments to be passed to the command
64* {options} - additional options with which the child process needs to be spawned
65*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago66public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
9596aa53digeff10 years ago67return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago68}
69
6126d899Meena Kunnathur Balakrishnan10 years ago70/**
71* Spawns the React Native packager in a child process.
72*/
9596aa53digeff10 years ago73public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
74return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago75}
76
b8a56999Patricio Beltran9 years ago77/**
229e4d51Ruslan Bikkinin6 years ago78* Uses the `react-native --version` command to get the version used on the project.
b8a56999Patricio Beltran9 years ago79* Returns null if the workspace is not a react native project
80*/
81public getReactNativeVersion(): Q.Promise<string> {
82let deferred = Q.defer<string>();
83const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
84let output = "";
85
86const result = this.childProcess.spawn(reactCommand,
87[CommandExecutor.ReactNativeVersionCommand],
88{ cwd: this.currentWorkingDirectory });
89
90result.stdout.on("data", (data: Buffer) => {
91output += data.toString();
92});
93
94result.stdout.on("end", () => {
95const match = output.match(/react-native: ([\d\.]+)/);
5c8365a6Artem Egorov8 years ago96deferred.resolve(match && match[1] || "");
b8a56999Patricio Beltran9 years ago97});
98
99return deferred.promise;
100}
101
c3a987a7Meena Kunnathur Balakrishnan10 years ago102/**
103* Kills the React Native packager in a child process.
104*/
5c8365a6Artem Egorov8 years ago105public killReactPackager(packagerProcess?: ChildProcess): Q.Promise<void> {
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago106if (packagerProcess) {
a1005420dlebu10 years ago107return Q({}).then(() => {
108if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
9596aa53digeff10 years ago109return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
a1005420dlebu10 years ago110} else {
111packagerProcess.kill();
5c8365a6Artem Egorov8 years ago112return Q.resolve(void 0);
a1005420dlebu10 years ago113}
114}).then(() => {
e2644f37Yuri Skorokhodov7 years ago115this.logger.info(localize("PackagerStopped", "Packager stopped"));
a1005420dlebu10 years ago116});
117
c3a987a7Meena Kunnathur Balakrishnan10 years ago118} else {
e2644f37Yuri Skorokhodov7 years ago119this.logger.warning(localize("PackagerNotFound", "Packager not found"));
10873e11digeff10 years ago120return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago121}
122}
123
f8d32439dlebu10 years ago124/**
125* Executes a react native command and waits for its completion.
126*/
e390ca0aVladimir Kotikov8 years ago127public spawnReactCommand(command: string, args: string[] = [], options: Options = {}): ISpawnResult {
9596aa53digeff10 years ago128const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
94cd5149Artem Egorov8 years ago129return this.spawnChildProcess(reactCommand, [command, ...args], options);
5b0582f3digeff10 years ago130}
131
831f4a85Patricio Beltran9 years ago132/**
133* Spawns a child process with the params passed
134* This method has logic to do while the command is executing
135* {command} - The command to be invoked in the child process
136* {args} - Arguments to be passed to the command
137* {options} - additional options with which the child process needs to be spawned
138*/
139public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
140let deferred = Q.defer<void>();
141const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
142const commandWithArgs = command + " " + args.join(" ");
143const timeBetweenDots = 1500;
b0af599cJimmy Thomson9 years ago144let lastDotTime = 0;
831f4a85Patricio Beltran9 years ago145
146const printDot = () => {
b0af599cJimmy Thomson9 years ago147const now = Date.now();
148if (now - lastDotTime > timeBetweenDots) {
149lastDotTime = now;
0a68f8dbArtem Egorov8 years ago150this.logger.logStream(".", process.stdout);
b0af599cJimmy Thomson9 years ago151}
831f4a85Patricio Beltran9 years ago152};
153
154if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago155this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start));
831f4a85Patricio Beltran9 years ago156}
157
158const result = this.childProcess.spawn(command, args, spawnOptions);
159
160result.stdout.on("data", (data: Buffer) => {
161if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago162this.logger.logStream(data, process.stdout);
b0af599cJimmy Thomson9 years ago163} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago164printDot();
165}
166});
167
168result.stderr.on("data", (data: Buffer) => {
169if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago170this.logger.logStream(data, process.stderr);
b0af599cJimmy Thomson9 years ago171} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago172printDot();
173}
174});
175
176result.outcome = result.outcome.then(
177() => {
178if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago179this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End));
831f4a85Patricio Beltran9 years ago180}
0a68f8dbArtem Egorov8 years ago181this.logger.logStream("\n", process.stdout);
831f4a85Patricio Beltran9 years ago182deferred.resolve(void 0);
183},
184reason => {
0d827d9bJimmy Thomson9 years ago185deferred.reject(reason);
831f4a85Patricio Beltran9 years ago186return this.generateRejectionForCommand(commandWithArgs, reason);
187});
188return deferred.promise;
189}
190
9596aa53digeff10 years ago191private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
192const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
193const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago194
0a68f8dbArtem Egorov8 years ago195this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start));
9596aa53digeff10 years ago196const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago197
198result.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago199this.logger.logStream(data, process.stderr);
3fb37ad5unknown10 years ago200});
201
202result.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago203this.logger.logStream(data, process.stdout);
3fb37ad5unknown10 years ago204});
205
10873e11digeff10 years ago206result.outcome = result.outcome.then(
9596aa53digeff10 years ago207() =>
0a68f8dbArtem Egorov8 years ago208this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End)),
10873e11digeff10 years ago209reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago210this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago211return result;
3fb37ad5unknown10 years ago212}
10873e11digeff10 years ago213
3677173cdigeff10 years ago214private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago215return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago216}
0a68f8dbArtem Egorov8 years ago217
218private static getCommandStatusString(command: string, status: CommandStatus) {
219switch (status) {
220case CommandStatus.Start:
fc602bb6Yuri Skorokhodov7 years ago221return `Executing command: ${command}`;
0a68f8dbArtem Egorov8 years ago222case CommandStatus.End:
fc602bb6Yuri Skorokhodov7 years ago223return `Finished executing: ${command}`;
0a68f8dbArtem Egorov8 years ago224default:
fc602bb6Yuri Skorokhodov7 years ago225throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus);
0a68f8dbArtem Egorov8 years ago226}
227}
3fb37ad5unknown10 years ago228}