microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f42e0082062bc77ebdb0bcadca1f33ae1fd67802

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

216lines · 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";
190e393cMeena Kunnathur Balakrishnan10 years ago6import {Log} from "./log/log";
b0061ac6Meena Kunnathur Balakrishnan10 years ago7import {Node} from "./node/node";
cb6d0922digeff10 years ago8import {ISpawnResult} from "./node/childProcess";
a1005420dlebu10 years ago9import {HostPlatform, HostPlatformId} from "../common/hostPlatform";
190e393cMeena Kunnathur Balakrishnan10 years ago10import {ErrorHelper} from "./error/errorHelper";
11import {InternalErrorCode} from "./error/internalErrorCode";
3fb37ad5unknown10 years ago12
831f4a85Patricio Beltran9 years ago13export enum CommandVerbosity {
14OUTPUT,
15SILENT,
16PROGRESS,
17}
18
3fb37ad5unknown10 years ago19interface EnvironmentOptions {
20REACT_DEBUGGER?: string;
21}
22
23interface Options {
24env?: EnvironmentOptions;
831f4a85Patricio Beltran9 years ago25verbosity?: CommandVerbosity;
0d827d9bJimmy Thomson9 years ago26cwd?: string;
3fb37ad5unknown10 years ago27}
28
17161993Meena Kunnathur Balakrishnan10 years ago29export enum CommandStatus {
30Start = 0,
31End = 1
32}
33
3fb37ad5unknown10 years ago34export class CommandExecutor {
bbdbbb3bdlebu10 years ago35private static ReactNativeCommand = "react-native";
b8a56999Patricio Beltran9 years ago36private static ReactNativeVersionCommand = "-v";
3fb37ad5unknown10 years ago37private currentWorkingDirectory: string;
9596aa53digeff10 years ago38private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago39
110558c0Jimmy Thomson10 years ago40constructor(currentWorkingDirectory?: string) {
323a3cc0Meena Kunnathur Balakrishnan10 years ago41this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
3fb37ad5unknown10 years ago42}
43
fb2bae06Daniel10 years ago44public execute(command: string, options: Options = {}): Q.Promise<void> {
831f4a85Patricio Beltran9 years ago45Log.logCommandStatus(command, CommandStatus.Start);
9596aa53digeff10 years ago46return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago47.then(stdout => {
831f4a85Patricio Beltran9 years ago48Log.logMessage(stdout);
49Log.logCommandStatus(command, CommandStatus.End);
3fb37ad5unknown10 years ago50},
cb6d0922digeff10 years ago51(reason: Error) =>
52this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago53}
54
45944d15Meena Kunnathur Balakrishnan10 years ago55/**
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*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago62public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
9596aa53digeff10 years ago63return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago64}
65
6126d899Meena Kunnathur Balakrishnan10 years ago66/**
67* Spawns the React Native packager in a child process.
68*/
9596aa53digeff10 years ago69public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
70return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago71}
72
b8a56999Patricio Beltran9 years ago73/**
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*/
77public getReactNativeVersion(): Q.Promise<string> {
78let deferred = Q.defer<string>();
79const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
80let output = "";
81
82const result = this.childProcess.spawn(reactCommand,
83[CommandExecutor.ReactNativeVersionCommand],
84{ cwd: this.currentWorkingDirectory });
85
86result.stdout.on("data", (data: Buffer) => {
87output += data.toString();
88});
89
90result.stdout.on("end", () => {
91const match = output.match(/react-native: ([\d\.]+)/);
92deferred.resolve(match && match[1]);
93});
94
95return deferred.promise;
96}
97
c3a987a7Meena Kunnathur Balakrishnan10 years ago98/**
99* Kills the React Native packager in a child process.
100*/
f1a07677Meena Kunnathur Balakrishnan10 years ago101public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago102if (packagerProcess) {
a1005420dlebu10 years ago103return Q({}).then(() => {
104if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
9596aa53digeff10 years ago105return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
a1005420dlebu10 years ago106} else {
107packagerProcess.kill();
108}
109}).then(() => {
110Log.logMessage("Packager stopped");
111});
112
c3a987a7Meena Kunnathur Balakrishnan10 years ago113} else {
898cb3c6Meena Kunnathur Balakrishnan10 years ago114Log.logMessage("Packager not found");
10873e11digeff10 years ago115return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago116}
117}
118
f8d32439dlebu10 years ago119/**
120* Executes a react native command and waits for its completion.
121*/
9596aa53digeff10 years ago122public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult {
123const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
124return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options);
5b0582f3digeff10 years ago125}
126
831f4a85Patricio Beltran9 years ago127/**
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*/
134public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
135let deferred = Q.defer<void>();
136const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
137const commandWithArgs = command + " " + args.join(" ");
138const timeBetweenDots = 1500;
b0af599cJimmy Thomson9 years ago139let lastDotTime = 0;
831f4a85Patricio Beltran9 years ago140
141const printDot = () => {
b0af599cJimmy Thomson9 years ago142const now = Date.now();
143if (now - lastDotTime > timeBetweenDots) {
144lastDotTime = now;
145Log.logString(".");
146}
831f4a85Patricio Beltran9 years ago147};
148
149if (options.verbosity === CommandVerbosity.OUTPUT) {
150Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
151}
152
153const result = this.childProcess.spawn(command, args, spawnOptions);
154
155result.stdout.on("data", (data: Buffer) => {
156if (options.verbosity === CommandVerbosity.OUTPUT) {
157Log.logStreamData(data, process.stdout);
b0af599cJimmy Thomson9 years ago158} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago159printDot();
160}
161});
162
163result.stderr.on("data", (data: Buffer) => {
164if (options.verbosity === CommandVerbosity.OUTPUT) {
165Log.logStreamData(data, process.stderr);
b0af599cJimmy Thomson9 years ago166} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago167printDot();
168}
169});
170
171result.outcome = result.outcome.then(
172() => {
173if (options.verbosity === CommandVerbosity.OUTPUT) {
174Log.logCommandStatus(commandWithArgs, CommandStatus.End);
175}
176Log.logString("\n");
177deferred.resolve(void 0);
178},
179reason => {
0d827d9bJimmy Thomson9 years ago180deferred.reject(reason);
831f4a85Patricio Beltran9 years ago181return this.generateRejectionForCommand(commandWithArgs, reason);
182});
183return deferred.promise;
184}
185
9596aa53digeff10 years ago186private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
187const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
188const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago189
898cb3c6Meena Kunnathur Balakrishnan10 years ago190Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
9596aa53digeff10 years ago191const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago192
193result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago194Log.logStreamData(data, process.stderr);
3fb37ad5unknown10 years ago195});
196
197result.stdout.on("data", (data: Buffer) => {
831f4a85Patricio Beltran9 years ago198Log.logStreamData(data, process.stdout);
3fb37ad5unknown10 years ago199});
200
10873e11digeff10 years ago201result.outcome = result.outcome.then(
9596aa53digeff10 years ago202() =>
203Log.logCommandStatus(commandWithArgs, CommandStatus.End),
10873e11digeff10 years ago204reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago205this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago206return result;
3fb37ad5unknown10 years ago207}
10873e11digeff10 years ago208
3677173cdigeff10 years ago209private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago210return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago211}
9596aa53digeff10 years ago212
213private combineArguments(firstArgument: string, otherArguments: string[] = []) {
214return [firstArgument].concat(otherArguments);
215}
3fb37ad5unknown10 years ago216}