microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

214lines · 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";
5c8365a6Artem Egorov8 years ago9import {HostPlatform, HostPlatformId} from "./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,
27710197Vladimir Kotikov8 years ago31End = 1,
17161993Meena Kunnathur Balakrishnan10 years ago32}
33
3fb37ad5unknown10 years ago34export class CommandExecutor {
94cd5149Artem Egorov8 years ago35
bbdbbb3bdlebu10 years ago36private static ReactNativeCommand = "react-native";
b8a56999Patricio Beltran10 years ago37private static ReactNativeVersionCommand = "-v";
3fb37ad5unknown10 years ago38private currentWorkingDirectory: string;
9596aa53digeff10 years ago39private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago40
110558c0Jimmy Thomson10 years ago41constructor(currentWorkingDirectory?: string) {
323a3cc0Meena Kunnathur Balakrishnan10 years ago42this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
3fb37ad5unknown10 years ago43}
44
fb2bae06Daniel10 years ago45public execute(command: string, options: Options = {}): Q.Promise<void> {
831f4a85Patricio Beltran9 years ago46Log.logCommandStatus(command, CommandStatus.Start);
9596aa53digeff10 years ago47return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago48.then(stdout => {
831f4a85Patricio Beltran9 years ago49Log.logMessage(stdout);
50Log.logCommandStatus(command, CommandStatus.End);
3fb37ad5unknown10 years ago51},
cb6d0922digeff10 years ago52(reason: Error) =>
53this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago54}
55
45944d15Meena Kunnathur Balakrishnan10 years ago56/**
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*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago63public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
9596aa53digeff10 years ago64return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago65}
66
6126d899Meena Kunnathur Balakrishnan10 years ago67/**
68* Spawns the React Native packager in a child process.
69*/
9596aa53digeff10 years ago70public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
71return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago72}
73
b8a56999Patricio Beltran10 years ago74/**
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*/
78public getReactNativeVersion(): Q.Promise<string> {
79let deferred = Q.defer<string>();
80const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
81let output = "";
82
83const result = this.childProcess.spawn(reactCommand,
84[CommandExecutor.ReactNativeVersionCommand],
85{ cwd: this.currentWorkingDirectory });
86
87result.stdout.on("data", (data: Buffer) => {
88output += data.toString();
89});
90
91result.stdout.on("end", () => {
92const match = output.match(/react-native: ([\d\.]+)/);
5c8365a6Artem Egorov8 years ago93deferred.resolve(match && match[1] || "");
b8a56999Patricio Beltran10 years ago94});
95
96return deferred.promise;
97}
98
c3a987a7Meena Kunnathur Balakrishnan10 years ago99/**
100* Kills the React Native packager in a child process.
101*/
5c8365a6Artem Egorov8 years ago102public killReactPackager(packagerProcess?: ChildProcess): Q.Promise<void> {
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago103if (packagerProcess) {
a1005420dlebu10 years ago104return Q({}).then(() => {
105if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
9596aa53digeff10 years ago106return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
a1005420dlebu10 years ago107} else {
108packagerProcess.kill();
5c8365a6Artem Egorov8 years ago109return Q.resolve(void 0);
a1005420dlebu10 years ago110}
111}).then(() => {
112Log.logMessage("Packager stopped");
113});
114
c3a987a7Meena Kunnathur Balakrishnan10 years ago115} else {
898cb3c6Meena Kunnathur Balakrishnan10 years ago116Log.logMessage("Packager not found");
10873e11digeff10 years ago117return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago118}
119}
120
f8d32439dlebu10 years ago121/**
122* Executes a react native command and waits for its completion.
123*/
e390ca0aVladimir Kotikov8 years ago124public spawnReactCommand(command: string, args: string[] = [], options: Options = {}): ISpawnResult {
9596aa53digeff10 years ago125const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
94cd5149Artem Egorov8 years ago126return this.spawnChildProcess(reactCommand, [command, ...args], options);
5b0582f3digeff10 years ago127}
128
831f4a85Patricio Beltran9 years ago129/**
130* Spawns a child process with the params passed
131* This method has logic to do while the command is executing
132* {command} - The command to be invoked in the child process
133* {args} - Arguments to be passed to the command
134* {options} - additional options with which the child process needs to be spawned
135*/
136public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> {
137let deferred = Q.defer<void>();
138const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
139const commandWithArgs = command + " " + args.join(" ");
140const timeBetweenDots = 1500;
b0af599cJimmy Thomson9 years ago141let lastDotTime = 0;
831f4a85Patricio Beltran9 years ago142
143const printDot = () => {
b0af599cJimmy Thomson9 years ago144const now = Date.now();
145if (now - lastDotTime > timeBetweenDots) {
146lastDotTime = now;
147Log.logString(".");
148}
831f4a85Patricio Beltran9 years ago149};
150
151if (options.verbosity === CommandVerbosity.OUTPUT) {
152Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
153}
154
155const result = this.childProcess.spawn(command, args, spawnOptions);
156
157result.stdout.on("data", (data: Buffer) => {
158if (options.verbosity === CommandVerbosity.OUTPUT) {
159Log.logStreamData(data, process.stdout);
b0af599cJimmy Thomson9 years ago160} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago161printDot();
162}
163});
164
165result.stderr.on("data", (data: Buffer) => {
166if (options.verbosity === CommandVerbosity.OUTPUT) {
167Log.logStreamData(data, process.stderr);
b0af599cJimmy Thomson9 years ago168} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago169printDot();
170}
171});
172
173result.outcome = result.outcome.then(
174() => {
175if (options.verbosity === CommandVerbosity.OUTPUT) {
176Log.logCommandStatus(commandWithArgs, CommandStatus.End);
177}
178Log.logString("\n");
179deferred.resolve(void 0);
180},
181reason => {
0d827d9bJimmy Thomson9 years ago182deferred.reject(reason);
831f4a85Patricio Beltran9 years ago183return this.generateRejectionForCommand(commandWithArgs, reason);
184});
185return deferred.promise;
186}
187
9596aa53digeff10 years ago188private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
189const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
190const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago191
898cb3c6Meena Kunnathur Balakrishnan10 years ago192Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
9596aa53digeff10 years ago193const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago194
195result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago196Log.logStreamData(data, process.stderr);
3fb37ad5unknown10 years ago197});
198
199result.stdout.on("data", (data: Buffer) => {
831f4a85Patricio Beltran9 years ago200Log.logStreamData(data, process.stdout);
3fb37ad5unknown10 years ago201});
202
10873e11digeff10 years ago203result.outcome = result.outcome.then(
9596aa53digeff10 years ago204() =>
205Log.logCommandStatus(commandWithArgs, CommandStatus.End),
10873e11digeff10 years ago206reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago207this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago208return result;
3fb37ad5unknown10 years ago209}
10873e11digeff10 years ago210
3677173cdigeff10 years ago211private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago212return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago213}
3fb37ad5unknown10 years ago214}