microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
140lines · 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 {ChildProcess} from "child_process"; |
| 5 | import {Log} from "./log"; |
| 6 | import {Node} from "./node/node"; |
| 7 | import {ISpawnResult} from "./node/childProcess"; |
| 8 | import {OutputChannel} from "vscode"; |
| 9 | import * as Q from "q"; |
| 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 | |
| 22 | constructor(currentWorkingDirectory?: string) { |
| 23 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 24 | } |
| 25 | |
| 26 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
| 27 | Log.commandStarted(command); |
| 28 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
| 29 | .then(stdout => { |
| 30 | Log.logMessage(stdout); |
| 31 | Log.commandEnded(command); |
| 32 | }, |
| 33 | reason => Log.commandFailed(command, reason)); |
| 34 | } |
| 35 | |
| 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 | */ |
| 44 | public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
| 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 | }, |
| 50 | (reason) => { |
| 51 | Log.commandFailed(commandWithArgs, reason, outputChannel); |
| 52 | }); |
| 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 | }, |
| 72 | (reason) => { |
| 73 | Log.commandFailed(commandWithArgs, reason, outputChannel); |
| 74 | throw reason; |
| 75 | }); |
| 76 | }); |
| 77 | } |
| 78 | |
| 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) { |
| 85 | runArguments = runArguments.concat(args); |
| 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) { |
| 96 | runArguments = runArguments.concat(args); |
| 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 | |
| 115 | private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> { |
| 116 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 117 | let commandWithArgs = command + " " + args.join(" "); |
| 118 | |
| 119 | Log.commandStarted(commandWithArgs, outputChannel); |
| 120 | let result = new Node.ChildProcess().spawn(command, args, spawnOptions); |
| 121 | |
| 122 | result.stderr.on("data", (data: Buffer) => { |
| 123 | if (outputChannel) { |
| 124 | outputChannel.append(data.toString()); |
| 125 | } else { |
| 126 | process.stderr.write(data); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | result.stdout.on("data", (data: Buffer) => { |
| 131 | if (outputChannel) { |
| 132 | outputChannel.append(data.toString()); |
| 133 | } else { |
| 134 | process.stdout.write(data); |
| 135 | } |
| 136 | }); |
| 137 | |
| 138 | return Q.resolve(result); |
| 139 | } |
| 140 | } |
| 141 | |