microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
207lines · 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 | | |
b3ef3553Meena 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 | */ | |
b3ef3553Meena 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 | */ | |
b3ef3553Meena Kunnathur Balakrishnan10 years ago | 82 | public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
f8d32439dlebu10 years ago | 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 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 90 | /** |
| 91 | * Spawns the React Native packager in a child process. | |
| 92 | */ | |
b3ef3553Meena Kunnathur Balakrishnan10 years ago | 93 | public spawnReactPackager(args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
| 94 | let deferred = Q.defer<ChildProcess>(); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 95 | let command = this.getReactCommandName(); |
| 96 | let runArguments = ["start"]; | |
| 97 | | |
| 98 | if (args) { | |
| 99 | runArguments = runArguments.concat(args); | |
| 100 | } | |
| 101 | | |
| 102 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 103 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 104 | let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 105 | result.spawnedProcess.once("error", (error: any) => { |
| 106 | deferred.reject({ error: error }); | |
| 107 | }); | |
| 108 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 109 | result.stderr.on("data", (data: Buffer) => { |
6126d899Meena Kunnathur Balakrishnan10 years ago | 110 | if (outputChannel) { |
| 111 | outputChannel.append(data.toString()); | |
| 112 | } else { | |
| 113 | process.stderr.write(data); | |
| 114 | } | |
| 115 | }); | |
| 116 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 117 | result.stdout.on("data", (data: Buffer) => { |
6126d899Meena Kunnathur Balakrishnan10 years ago | 118 | if (outputChannel) { |
| 119 | outputChannel.append(data.toString()); | |
| 120 | } else { | |
| 121 | process.stdout.write(data); | |
| 122 | } | |
| 123 | }); | |
| 124 | | |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 125 | Q.delay(300).then(() => deferred.resolve(result.spawnedProcess)); |
| 126 | return deferred.promise; | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 127 | } |
| 128 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 129 | /** |
| 130 | * Kills the React Native packager in a child process. | |
| 131 | */ | |
b3ef3553Meena Kunnathur Balakrishnan10 years ago | 132 | public killReactPackager(packagerProcess: ChildProcess, outputChannel?: OutputChannel): void { |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 133 | Log.logMessage("Stopping Packager", outputChannel); |
| 134 | | |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 135 | if (packagerProcess) { |
| 136 | /* To reliably kill the child process on all versions of Windows, | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 137 | * please use taskkill to end the packager process */ |
| 138 | if (process.platform === "win32") { | |
| 139 | new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome.then(() => { | |
| 140 | Log.logMessage("Packager stopped", outputChannel); | |
| 141 | }, function() { | |
| 142 | Log.logError("Failed to exit the React Native packager", outputChannel); | |
| 143 | }); | |
| 144 | } else { | |
| 145 | packagerProcess.kill(); | |
| 146 | Log.logMessage("Packager stopped", outputChannel); | |
| 147 | } | |
| 148 | | |
| 149 | packagerProcess = null; | |
| 150 | } else { | |
| 151 | Log.logMessage("Packager not found", outputChannel); | |
| 152 | } | |
| 153 | } | |
| 154 | | |
| 155 | | |
| 156 | | |
f8d32439dlebu10 years ago | 157 | /** |
| 158 | * Executes a react native command and waits for its completion. | |
| 159 | */ | |
| 160 | public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { | |
| 161 | let runArguments = [command]; | |
| 162 | if (args) { | |
f7208a21Jimmy Thomson10 years ago | 163 | runArguments = runArguments.concat(args); |
f8d32439dlebu10 years ago | 164 | } |
| 165 | return this.spawnAndWaitForCompletion(this.getReactCommandName(), runArguments, options, outputChannel); | |
| 166 | } | |
| 167 | | |
| 168 | /** | |
| 169 | * Resolves the dev machine, desktop platform. | |
| 170 | */ | |
| 171 | private getReactCommandName() { | |
| 172 | let platform = process.platform; | |
| 173 | switch (platform) { | |
| 174 | case "darwin": | |
| 175 | return "react-native"; | |
| 176 | case "win32": | |
| 177 | default: | |
| 178 | return "react-native.cmd"; | |
| 179 | } | |
| 180 | } | |
| 181 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 182 | private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> { |
08e81a53unknown10 years ago | 183 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
3fb37ad5unknown10 years ago | 184 | let commandWithArgs = command + " " + args.join(" "); |
| 185 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 186 | Log.commandStarted(commandWithArgs, outputChannel); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 187 | let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 188 | |
| 189 | result.stderr.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 190 | if (outputChannel) { |
| 191 | outputChannel.append(data.toString()); | |
| 192 | } else { | |
| 193 | process.stderr.write(data); | |
| 194 | } | |
3fb37ad5unknown10 years ago | 195 | }); |
| 196 | | |
| 197 | result.stdout.on("data", (data: Buffer) => { | |
5e5bf86eMeena Kunnathur Balakrishnan10 years ago | 198 | if (outputChannel) { |
| 199 | outputChannel.append(data.toString()); | |
| 200 | } else { | |
| 201 | process.stdout.write(data); | |
| 202 | } | |
3fb37ad5unknown10 years ago | 203 | }); |
| 204 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 205 | return Q.resolve(result); |
3fb37ad5unknown10 years ago | 206 | } |
| 207 | } |