microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
18d8ad2abfdce9bcdb96cc3fe6fb491da873c8f6

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

125lines · 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;
9596aa53digeff10 years ago29private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago30
110558c0Jimmy Thomson10 years ago31constructor(currentWorkingDirectory?: string) {
323a3cc0Meena Kunnathur Balakrishnan10 years ago32this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
3fb37ad5unknown10 years ago33}
34
fb2bae06Daniel10 years ago35public execute(command: string, options: Options = {}): Q.Promise<void> {
17161993Meena Kunnathur Balakrishnan10 years ago36Log.logCommandStatus(command, CommandStatus.Start);
9596aa53digeff10 years ago37return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago38.then(stdout => {
bedf110funknown10 years ago39Log.logMessage(stdout);
17161993Meena Kunnathur Balakrishnan10 years ago40Log.logCommandStatus(command, CommandStatus.End);
3fb37ad5unknown10 years ago41},
cb6d0922digeff10 years ago42(reason: Error) =>
43this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago44}
45
45944d15Meena Kunnathur Balakrishnan10 years ago46/**
47* Spawns a child process with the params passed
48* This method waits until the spawned process finishes execution
49* {command} - The command to be invoked in the child process
50* {args} - Arguments to be passed to the command
51* {options} - additional options with which the child process needs to be spawned
52*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago53public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
9596aa53digeff10 years ago54return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago55}
56
6126d899Meena Kunnathur Balakrishnan10 years ago57/**
58* Spawns the React Native packager in a child process.
59*/
9596aa53digeff10 years ago60public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
61return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago62}
63
c3a987a7Meena Kunnathur Balakrishnan10 years ago64/**
65* Kills the React Native packager in a child process.
66*/
f1a07677Meena Kunnathur Balakrishnan10 years ago67public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
898cb3c6Meena Kunnathur Balakrishnan10 years ago68Log.logMessage("Stopping Packager");
a1005420dlebu10 years ago69
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago70if (packagerProcess) {
a1005420dlebu10 years ago71return Q({}).then(() => {
72if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
9596aa53digeff10 years ago73return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
a1005420dlebu10 years ago74} else {
75packagerProcess.kill();
76}
77}).then(() => {
78Log.logMessage("Packager stopped");
79});
80
c3a987a7Meena Kunnathur Balakrishnan10 years ago81} else {
898cb3c6Meena Kunnathur Balakrishnan10 years ago82Log.logMessage("Packager not found");
10873e11digeff10 years ago83return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago84}
85}
86
f8d32439dlebu10 years ago87/**
88* Executes a react native command and waits for its completion.
89*/
9596aa53digeff10 years ago90public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult {
91const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
92return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options);
5b0582f3digeff10 years ago93}
94
9596aa53digeff10 years ago95private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
96const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
97const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago98
898cb3c6Meena Kunnathur Balakrishnan10 years ago99Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
9596aa53digeff10 years ago100const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago101
102result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago103Log.logStreamData(data, process.stderr);
3fb37ad5unknown10 years ago104});
105
106result.stdout.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago107Log.logStreamData(data, process.stdout);
3fb37ad5unknown10 years ago108});
109
10873e11digeff10 years ago110result.outcome = result.outcome.then(
9596aa53digeff10 years ago111() =>
112Log.logCommandStatus(commandWithArgs, CommandStatus.End),
10873e11digeff10 years ago113reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago114this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago115return result;
3fb37ad5unknown10 years ago116}
10873e11digeff10 years ago117
3677173cdigeff10 years ago118private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago119return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago120}
9596aa53digeff10 years ago121
122private combineArguments(firstArgument: string, otherArguments: string[] = []) {
123return [firstArgument].concat(otherArguments);
124}
3fb37ad5unknown10 years ago125}