microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/commands/commandExecutor.ts
58lines · 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 | |
| 4 | import * as Q from "q"; |
| 5 | import {Node} from "../node/node"; |
| 6 | import * as _ from "lodash"; |
| 7 | import {Log} from "./log"; |
| 8 | |
| 9 | interface EnvironmentOptions { |
| 10 | REACT_DEBUGGER?: string; |
| 11 | } |
| 12 | |
| 13 | interface Options { |
| 14 | env?: EnvironmentOptions; |
| 15 | } |
| 16 | |
| 17 | export class CommandExecutor { |
| 18 | private currentWorkingDirectory: string; |
| 19 | |
| 20 | constructor(currentWorkingDirectory: string) { |
| 21 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 22 | } |
| 23 | |
| 24 | public execute(subject: string, command: string, options: Options = {}): Q.Promise<void> { |
| 25 | // let outputChannel = vscode.window.createOutputChannel("React Native: " + subject); |
| 26 | Log.commandStarted(command); |
| 27 | // outputChannel.show(); |
| 28 | // let process = child_process.exec(command, {cwd: vscode.workspace.rootPath}); |
| 29 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
| 30 | .then(stdout => { |
| 31 | Log.logMessage(stdout); |
| 32 | Log.commandEnded(command); |
| 33 | }, |
| 34 | reason => Log.commandFailed(command, reason)); |
| 35 | } |
| 36 | |
| 37 | public spawn(subject: string, command: string, args: string[], options: Options = {}): Q.Promise<void> { |
| 38 | let spawnOptions = _.extend({}, { cwd: this.currentWorkingDirectory }, options); |
| 39 | let commandWithArgs = command + " " + args.join(" "); |
| 40 | |
| 41 | Log.commandStarted(commandWithArgs); |
| 42 | let result = new Node.ChildProcess().spawn(command, args, spawnOptions); |
| 43 | |
| 44 | result.stderr.on("data", (data: Buffer) => { |
| 45 | process.stdout.write(data); |
| 46 | }); |
| 47 | |
| 48 | result.stdout.on("data", (data: Buffer) => { |
| 49 | process.stdout.write(data); |
| 50 | }); |
| 51 | |
| 52 | return result.outcome.then(() => { |
| 53 | Log.commandEnded(commandWithArgs); |
| 54 | }, |
| 55 | reason => Log.commandFailed(commandWithArgs, reason)); |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | |