microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.16

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";
3fb37ad5unknown10 years ago13
831f4a85Patricio Beltran9 years ago14export enum CommandVerbosity {
15OUTPUT,
16SILENT,
17PROGRESS,
18}
19
3fb37ad5unknown10 years ago20interface EnvironmentOptions {
21REACT_DEBUGGER?: string;
22}
23
24interface Options {
25env?: EnvironmentOptions;
831f4a85Patricio Beltran9 years ago26verbosity?: CommandVerbosity;
0d827d9bJimmy Thomson9 years ago27cwd?: string;
3fb37ad5unknown10 years ago28}
29
17161993Meena Kunnathur Balakrishnan10 years ago30export enum CommandStatus {
31Start = 0,
27710197Vladimir Kotikov8 years ago32End = 1,
17161993Meena Kunnathur Balakrishnan10 years ago33}
34
3fb37ad5unknown10 years ago35export class CommandExecutor {
94cd5149Artem Egorov8 years ago36
bbdbbb3bdlebu10 years ago37private static ReactNativeCommand = "react-native";
b8a56999Patricio Beltran9 years ago38private static ReactNativeVersionCommand = "-v";
9596aa53digeff10 years ago39private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago40
0a68f8dbArtem Egorov8 years ago41constructor(
42private currentWorkingDirectory: string = process.cwd(),
43private logger: ILogger = new NullLogger()
44) { }
3fb37ad5unknown10 years ago45
fb2bae06Daniel10 years ago46public execute(command: string, options: Options = {}): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago47this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start));
9596aa53digeff10 years ago48return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago49.then(stdout => {
0a68f8dbArtem Egorov8 years ago50this.logger.info(stdout);
51this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End));
3fb37ad5unknown10 years ago52},
cb6d0922digeff10 years ago53(reason: Error) =>
54this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago55}
56
45944d15Meena Kunnathur Balakrishnan10 years ago57/**
58* Spawns a child process with the params passed
59* This method waits until the spawned process finishes execution
60* {command} - The command to be invoked in the child process
61* {args} - Arguments to be passed to the command
62* {options} - additional options with which the child process needs to be spawned
63*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago64public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
9596aa53digeff10 years ago65return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago66}
67
6126d899Meena Kunnathur Balakrishnan10 years ago68/**
69* Spawns the React Native packager in a child process.
70*/
9596aa53digeff10 years ago71public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
72return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago73}
74
b8a56999Patricio Beltran9 years ago75/**
76* Uses the `react-native -v` command to get the version used on the project.
77* Returns null if the workspace is not a react native project
78*/
79public getReactNativeVersion(): Q.Promise<string> {
80let deferred = Q.defer<string>();
81const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
82let output = "";
83
84const result = this.childProcess.spawn(reactCommand,
85[CommandExecutor.ReactNativeVersionCommand],
86{ cwd: this.currentWorkingDirectory });
87
88result.stdout.on("data", (data: Buffer) => {
89output += data.toString();
90});
91
92result.stdout.on("end", () => {
93const match = output.match(/react-native: ([\d\.]+)/);
5c8365a6Artem Egorov8 years ago94deferred.resolve(match && match[1] || "");
b8a56999Patricio Beltran9 years ago95});
96
97return deferred.promise;
98}
99
c3a987a7Meena Kunnathur Balakrishnan10 years ago100/**
101* Kills the React Native packager in a child process.
102*/
5c8365a6Artem Egorov8 years ago103public killReactPackager(packagerProcess?: ChildProcess): Q.Promise<void> {
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago104if (packagerProcess) {
a1005420dlebu10 years ago105return Q({}).then(() => {
106if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
9596aa53digeff10 years ago107return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
a1005420dlebu10 years ago108} else {
109packagerProcess.kill();
5c8365a6Artem Egorov8 years ago110return Q.resolve(void 0);
a1005420dlebu10 years ago111}
112}).then(() => {
0a68f8dbArtem Egorov8 years ago113this.logger.info("Packager stopped");
a1005420dlebu10 years ago114});
115
c3a987a7Meena Kunnathur Balakrishnan10 years ago116} else {
0a68f8dbArtem Egorov8 years ago117this.logger.warning("Packager not found");
10873e11digeff10 years ago118return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago119}
120}
121
f8d32439dlebu10 years ago122/**
123* Executes a react native command and waits for its completion.
124*/
e390ca0aVladimir Kotikov8 years ago125public spawnReactCommand(command: string, args: string[] = [], options: Options = {}): ISpawnResult {
9596aa53digeff10 years ago126const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
94cd5149Artem Egorov8 years ago127return this.spawnChildProcess(reactCommand, [command, ...args], options);
5b0582f3digeff10 years ago128}
129
831f4a85Patricio Beltran9 years ago130/**
131* Spawns a child process with the params passed
132* This method has logic to do while the command is executing
133* {command} - The command to be invoked in the child process
134* {args} - Arguments to be passed to the command
135* {options} - additional options with which the child process needs to be spawned
136*/
137public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
138let deferred = Q.defer<void>();
139const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
140const commandWithArgs = command + " " + args.join(" ");
141const timeBetweenDots = 1500;
b0af599cJimmy Thomson9 years ago142let lastDotTime = 0;
831f4a85Patricio Beltran9 years ago143
144const printDot = () => {
b0af599cJimmy Thomson9 years ago145const now = Date.now();
146if (now - lastDotTime > timeBetweenDots) {
147lastDotTime = now;
0a68f8dbArtem Egorov8 years ago148this.logger.logStream(".", process.stdout);
b0af599cJimmy Thomson9 years ago149}
831f4a85Patricio Beltran9 years ago150};
151
152if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago153this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start));
831f4a85Patricio Beltran9 years ago154}
155
156const result = this.childProcess.spawn(command, args, spawnOptions);
157
158result.stdout.on("data", (data: Buffer) => {
159if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago160this.logger.logStream(data, process.stdout);
b0af599cJimmy Thomson9 years ago161} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago162printDot();
163}
164});
165
166result.stderr.on("data", (data: Buffer) => {
167if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago168this.logger.logStream(data, process.stderr);
b0af599cJimmy Thomson9 years ago169} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago170printDot();
171}
172});
173
174result.outcome = result.outcome.then(
175() => {
176if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago177this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End));
831f4a85Patricio Beltran9 years ago178}
0a68f8dbArtem Egorov8 years ago179this.logger.logStream("\n", process.stdout);
831f4a85Patricio Beltran9 years ago180deferred.resolve(void 0);
181},
182reason => {
0d827d9bJimmy Thomson9 years ago183deferred.reject(reason);
831f4a85Patricio Beltran9 years ago184return this.generateRejectionForCommand(commandWithArgs, reason);
185});
186return deferred.promise;
187}
188
9596aa53digeff10 years ago189private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
190const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
191const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago192
0a68f8dbArtem Egorov8 years ago193this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start));
9596aa53digeff10 years ago194const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago195
196result.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago197this.logger.logStream(data, process.stderr);
3fb37ad5unknown10 years ago198});
199
200result.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago201this.logger.logStream(data, process.stdout);
3fb37ad5unknown10 years ago202});
203
10873e11digeff10 years ago204result.outcome = result.outcome.then(
9596aa53digeff10 years ago205() =>
0a68f8dbArtem Egorov8 years ago206this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End)),
10873e11digeff10 years ago207reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago208this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago209return result;
3fb37ad5unknown10 years ago210}
10873e11digeff10 years ago211
3677173cdigeff10 years ago212private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago213return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago214}
0a68f8dbArtem Egorov8 years ago215
216private static getCommandStatusString(command: string, status: CommandStatus) {
217switch (status) {
218case CommandStatus.Start:
219return `Executing command: ${command}`;
220
221case CommandStatus.End:
222return `Finished executing: ${command}`;
223
224default:
225throw new Error("Unsupported command status");
226}
227}
3fb37ad5unknown10 years ago228}