microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
192lines · 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 | | |
10873e11digeff10 years ago | 4 | import * as Q from "q"; |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 5 | import {ChildProcess} from "child_process"; |
3fb37ad5unknown10 years ago | 6 | import {Log} from "./log"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 7 | import {Node} from "./node/node"; |
cb6d0922digeff10 years ago | 8 | import {ISpawnResult} from "./node/childProcess"; |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 9 | import {OutputChannel} from "vscode"; |
10873e11digeff10 years ago | 10 | import {NestedError} from "./nestedError"; |
3fb37ad5unknown10 years ago | 11 | |
| 12 | interface EnvironmentOptions { | |
| 13 | REACT_DEBUGGER?: string; | |
| 14 | } | |
| 15 | | |
| 16 | interface Options { | |
| 17 | env?: EnvironmentOptions; | |
| 18 | } | |
| 19 | | |
| 20 | export class CommandExecutor { | |
| 21 | private currentWorkingDirectory: string; | |
| 22 | | |
110558c0Jimmy Thomson10 years ago | 23 | constructor(currentWorkingDirectory?: string) { |
3fb37ad5unknown10 years ago | 24 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 25 | } | |
| 26 | | |
fb2bae06Daniel10 years ago | 27 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
bedf110funknown10 years ago | 28 | Log.commandStarted(command); |
| 29 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) | |
3fb37ad5unknown10 years ago | 30 | .then(stdout => { |
bedf110funknown10 years ago | 31 | Log.logMessage(stdout); |
| 32 | Log.commandEnded(command); | |
3fb37ad5unknown10 years ago | 33 | }, |
cb6d0922digeff10 years ago | 34 | (reason: Error) => |
| 35 | this.generateRejectionForCommand(command, reason)); | |
3fb37ad5unknown10 years ago | 36 | } |
| 37 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 38 | /** |
| 39 | * Spawns a child process with the params passed and returns promise of the spawned ChildProcess | |
| 40 | * This method does not wait for the spawned process to finish execution | |
| 41 | * {command} - The command to be invoked in the child process | |
| 42 | * {args} - Arguments to be passed to the command | |
| 43 | * {options} - additional options with which the child process needs to be spawned | |
| 44 | * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed | |
| 45 | */ | |
5b0582f3digeff10 years ago | 46 | public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess { |
| 47 | return this.spawnChildProcess(command, args, options, outputChannel).spawnedProcess; | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 48 | } |
| 49 | | |
| 50 | /** | |
| 51 | * Spawns a child process with the params passed | |
| 52 | * This method waits until the spawned process finishes execution | |
| 53 | * {command} - The command to be invoked in the child process | |
| 54 | * {args} - Arguments to be passed to the command | |
| 55 | * {options} - additional options with which the child process needs to be spawned | |
| 56 | * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed | |
| 57 | */ | |
3d69a9b4digeff10 years ago | 58 | public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { |
5b0582f3digeff10 years ago | 59 | return this.spawnChildProcess(command, args, options, outputChannel).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 60 | } |
| 61 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 62 | /** |
| 63 | * Spawns the React Native packager in a child process. | |
| 64 | */ | |
b3ef3553Meena Kunnathur Balakrishnan10 years ago | 65 | public spawnReactPackager(args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
| 66 | let deferred = Q.defer<ChildProcess>(); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 67 | let command = this.getReactCommandName(); |
| 68 | let runArguments = ["start"]; | |
| 69 | | |
| 70 | if (args) { | |
| 71 | runArguments = runArguments.concat(args); | |
| 72 | } | |
| 73 | | |
| 74 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 75 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 76 | let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 77 | result.spawnedProcess.once("error", (error: any) => { |
cb6d0922digeff10 years ago | 78 | deferred.reject(new NestedError(`Error while executing React Native Packager`, error)); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 79 | }); |
| 80 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 81 | result.stderr.on("data", (data: Buffer) => { |
6126d899Meena Kunnathur Balakrishnan10 years ago | 82 | if (outputChannel) { |
| 83 | outputChannel.append(data.toString()); | |
| 84 | } else { | |
| 85 | process.stderr.write(data); | |
| 86 | } | |
| 87 | }); | |
| 88 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 89 | result.stdout.on("data", (data: Buffer) => { |
6126d899Meena Kunnathur Balakrishnan10 years ago | 90 | if (outputChannel) { |
| 91 | outputChannel.append(data.toString()); | |
| 92 | } else { | |
| 93 | process.stdout.write(data); | |
| 94 | } | |
| 95 | }); | |
| 96 | | |
65fd8e85digeff10 years ago | 97 | // TODO #83 - PROMISE: We need to consume result.outcome here |
10873e11digeff10 years ago | 98 | Q.delay(300).done(() => deferred.resolve(result.spawnedProcess)); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 99 | return deferred.promise; |
6126d899Meena Kunnathur Balakrishnan10 years ago | 100 | } |
| 101 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 102 | /** |
| 103 | * Kills the React Native packager in a child process. | |
| 104 | */ | |
10873e11digeff10 years ago | 105 | public killReactPackager(packagerProcess: ChildProcess, outputChannel?: OutputChannel): Q.Promise<void> { |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 106 | Log.logMessage("Stopping Packager", outputChannel); |
| 107 | | |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 108 | if (packagerProcess) { |
| 109 | /* To reliably kill the child process on all versions of Windows, | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 110 | * please use taskkill to end the packager process */ |
| 111 | if (process.platform === "win32") { | |
8ee905e8digeff10 years ago | 112 | return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome.then(() => { |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 113 | Log.logMessage("Packager stopped", outputChannel); |
| 114 | }); | |
| 115 | } else { | |
| 116 | packagerProcess.kill(); | |
| 117 | Log.logMessage("Packager stopped", outputChannel); | |
8ee905e8digeff10 years ago | 118 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 119 | } |
| 120 | } else { | |
| 121 | Log.logMessage("Packager not found", outputChannel); | |
10873e11digeff10 years ago | 122 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 123 | } |
| 124 | } | |
| 125 | | |
| 126 | | |
| 127 | | |
f8d32439dlebu10 years ago | 128 | /** |
| 129 | * Executes a react native command and waits for its completion. | |
| 130 | */ | |
3d69a9b4digeff10 years ago | 131 | public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { |
5b0582f3digeff10 years ago | 132 | return this.spawnChildReactCommandProcess(command, args, options, outputChannel).outcome; |
| 133 | } | |
| 134 | | |
| 135 | public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult { | |
f8d32439dlebu10 years ago | 136 | let runArguments = [command]; |
| 137 | if (args) { | |
f7208a21Jimmy Thomson10 years ago | 138 | runArguments = runArguments.concat(args); |
f8d32439dlebu10 years ago | 139 | } |
5b0582f3digeff10 years ago | 140 | return this.spawnChildProcess(this.getReactCommandName(), runArguments, options, outputChannel); |
f8d32439dlebu10 years ago | 141 | } |
| 142 | | |
| 143 | /** | |
| 144 | * Resolves the dev machine, desktop platform. | |
| 145 | */ | |
| 146 | private getReactCommandName() { | |
| 147 | let platform = process.platform; | |
| 148 | switch (platform) { | |
| 149 | case "darwin": | |
| 150 | return "react-native"; | |
| 151 | case "win32": | |
| 152 | default: | |
| 153 | return "react-native.cmd"; | |
| 154 | } | |
| 155 | } | |
| 156 | | |
5b0582f3digeff10 years ago | 157 | private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult { |
08e81a53unknown10 years ago | 158 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
3fb37ad5unknown10 years ago | 159 | let commandWithArgs = command + " " + args.join(" "); |
| 160 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 161 | Log.commandStarted(commandWithArgs, outputChannel); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 162 | let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 163 | |
| 164 | result.stderr.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 165 | if (outputChannel) { |
| 166 | outputChannel.append(data.toString()); | |
| 167 | } else { | |
| 168 | process.stderr.write(data); | |
| 169 | } | |
3fb37ad5unknown10 years ago | 170 | }); |
| 171 | | |
| 172 | result.stdout.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 173 | if (outputChannel) { |
| 174 | outputChannel.append(data.toString()); | |
| 175 | } else { | |
| 176 | process.stdout.write(data); | |
| 177 | } | |
3fb37ad5unknown10 years ago | 178 | }); |
| 179 | | |
10873e11digeff10 years ago | 180 | result.outcome = result.outcome.then( |
| 181 | () => | |
| 182 | Log.commandEnded(commandWithArgs, outputChannel), | |
| 183 | reason => | |
3677173cdigeff10 years ago | 184 | this.generateRejectionForCommand(command, reason)); |
5b0582f3digeff10 years ago | 185 | |
| 186 | return result; | |
3fb37ad5unknown10 years ago | 187 | } |
10873e11digeff10 years ago | 188 | |
3677173cdigeff10 years ago | 189 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
10873e11digeff10 years ago | 190 | return Q.reject<void>(new NestedError(`Error while executing: ${command}`, reason)); |
| 191 | } | |
3fb37ad5unknown10 years ago | 192 | } |