microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
140lines · 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"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 6 | import {Node} from "./node/node"; |
| 7 | import {ISpawnResult} from "./node/childProcess"; | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 8 | import {OutputChannel} from "vscode"; |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 9 | import * as Q from "q"; |
3fb37ad5unknown10 years ago | 10 | |
| 11 | interface EnvironmentOptions { | |
| 12 | REACT_DEBUGGER?: string; | |
| 13 | } | |
| 14 | | |
| 15 | interface Options { | |
| 16 | env?: EnvironmentOptions; | |
| 17 | } | |
| 18 | | |
| 19 | export class CommandExecutor { | |
| 20 | private currentWorkingDirectory: string; | |
| 21 | | |
110558c0Jimmy Thomson10 years ago | 22 | constructor(currentWorkingDirectory?: string) { |
3fb37ad5unknown10 years ago | 23 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 24 | } | |
| 25 | | |
fb2bae06Daniel10 years ago | 26 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
bedf110funknown10 years ago | 27 | Log.commandStarted(command); |
| 28 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) | |
3fb37ad5unknown10 years ago | 29 | .then(stdout => { |
bedf110funknown10 years ago | 30 | Log.logMessage(stdout); |
| 31 | Log.commandEnded(command); | |
3fb37ad5unknown10 years ago | 32 | }, |
bedf110funknown10 years ago | 33 | reason => Log.commandFailed(command, reason)); |
3fb37ad5unknown10 years ago | 34 | } |
| 35 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 36 | /** |
| 37 | * Spawns a child process with the params passed and returns promise of the spawned ChildProcess | |
| 38 | * This method does not wait for the spawned process to finish execution | |
| 39 | * {command} - The command to be invoked in the child process | |
| 40 | * {args} - Arguments to be passed to the command | |
| 41 | * {options} - additional options with which the child process needs to be spawned | |
| 42 | * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed | |
| 43 | */ | |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 44 | public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
45944d15Meena Kunnathur Balakrishnan10 years ago | 45 | return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => { |
| 46 | let commandWithArgs = command + " " + args.join(" "); | |
| 47 | spawnResult.outcome.then(() => { | |
| 48 | Log.commandEnded(commandWithArgs, outputChannel); | |
| 49 | }, | |
f8d32439dlebu10 years ago | 50 | (reason) => { |
| 51 | Log.commandFailed(commandWithArgs, reason, outputChannel); | |
| 52 | }); | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 53 | |
| 54 | return Q.resolve(spawnResult.spawnedProcess); | |
| 55 | }); | |
| 56 | } | |
| 57 | | |
| 58 | /** | |
| 59 | * Spawns a child process with the params passed | |
| 60 | * This method waits until the spawned process finishes execution | |
| 61 | * {command} - The command to be invoked in the child process | |
| 62 | * {args} - Arguments to be passed to the command | |
| 63 | * {options} - additional options with which the child process needs to be spawned | |
| 64 | * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed | |
| 65 | */ | |
| 66 | public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { | |
| 67 | return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => { | |
| 68 | let commandWithArgs = command + " " + args.join(" "); | |
| 69 | return spawnResult.outcome.then(() => { | |
| 70 | Log.commandEnded(commandWithArgs, outputChannel); | |
| 71 | }, | |
f8d32439dlebu10 years ago | 72 | (reason) => { |
| 73 | Log.commandFailed(commandWithArgs, reason, outputChannel); | |
bc96b26bJimmy Thomson10 years ago | 74 | throw reason; |
f8d32439dlebu10 years ago | 75 | }); |
45944d15Meena Kunnathur Balakrishnan10 years ago | 76 | }); |
| 77 | } | |
| 78 | | |
f8d32439dlebu10 years ago | 79 | /** |
| 80 | * Executes a react native command. | |
| 81 | */ | |
| 82 | public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { | |
| 83 | let runArguments = [command]; | |
| 84 | if (args) { | |
f7208a21Jimmy Thomson10 years ago | 85 | runArguments = runArguments.concat(args); |
f8d32439dlebu10 years ago | 86 | } |
| 87 | return this.spawn(this.getReactCommandName(), runArguments, options, outputChannel); | |
| 88 | } | |
| 89 | | |
| 90 | /** | |
| 91 | * Executes a react native command and waits for its completion. | |
| 92 | */ | |
| 93 | public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { | |
| 94 | let runArguments = [command]; | |
| 95 | if (args) { | |
f7208a21Jimmy Thomson10 years ago | 96 | runArguments = runArguments.concat(args); |
f8d32439dlebu10 years ago | 97 | } |
| 98 | return this.spawnAndWaitForCompletion(this.getReactCommandName(), runArguments, options, outputChannel); | |
| 99 | } | |
| 100 | | |
| 101 | /** | |
| 102 | * Resolves the dev machine, desktop platform. | |
| 103 | */ | |
| 104 | private getReactCommandName() { | |
| 105 | let platform = process.platform; | |
| 106 | switch (platform) { | |
| 107 | case "darwin": | |
| 108 | return "react-native"; | |
| 109 | case "win32": | |
| 110 | default: | |
| 111 | return "react-native.cmd"; | |
| 112 | } | |
| 113 | } | |
| 114 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 115 | private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> { |
08e81a53unknown10 years ago | 116 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
3fb37ad5unknown10 years ago | 117 | let commandWithArgs = command + " " + args.join(" "); |
| 118 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 119 | Log.commandStarted(commandWithArgs, outputChannel); |
3fb37ad5unknown10 years ago | 120 | let result = new Node.ChildProcess().spawn(command, args, spawnOptions); |
| 121 | | |
| 122 | result.stderr.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 123 | if (outputChannel) { |
| 124 | outputChannel.append(data.toString()); | |
| 125 | } else { | |
| 126 | process.stderr.write(data); | |
| 127 | } | |
3fb37ad5unknown10 years ago | 128 | }); |
| 129 | | |
| 130 | result.stdout.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 131 | if (outputChannel) { |
| 132 | outputChannel.append(data.toString()); | |
| 133 | } else { | |
| 134 | process.stdout.write(data); | |
| 135 | } | |
3fb37ad5unknown10 years ago | 136 | }); |
| 137 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 138 | return Q.resolve(result); |
3fb37ad5unknown10 years ago | 139 | } |
| 140 | } |