microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f16656e963fb76d68e7e5310a3661326540b54f2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

151lines · 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";
b8a56999Patricio Beltran10 years ago28private static ReactNativeVersionCommand = "-v";
3fb37ad5unknown10 years ago29private currentWorkingDirectory: string;
9596aa53digeff10 years ago30private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago31
110558c0Jimmy Thomson10 years ago32constructor(currentWorkingDirectory?: string) {
323a3cc0Meena Kunnathur Balakrishnan10 years ago33this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
3fb37ad5unknown10 years ago34}
35
fb2bae06Daniel10 years ago36public execute(command: string, options: Options = {}): Q.Promise<void> {
17161993Meena Kunnathur Balakrishnan10 years ago37Log.logCommandStatus(command, CommandStatus.Start);
9596aa53digeff10 years ago38return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago39.then(stdout => {
bedf110funknown10 years ago40Log.logMessage(stdout);
17161993Meena Kunnathur Balakrishnan10 years ago41Log.logCommandStatus(command, CommandStatus.End);
3fb37ad5unknown10 years ago42},
cb6d0922digeff10 years ago43(reason: Error) =>
44this.generateRejectionForCommand(command, reason));
3fb37ad5unknown10 years ago45}
46
45944d15Meena Kunnathur Balakrishnan10 years ago47/**
48* Spawns a child process with the params passed
49* This method waits until the spawned process finishes execution
50* {command} - The command to be invoked in the child process
51* {args} - Arguments to be passed to the command
52* {options} - additional options with which the child process needs to be spawned
53*/
323a3cc0Meena Kunnathur Balakrishnan10 years ago54public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
9596aa53digeff10 years ago55return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago56}
57
6126d899Meena Kunnathur Balakrishnan10 years ago58/**
59* Spawns the React Native packager in a child process.
60*/
9596aa53digeff10 years ago61public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
62return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago63}
64
b8a56999Patricio Beltran10 years ago65/**
66* Uses the `react-native -v` command to get the version used on the project.
67* Returns null if the workspace is not a react native project
68*/
69public getReactNativeVersion(): Q.Promise<string> {
70let deferred = Q.defer<string>();
71const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
72let output = "";
73
74const result = this.childProcess.spawn(reactCommand,
75[CommandExecutor.ReactNativeVersionCommand],
76{ cwd: this.currentWorkingDirectory });
77
78result.stdout.on("data", (data: Buffer) => {
79output += data.toString();
80});
81
82result.stdout.on("end", () => {
83const match = output.match(/react-native: ([\d\.]+)/);
84deferred.resolve(match && match[1]);
85});
86
87return deferred.promise;
88}
89
c3a987a7Meena Kunnathur Balakrishnan10 years ago90/**
91* Kills the React Native packager in a child process.
92*/
f1a07677Meena Kunnathur Balakrishnan10 years ago93public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
898cb3c6Meena Kunnathur Balakrishnan10 years ago94Log.logMessage("Stopping Packager");
a1005420dlebu10 years ago95
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago96if (packagerProcess) {
a1005420dlebu10 years ago97return Q({}).then(() => {
98if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
9596aa53digeff10 years ago99return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
a1005420dlebu10 years ago100} else {
101packagerProcess.kill();
102}
103}).then(() => {
104Log.logMessage("Packager stopped");
105});
106
c3a987a7Meena Kunnathur Balakrishnan10 years ago107} else {
898cb3c6Meena Kunnathur Balakrishnan10 years ago108Log.logMessage("Packager not found");
10873e11digeff10 years ago109return Q.resolve<void>(void 0);
c3a987a7Meena Kunnathur Balakrishnan10 years ago110}
111}
112
f8d32439dlebu10 years ago113/**
114* Executes a react native command and waits for its completion.
115*/
9596aa53digeff10 years ago116public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult {
117const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
118return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options);
5b0582f3digeff10 years ago119}
120
9596aa53digeff10 years ago121private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
122const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
123const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago124
898cb3c6Meena Kunnathur Balakrishnan10 years ago125Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
9596aa53digeff10 years ago126const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago127
128result.stderr.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago129Log.logStreamData(data, process.stderr);
3fb37ad5unknown10 years ago130});
131
132result.stdout.on("data", (data: Buffer) => {
99e41548Meena Kunnathur Balakrishnan10 years ago133Log.logStreamData(data, process.stdout);
3fb37ad5unknown10 years ago134});
135
10873e11digeff10 years ago136result.outcome = result.outcome.then(
9596aa53digeff10 years ago137() =>
138Log.logCommandStatus(commandWithArgs, CommandStatus.End),
10873e11digeff10 years ago139reason =>
4f7c7447Meena Kunnathur Balakrishnan10 years ago140this.generateRejectionForCommand(commandWithArgs, reason));
efa076b0Meena Kunnathur Balakrishnan10 years ago141return result;
3fb37ad5unknown10 years ago142}
10873e11digeff10 years ago143
3677173cdigeff10 years ago144private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
4f7c7447Meena Kunnathur Balakrishnan10 years ago145return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
10873e11digeff10 years ago146}
9596aa53digeff10 years ago147
148private combineArguments(firstArgument: string, otherArguments: string[] = []) {
149return [firstArgument].concat(otherArguments);
150}
3fb37ad5unknown10 years ago151}