microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3314a820561f672a145a365db5c47896e6286fa2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/commands/commandExecutor.ts

103lines · 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): Q.Promise<ChildProcess> {
45 return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => {
46 let commandWithArgs = command + " " + args.join(" ");
47 spawnResult.outcome.then(() => {
48 Log.commandEnded(commandWithArgs, outputChannel);
49 },
50 (reason) => {
51 Log.commandFailed(commandWithArgs, reason, outputChannel);
52 });
53
54 return Q.resolve(spawnResult.spawnedProcess);
55 });
56 }
57
58 /**
59 * Spawns a child process with the params passed
60 * This method waits until the spawned process finishes execution
61 * {command} - The command to be invoked in the child process
62 * {args} - Arguments to be passed to the command
63 * {options} - additional options with which the child process needs to be spawned
64 * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
65 */
66 public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
67 return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => {
68 let commandWithArgs = command + " " + args.join(" ");
69 return spawnResult.outcome.then(() => {
70 Log.commandEnded(commandWithArgs, outputChannel);
71 },
72 (reason) => {
73 Log.commandFailed(commandWithArgs, reason, outputChannel);
74 });
75 });
76 }
77
78 private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> {
79 let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
80 let commandWithArgs = command + " " + args.join(" ");
81
82 Log.commandStarted(commandWithArgs, outputChannel);
83 let result = new Node.ChildProcess().spawn(command, args, spawnOptions);
84
85 result.stderr.on("data", (data: Buffer) => {
86 if (outputChannel) {
87 outputChannel.append(data.toString());
88 } else {
89 process.stderr.write(data);
90 }
91 });
92
93 result.stdout.on("data", (data: Buffer) => {
94 if (outputChannel) {
95 outputChannel.append(data.toString());
96 } else {
97 process.stdout.write(data);
98 }
99 });
100
101 return Q.resolve(result);
102 }
103}
104