microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
5b0582f3b1c06aee308d7107793a302527678884

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

124lines · 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 {ChildProcess} from "child_process";
5import {Log} from "./log";
6import {Node} from "./node/node";
7import {ISpawnResult} from "./node/childProcess";
8import {OutputChannel} from "vscode";
9import * as Q from "q";
10
11interface EnvironmentOptions {
12 REACT_DEBUGGER?: string;
13}
14
15interface Options {
16 env?: EnvironmentOptions;
17}
18
19export class CommandExecutor {
20 private currentWorkingDirectory: string;
21
22 constructor(currentWorkingDirectory?: string) {
23 this.currentWorkingDirectory = currentWorkingDirectory;
24 }
25
26 public execute(command: string, options: Options = {}): Q.Promise<void> {
27 Log.commandStarted(command);
28 return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
29 .then(stdout => {
30 Log.logMessage(stdout);
31 Log.commandEnded(command);
32 },
33 reason => Log.commandFailed(command, reason));
34 }
35
36 /**
37 * Spawns a child process with the params passed and returns promise of the spawned ChildProcess
38 * This method does not wait for the spawned process to finish execution
39 * {command} - The command to be invoked in the child process
40 * {args} - Arguments to be passed to the command
41 * {options} - additional options with which the child process needs to be spawned
42 * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
43 */
44 public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess {
45 return this.spawnChildProcess(command, args, options, outputChannel).spawnedProcess;
46 }
47
48 /**
49 * Spawns a child process with the params passed
50 * This method waits until the spawned process finishes execution
51 * {command} - The command to be invoked in the child process
52 * {args} - Arguments to be passed to the command
53 * {options} - additional options with which the child process needs to be spawned
54 * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
55 */
56 public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<number> {
57 return this.spawnChildProcess(command, args, options, outputChannel).outcome;
58 }
59
60 public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess {
61 return this.spawnChildReactCommandProcess(command, args, options, outputChannel).spawnedProcess;
62 }
63
64 /**
65 * Executes a react native command and waits for its completion.
66 */
67 public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<number> {
68 return this.spawnChildReactCommandProcess(command, args, options, outputChannel).outcome;
69 }
70
71 public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult {
72 let runArguments = [command];
73 if (args) {
74 runArguments = runArguments.concat(args);
75 }
76 return this.spawnChildProcess(this.getReactCommandName(), runArguments, options, outputChannel);
77 }
78
79 /**
80 * Resolves the dev machine, desktop platform.
81 */
82 private getReactCommandName() {
83 let platform = process.platform;
84 switch (platform) {
85 case "darwin":
86 return "react-native";
87 case "win32":
88 default:
89 return "react-native.cmd";
90 }
91 }
92
93 private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult {
94 let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
95 let commandWithArgs = command + " " + args.join(" ");
96
97 Log.commandStarted(commandWithArgs, outputChannel);
98 let result = new Node.ChildProcess().spawn(command, args, spawnOptions);
99
100 result.stderr.on("data", (data: Buffer) => {
101 if (outputChannel) {
102 outputChannel.append(data.toString());
103 } else {
104 process.stderr.write(data);
105 }
106 });
107
108 result.stdout.on("data", (data: Buffer) => {
109 if (outputChannel) {
110 outputChannel.append(data.toString());
111 } else {
112 process.stdout.write(data);
113 }
114 });
115
116 result.outcome.done(() => {
117 Log.commandEnded(commandWithArgs, outputChannel);
118 }, (reason) => {
119 Log.commandFailed(commandWithArgs, reason, outputChannel);
120 });
121
122 return result;
123 }
124}
125