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