microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
65ee84c9bbc97865fbc32d7fc3649e3096ad582d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/commands/commandExecutor.ts

55lines · modepreview

import * as Q from "q";
import {Node} from "../node/node";
import * as _ from "lodash";
import {Log} from "./log";

interface EnvironmentOptions {
    REACT_DEBUGGER?: string;
}

interface Options {
    env?: EnvironmentOptions;
}

export class CommandExecutor {
    private currentWorkingDirectory: string;

    constructor(currentWorkingDirectory: string) {
        this.currentWorkingDirectory = currentWorkingDirectory;
    }

    public execute(subject: string, command: string, options: Options = {}): Q.Promise<void> {
        // let outputChannel = vscode.window.createOutputChannel("React Native: " + subject);
        Log.commandStarted(command);
        // outputChannel.show();
        // let process = child_process.exec(command, {cwd: vscode.workspace.rootPath});
        return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
            .then(stdout => {
                Log.logMessage(stdout);
                Log.commandEnded(command);
            },
            reason => Log.commandFailed(command, reason));
    }

    public spawn(subject: string, command: string, args: string[], options: Options = {}): Q.Promise<void> {
        let spawnOptions = _.extend({}, { cwd: this.currentWorkingDirectory }, options);
        let commandWithArgs = command + " " + args.join(" ");

        Log.commandStarted(commandWithArgs);
        let result = new Node.ChildProcess().spawn(command, args, spawnOptions);

        result.stderr.on("data", (data: Buffer) => {
            process.stdout.write(data);
        });

        result.stdout.on("data", (data: Buffer) => {
            process.stdout.write(data);
        });

        return result.outcome.then(() => {
            Log.commandEnded(commandWithArgs);
        },
            reason => Log.commandFailed(commandWithArgs, reason));
    }

}