microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
433116d1ec65cfe6cab9e279fda35fe5f9bf035c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

166lines · 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
13interface EnvironmentOptions {
14REACT_DEBUGGER?: string;
15}
16
17interface Options {
18env?: EnvironmentOptions;
19}
20
17161993Meena Kunnathur Balakrishnan10 years ago21export enum CommandStatus {
22Start = 0,
23End = 1
24}
25
3fb37ad5unknown10 years ago26export class CommandExecutor {
bbdbbb3bdlebu10 years ago27private static ReactNativeCommand = "react-native";
3fb37ad5unknown10 years ago28private currentWorkingDirectory: string;
29
110558c0Jimmy Thomson10 years ago30constructor(currentWorkingDirectory?: string) {
3fb37ad5unknown10 years ago31this.currentWorkingDirectory = currentWorkingDirectory;
32}
33
fb2bae06Daniel10 years ago34public execute(command: string, options: Options = {}): Q.Promise<void> {
17161993Meena Kunnathur Balakrishnan10 years ago35Log.logCommandStatus(command, CommandStatus.Start);
bedf110funknown10 years ago36return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago37.then(stdout => {
bedf110funknown10 years ago38Log.logMessage(stdout);
17161993Meena Kunnathur Balakrishnan10 years ago39Log.logCommandStatus(command, CommandStatus.End);
3fb37ad5unknown10 years ago40},
cb6d0922digeff10 years ago41(reason: Error) =>
42this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago43}
44
45944d15Meena Kunnathur Balakrishnan10 years ago45/**
46* Spawns a child process with the params passed and returns promise of the spawned ChildProcess
47* This method does not wait for the spawned process to finish execution
48* {command} - The command to be invoked in the child process
49* {args} - Arguments to be passed to the command
50* {options} - additional options with which the child process needs to be spawned
51*/
f1a07677Meena Kunnathur Balakrishnan10 years ago52public spawn(command: string, args: string[], options: Options = {}): ChildProcess {
53return this.spawnChildProcess(command, args, options).spawnedProcess;
45944d15Meena Kunnathur Balakrishnan10 years ago54}
55
56/**
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*/
f1a07677Meena Kunnathur Balakrishnan10 years ago63public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}): Q.Promise<void> {
64return 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*/
f1a07677Meena Kunnathur Balakrishnan10 years ago70public spawnReactPackager(args?: string[], options: Options = {}): Q.Promise<ChildProcess> {
b3ef3553Meena Kunnathur Balakrishnan10 years ago71let deferred = Q.defer<ChildProcess>();
a1005420dlebu10 years ago72let command = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
6126d899Meena Kunnathur Balakrishnan10 years ago73let runArguments = ["start"];
74
75if (args) {
76runArguments = runArguments.concat(args);
77}
78
79let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
80
c3a987a7Meena Kunnathur Balakrishnan10 years ago81let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions);
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago82result.spawnedProcess.once("error", (error: any) => {
898cb3c6Meena Kunnathur Balakrishnan10 years ago83deferred.reject(ErrorHelper.getNestedError(error, InternalErrorCode.PackagerStartFailed));
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago84});
85
c3a987a7Meena Kunnathur Balakrishnan10 years ago86result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago87Log.logStreamData(data, process.stderr);
6126d899Meena Kunnathur Balakrishnan10 years ago88});
89
c3a987a7Meena Kunnathur Balakrishnan10 years ago90result.stdout.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago91Log.logStreamData(data, process.stdout);
6126d899Meena Kunnathur Balakrishnan10 years ago92});
93
65fd8e85digeff10 years ago94// TODO #83 - PROMISE: We need to consume result.outcome here
10873e11digeff10 years ago95Q.delay(300).done(() => deferred.resolve(result.spawnedProcess));
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago96return deferred.promise;
6126d899Meena Kunnathur Balakrishnan10 years ago97}
98
c3a987a7Meena Kunnathur Balakrishnan10 years ago99/**
100* Kills the React Native packager in a child process.
101*/
f1a07677Meena Kunnathur Balakrishnan10 years ago102public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
898cb3c6Meena Kunnathur Balakrishnan10 years ago103Log.logMessage("Stopping Packager");
a1005420dlebu10 years ago104
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago105if (packagerProcess) {
a1005420dlebu10 years ago106return Q({}).then(() => {
107if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
108return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
109} else {
110packagerProcess.kill();
111}
112}).then(() => {
113Log.logMessage("Packager stopped");
114});
115
c3a987a7Meena Kunnathur Balakrishnan10 years ago116} else {
898cb3c6Meena Kunnathur Balakrishnan10 years ago117Log.logMessage("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*/
f1a07677Meena Kunnathur Balakrishnan10 years ago125public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}): Q.Promise<void> {
126return this.spawnChildReactCommandProcess(command, args, options).outcome;
5b0582f3digeff10 years ago127}
128
f1a07677Meena Kunnathur Balakrishnan10 years ago129public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}): ISpawnResult {
f8d32439dlebu10 years ago130let runArguments = [command];
131if (args) {
f7208a21Jimmy Thomson10 years ago132runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago133}
134
a1005420dlebu10 years ago135let reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
445b113fdlebu10 years ago136return this.spawnChildProcess(reactCommand, runArguments, options);
f8d32439dlebu10 years ago137}
138
f1a07677Meena Kunnathur Balakrishnan10 years ago139private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
08e81a53unknown10 years ago140let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
3fb37ad5unknown10 years ago141let commandWithArgs = command + " " + args.join(" ");
142
898cb3c6Meena Kunnathur Balakrishnan10 years ago143Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
c3a987a7Meena Kunnathur Balakrishnan10 years ago144let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions);
3fb37ad5unknown10 years ago145
146result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago147Log.logStreamData(data, process.stderr);
3fb37ad5unknown10 years ago148});
149
150result.stdout.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago151Log.logStreamData(data, process.stdout);
3fb37ad5unknown10 years ago152});
153
10873e11digeff10 years ago154result.outcome = result.outcome.then(
155() =>
898cb3c6Meena Kunnathur Balakrishnan10 years ago156Log.logCommandStatus(commandWithArgs, CommandStatus.End),
10873e11digeff10 years ago157reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago158this.generateRejectionForCommand(commandWithArgs, reason));
5b0582f3digeff10 years ago159
160return result;
3fb37ad5unknown10 years ago161}
10873e11digeff10 years ago162
3677173cdigeff10 years ago163private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago164return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago165}
3fb37ad5unknown10 years ago166}