microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
75c7d921c8412f73630c6d1964c571077e286014

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

125lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5import {ChildProcess} from "child_process";
6import {Log} from "./log/log";
7import {Node} from "./node/node";
8import {ISpawnResult} from "./node/childProcess";
9import {HostPlatform, HostPlatformId} from "../common/hostPlatform";
10import {ErrorHelper} from "./error/errorHelper";
11import {InternalErrorCode} from "./error/internalErrorCode";
12
13interface EnvironmentOptions {
14 REACT_DEBUGGER?: string;
15}
16
17interface Options {
18 env?: EnvironmentOptions;
19}
20
21export enum CommandStatus {
22 Start = 0,
23 End = 1
24}
25
26export class CommandExecutor {
27 private static ReactNativeCommand = "react-native";
28 private currentWorkingDirectory: string;
29 private childProcess = new Node.ChildProcess();
30
31 constructor(currentWorkingDirectory?: string) {
32 this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
33 }
34
35 public execute(command: string, options: Options = {}): Q.Promise<void> {
36 Log.logCommandStatus(command, CommandStatus.Start);
37 return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
38 .then(stdout => {
39 Log.logMessage(stdout);
40 Log.logCommandStatus(command, CommandStatus.End);
41 },
42 (reason: Error) =>
43 this.generateRejectionForCommand(command, reason));
44 }
45
46 /**
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 */
53 public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
54 return this.spawnChildProcess(command, args, options).outcome;
55 }
56
57 /**
58 * Spawns the React Native packager in a child process.
59 */
60 public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
61 return this.spawnReactCommand("start", args, options);
62 }
63
64 /**
65 * Kills the React Native packager in a child process.
66 */
67 public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
68 Log.logMessage("Stopping Packager");
69
70 if (packagerProcess) {
71 return Q({}).then(() => {
72 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
73 return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
74 } else {
75 packagerProcess.kill();
76 }
77 }).then(() => {
78 Log.logMessage("Packager stopped");
79 });
80
81 } else {
82 Log.logMessage("Packager not found");
83 return Q.resolve<void>(void 0);
84 }
85 }
86
87 /**
88 * Executes a react native command and waits for its completion.
89 */
90 public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult {
91 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
92 return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options);
93 }
94
95 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
96 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
97 const commandWithArgs = command + " " + args.join(" ");
98
99 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
100 const result = this.childProcess.spawn(command, args, spawnOptions);
101
102 result.stderr.on("data", (data: Buffer) => {
103 Log.logStreamData(data, process.stderr);
104 });
105
106 result.stdout.on("data", (data: Buffer) => {
107 Log.logStreamData(data, process.stdout);
108 });
109
110 result.outcome = result.outcome.then(
111 () =>
112 Log.logCommandStatus(commandWithArgs, CommandStatus.End),
113 reason =>
114 this.generateRejectionForCommand(commandWithArgs, reason));
115 return result;
116 }
117
118 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
119 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
120 }
121
122 private combineArguments(firstArgument: string, otherArguments: string[] = []) {
123 return [firstArgument].concat(otherArguments);
124 }
125}
126