microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
05604bfbc51c4280798f0dcaf994bb2cee9f8571

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

145lines · 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) {
323a3cc0Meena Kunnathur Balakrishnan10 years ago31this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
3fb37ad5unknown10 years ago32}
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
47* This method waits until the spawned process finishes 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*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago52public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
efa076b0Meena Kunnathur Balakrishnan10 years ago53return this.spawnChildProcess(command, args, true, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago54}
55
6126d899Meena Kunnathur Balakrishnan10 years ago56/**
57* Spawns the React Native packager in a child process.
58*/
8269b3b7Meena Kunnathur Balakrishnan10 years ago59public spawnReactPackager(args?: string[], options: Options = {}): Q.Promise<ISpawnResult> {
efa076b0Meena Kunnathur Balakrishnan10 years ago60let command = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
61let runArguments = ["start"];
62
63if (args) {
64runArguments = runArguments.concat(args);
65}
66
67let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
68let result = this.spawnChildProcess(command, runArguments, false, spawnOptions);
8269b3b7Meena Kunnathur Balakrishnan10 years ago69return Q.resolve(result);
6126d899Meena Kunnathur Balakrishnan10 years ago70}
71
c3a987a7Meena Kunnathur Balakrishnan10 years ago72/**
73* Kills the React Native packager in a child process.
74*/
f1a07677Meena Kunnathur Balakrishnan10 years ago75public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
898cb3c6Meena Kunnathur Balakrishnan10 years ago76Log.logMessage("Stopping Packager");
a1005420dlebu10 years ago77
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago78if (packagerProcess) {
a1005420dlebu10 years ago79return Q({}).then(() => {
80if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
81return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
82} else {
83packagerProcess.kill();
84}
85}).then(() => {
86Log.logMessage("Packager stopped");
87});
88
c3a987a7Meena Kunnathur Balakrishnan10 years ago89} else {
898cb3c6Meena Kunnathur Balakrishnan10 years ago90Log.logMessage("Packager not found");
10873e11digeff10 years ago91return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago92}
93}
94
f8d32439dlebu10 years ago95/**
96* Executes a react native command and waits for its completion.
97*/
efa076b0Meena Kunnathur Balakrishnan10 years ago98public spawnReactCommand(command: string, args?: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult {
323a3cc0Meena Kunnathur Balakrishnan10 years ago99return this.spawnChildReactCommandProcess(command, args, waitForExit, options);
5b0582f3digeff10 years ago100}
101
efa076b0Meena Kunnathur Balakrishnan10 years ago102public spawnChildReactCommandProcess(command: string, args?: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult {
f8d32439dlebu10 years ago103let runArguments = [command];
104if (args) {
f7208a21Jimmy Thomson10 years ago105runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago106}
107
a1005420dlebu10 years ago108let reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
323a3cc0Meena Kunnathur Balakrishnan10 years ago109return this.spawnChildProcess(reactCommand, runArguments, waitForExit, options);
f8d32439dlebu10 years ago110}
111
efa076b0Meena Kunnathur Balakrishnan10 years ago112private spawnChildProcess(command: string, args: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult {
08e81a53unknown10 years ago113let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
3fb37ad5unknown10 years ago114let commandWithArgs = command + " " + args.join(" ");
88246fe9Meena Kunnathur Balakrishnan10 years ago115let childProcessHelper = new Node.ChildProcess();
3fb37ad5unknown10 years ago116
898cb3c6Meena Kunnathur Balakrishnan10 years ago117Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
88246fe9Meena Kunnathur Balakrishnan10 years ago118let result = waitForExit ? childProcessHelper.spawnWaitUntilFinished(command, args, spawnOptions) : childProcessHelper.spawnWaitUntilStarted(command, args, spawnOptions);
3fb37ad5unknown10 years ago119
120result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago121Log.logStreamData(data, process.stderr);
3fb37ad5unknown10 years ago122});
123
124result.stdout.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago125Log.logStreamData(data, process.stdout);
3fb37ad5unknown10 years ago126});
127
05604bfbdigeff10 years ago128// HACK: Remove this and make a better implementation
129result.spawnedProcess.once("exit", (code: number) => {
130if (code === 0) {
131Log.logCommandStatus(commandWithArgs, CommandStatus.End);
132}
133});
134
10873e11digeff10 years ago135result.outcome = result.outcome.then(
05604bfbdigeff10 years ago136() => {},
10873e11digeff10 years ago137reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago138this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago139return result;
3fb37ad5unknown10 years ago140}
10873e11digeff10 years ago141
3677173cdigeff10 years ago142private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago143return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago144}
3fb37ad5unknown10 years ago145}