microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0904a0d7ddf15d888e1d494ac66aa4f27e49aefb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/commands/commandExecutor.ts

81lines · 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 {OutputChannel} from "vscode";
8import * as Q from "q";
9
10interface EnvironmentOptions {
11 REACT_DEBUGGER?: string;
12}
13
14interface Options {
15 env?: EnvironmentOptions;
16}
17
18export class CommandExecutor {
19 private currentWorkingDirectory: string;
20
21 constructor(currentWorkingDirectory: string) {
22 this.currentWorkingDirectory = currentWorkingDirectory;
23 }
24
25 public execute(command: string, options: Options = {}): Q.Promise<void> {
26 Log.commandStarted(command);
27 return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
28 .then(stdout => {
29 Log.logMessage(stdout);
30 Log.commandEnded(command);
31 },
32 reason => Log.commandFailed(command, reason));
33 }
34
35 public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
36 let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
37 let commandWithArgs = command + " " + args.join(" ");
38
39 if (outputChannel) {
40 outputChannel.appendLine("######### Executing: " + commandWithArgs + " ##########");
41 outputChannel.show();
42 }
43
44 Log.commandStarted(commandWithArgs);
45 let result = new Node.ChildProcess().spawn(command, args, spawnOptions);
46
47 result.stderr.on("data", (data: Buffer) => {
48 if (outputChannel) {
49 outputChannel.append(data.toString());
50 } else {
51 process.stderr.write(data);
52 }
53 });
54
55 result.stdout.on("data", (data: Buffer) => {
56 if (outputChannel) {
57 outputChannel.append(data.toString());
58 } else {
59 process.stdout.write(data);
60 }
61 });
62
63 result.outcome.then(() => {
64 if (outputChannel) {
65 outputChannel.appendLine("######### Finished executing: " + commandWithArgs + " ##########");
66 } else {
67 Log.commandEnded(commandWithArgs);
68 }
69 },
70 (reason) => {
71 if (outputChannel) {
72 outputChannel.appendLine("######### Failed executing: " + commandWithArgs + " ##########");
73 } else {
74 Log.commandFailed(commandWithArgs, reason)
75 }
76 });
77
78 return Q.resolve(result.spawnedProcess);
79 }
80
81}
82