microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/commands/commandExecutor.ts
68lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 4 | import {ChildProcess} from "child_process"; |
3fb37ad5unknown10 years ago | 5 | import {Log} from "./log"; |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 6 | import {Node} from "../node/node"; |
| 7 | import {OutputChannel} from "vscode"; | |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 8 | import * as Q from "q"; |
3fb37ad5unknown10 years ago | 9 | |
| 10 | interface EnvironmentOptions { | |
| 11 | REACT_DEBUGGER?: string; | |
| 12 | } | |
| 13 | | |
| 14 | interface Options { | |
| 15 | env?: EnvironmentOptions; | |
| 16 | } | |
| 17 | | |
| 18 | export class CommandExecutor { | |
| 19 | private currentWorkingDirectory: string; | |
| 20 | | |
110558c0Jimmy Thomson10 years ago | 21 | constructor(currentWorkingDirectory?: string) { |
3fb37ad5unknown10 years ago | 22 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 23 | } | |
| 24 | | |
fb2bae06Daniel10 years ago | 25 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
bedf110funknown10 years ago | 26 | Log.commandStarted(command); |
| 27 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) | |
3fb37ad5unknown10 years ago | 28 | .then(stdout => { |
bedf110funknown10 years ago | 29 | Log.logMessage(stdout); |
| 30 | Log.commandEnded(command); | |
3fb37ad5unknown10 years ago | 31 | }, |
bedf110funknown10 years ago | 32 | reason => Log.commandFailed(command, reason)); |
3fb37ad5unknown10 years ago | 33 | } |
| 34 | | |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 35 | public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
08e81a53unknown10 years ago | 36 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
3fb37ad5unknown10 years ago | 37 | let commandWithArgs = command + " " + args.join(" "); |
| 38 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 39 | Log.commandStarted(commandWithArgs, outputChannel); |
3fb37ad5unknown10 years ago | 40 | let result = new Node.ChildProcess().spawn(command, args, spawnOptions); |
| 41 | | |
| 42 | result.stderr.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 43 | if (outputChannel) { |
| 44 | outputChannel.append(data.toString()); | |
| 45 | } else { | |
| 46 | process.stderr.write(data); | |
| 47 | } | |
3fb37ad5unknown10 years ago | 48 | }); |
| 49 | | |
| 50 | result.stdout.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 51 | if (outputChannel) { |
| 52 | outputChannel.append(data.toString()); | |
| 53 | } else { | |
| 54 | process.stdout.write(data); | |
| 55 | } | |
3fb37ad5unknown10 years ago | 56 | }); |
| 57 | | |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 58 | result.outcome.then(() => { |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 59 | Log.commandEnded(commandWithArgs, outputChannel); |
bedf110funknown10 years ago | 60 | }, |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 61 | (reason) => { |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 62 | Log.commandFailed(commandWithArgs, reason, outputChannel); |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 63 | }); |
| 64 | | |
| 65 | return Q.resolve(result.spawnedProcess); | |
3fb37ad5unknown10 years ago | 66 | } |
| 67 | | |
| 68 | } |