microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
228lines · 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"; |
0a68f8dbArtem Egorov8 years ago | 6 | import {ILogger} from "../extension/log/LogHelper"; |
| 7 | import { NullLogger } from "../extension/log/NullLogger"; | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 8 | import {Node} from "./node/node"; |
cb6d0922digeff10 years ago | 9 | import {ISpawnResult} from "./node/childProcess"; |
5c8365a6Artem Egorov8 years ago | 10 | import {HostPlatform, HostPlatformId} from "./hostPlatform"; |
190e393cMeena Kunnathur Balakrishnan10 years ago | 11 | import {ErrorHelper} from "./error/errorHelper"; |
| 12 | import {InternalErrorCode} from "./error/internalErrorCode"; | |
3fb37ad5unknown10 years ago | 13 | |
831f4a85Patricio Beltran9 years ago | 14 | export enum CommandVerbosity { |
| 15 | OUTPUT, | |
| 16 | SILENT, | |
| 17 | PROGRESS, | |
| 18 | } | |
| 19 | | |
3fb37ad5unknown10 years ago | 20 | interface EnvironmentOptions { |
| 21 | REACT_DEBUGGER?: string; | |
| 22 | } | |
| 23 | | |
| 24 | interface Options { | |
| 25 | env?: EnvironmentOptions; | |
831f4a85Patricio Beltran9 years ago | 26 | verbosity?: CommandVerbosity; |
0d827d9bJimmy Thomson9 years ago | 27 | cwd?: string; |
3fb37ad5unknown10 years ago | 28 | } |
| 29 | | |
17161993Meena Kunnathur Balakrishnan10 years ago | 30 | export enum CommandStatus { |
| 31 | Start = 0, | |
27710197Vladimir Kotikov8 years ago | 32 | End = 1, |
17161993Meena Kunnathur Balakrishnan10 years ago | 33 | } |
| 34 | | |
3fb37ad5unknown10 years ago | 35 | export class CommandExecutor { |
94cd5149Artem Egorov8 years ago | 36 | |
bbdbbb3bdlebu10 years ago | 37 | private static ReactNativeCommand = "react-native"; |
b8a56999Patricio Beltran9 years ago | 38 | private static ReactNativeVersionCommand = "-v"; |
9596aa53digeff10 years ago | 39 | private childProcess = new Node.ChildProcess(); |
3fb37ad5unknown10 years ago | 40 | |
0a68f8dbArtem Egorov8 years ago | 41 | constructor( |
| 42 | private currentWorkingDirectory: string = process.cwd(), | |
| 43 | private logger: ILogger = new NullLogger() | |
| 44 | ) { } | |
3fb37ad5unknown10 years ago | 45 | |
fb2bae06Daniel10 years ago | 46 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 47 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start)); |
9596aa53digeff10 years ago | 48 | return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
3fb37ad5unknown10 years ago | 49 | .then(stdout => { |
0a68f8dbArtem Egorov8 years ago | 50 | this.logger.info(stdout); |
| 51 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End)); | |
3fb37ad5unknown10 years ago | 52 | }, |
cb6d0922digeff10 years ago | 53 | (reason: Error) => |
| 54 | this.generateRejectionForCommand(command, reason)); | |
3fb37ad5unknown10 years ago | 55 | } |
| 56 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 57 | /** |
| 58 | * Spawns a child process with the params passed | |
| 59 | * This method waits until the spawned process finishes execution | |
| 60 | * {command} - The command to be invoked in the child process | |
| 61 | * {args} - Arguments to be passed to the command | |
| 62 | * {options} - additional options with which the child process needs to be spawned | |
| 63 | */ | |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 64 | public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> { |
9596aa53digeff10 years ago | 65 | return this.spawnChildProcess(command, args, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 66 | } |
| 67 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 68 | /** |
| 69 | * Spawns the React Native packager in a child process. | |
| 70 | */ | |
9596aa53digeff10 years ago | 71 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 72 | return this.spawnReactCommand("start", args, options); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 73 | } |
| 74 | | |
b8a56999Patricio Beltran9 years ago | 75 | /** |
| 76 | * Uses the `react-native -v` command to get the version used on the project. | |
| 77 | * Returns null if the workspace is not a react native project | |
| 78 | */ | |
| 79 | public getReactNativeVersion(): Q.Promise<string> { | |
| 80 | let deferred = Q.defer<string>(); | |
| 81 | const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); | |
| 82 | let output = ""; | |
| 83 | | |
| 84 | const result = this.childProcess.spawn(reactCommand, | |
| 85 | [CommandExecutor.ReactNativeVersionCommand], | |
| 86 | { cwd: this.currentWorkingDirectory }); | |
| 87 | | |
| 88 | result.stdout.on("data", (data: Buffer) => { | |
| 89 | output += data.toString(); | |
| 90 | }); | |
| 91 | | |
| 92 | result.stdout.on("end", () => { | |
| 93 | const match = output.match(/react-native: ([\d\.]+)/); | |
5c8365a6Artem Egorov8 years ago | 94 | deferred.resolve(match && match[1] || ""); |
b8a56999Patricio Beltran9 years ago | 95 | }); |
| 96 | | |
| 97 | return deferred.promise; | |
| 98 | } | |
| 99 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 100 | /** |
| 101 | * Kills the React Native packager in a child process. | |
| 102 | */ | |
5c8365a6Artem Egorov8 years ago | 103 | public killReactPackager(packagerProcess?: ChildProcess): Q.Promise<void> { |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 104 | if (packagerProcess) { |
a1005420dlebu10 years ago | 105 | return Q({}).then(() => { |
| 106 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { | |
9596aa53digeff10 years ago | 107 | return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome; |
a1005420dlebu10 years ago | 108 | } else { |
| 109 | packagerProcess.kill(); | |
5c8365a6Artem Egorov8 years ago | 110 | return Q.resolve(void 0); |
a1005420dlebu10 years ago | 111 | } |
| 112 | }).then(() => { | |
0a68f8dbArtem Egorov8 years ago | 113 | this.logger.info("Packager stopped"); |
a1005420dlebu10 years ago | 114 | }); |
| 115 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 116 | } else { |
0a68f8dbArtem Egorov8 years ago | 117 | this.logger.warning("Packager not found"); |
10873e11digeff10 years ago | 118 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 119 | } |
| 120 | } | |
| 121 | | |
f8d32439dlebu10 years ago | 122 | /** |
| 123 | * Executes a react native command and waits for its completion. | |
| 124 | */ | |
e390ca0aVladimir Kotikov8 years ago | 125 | public spawnReactCommand(command: string, args: string[] = [], options: Options = {}): ISpawnResult { |
9596aa53digeff10 years ago | 126 | const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); |
94cd5149Artem Egorov8 years ago | 127 | return this.spawnChildProcess(reactCommand, [command, ...args], options); |
5b0582f3digeff10 years ago | 128 | } |
| 129 | | |
831f4a85Patricio Beltran9 years ago | 130 | /** |
| 131 | * Spawns a child process with the params passed | |
| 132 | * This method has logic to do while the command is executing | |
| 133 | * {command} - The command to be invoked in the child process | |
| 134 | * {args} - Arguments to be passed to the command | |
| 135 | * {options} - additional options with which the child process needs to be spawned | |
| 136 | */ | |
| 137 | public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> { | |
| 138 | let deferred = Q.defer<void>(); | |
| 139 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 140 | const commandWithArgs = command + " " + args.join(" "); | |
| 141 | const timeBetweenDots = 1500; | |
b0af599cJimmy Thomson9 years ago | 142 | let lastDotTime = 0; |
831f4a85Patricio Beltran9 years ago | 143 | |
| 144 | const printDot = () => { | |
b0af599cJimmy Thomson9 years ago | 145 | const now = Date.now(); |
| 146 | if (now - lastDotTime > timeBetweenDots) { | |
| 147 | lastDotTime = now; | |
0a68f8dbArtem Egorov8 years ago | 148 | this.logger.logStream(".", process.stdout); |
b0af599cJimmy Thomson9 years ago | 149 | } |
831f4a85Patricio Beltran9 years ago | 150 | }; |
| 151 | | |
| 152 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 153 | this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start)); |
831f4a85Patricio Beltran9 years ago | 154 | } |
| 155 | | |
| 156 | const result = this.childProcess.spawn(command, args, spawnOptions); | |
| 157 | | |
| 158 | result.stdout.on("data", (data: Buffer) => { | |
| 159 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 160 | this.logger.logStream(data, process.stdout); |
b0af599cJimmy Thomson9 years ago | 161 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 162 | printDot(); |
| 163 | } | |
| 164 | }); | |
| 165 | | |
| 166 | result.stderr.on("data", (data: Buffer) => { | |
| 167 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 168 | this.logger.logStream(data, process.stderr); |
b0af599cJimmy Thomson9 years ago | 169 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 170 | printDot(); |
| 171 | } | |
| 172 | }); | |
| 173 | | |
| 174 | result.outcome = result.outcome.then( | |
| 175 | () => { | |
| 176 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 177 | this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End)); |
831f4a85Patricio Beltran9 years ago | 178 | } |
0a68f8dbArtem Egorov8 years ago | 179 | this.logger.logStream("\n", process.stdout); |
831f4a85Patricio Beltran9 years ago | 180 | deferred.resolve(void 0); |
| 181 | }, | |
| 182 | reason => { | |
0d827d9bJimmy Thomson9 years ago | 183 | deferred.reject(reason); |
831f4a85Patricio Beltran9 years ago | 184 | return this.generateRejectionForCommand(commandWithArgs, reason); |
| 185 | }); | |
| 186 | return deferred.promise; | |
| 187 | } | |
| 188 | | |
9596aa53digeff10 years ago | 189 | private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult { |
| 190 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 191 | const commandWithArgs = command + " " + args.join(" "); | |
3fb37ad5unknown10 years ago | 192 | |
0a68f8dbArtem Egorov8 years ago | 193 | this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start)); |
9596aa53digeff10 years ago | 194 | const result = this.childProcess.spawn(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 195 | |
| 196 | result.stderr.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 197 | this.logger.logStream(data, process.stderr); |
3fb37ad5unknown10 years ago | 198 | }); |
| 199 | | |
| 200 | result.stdout.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 201 | this.logger.logStream(data, process.stdout); |
3fb37ad5unknown10 years ago | 202 | }); |
| 203 | | |
10873e11digeff10 years ago | 204 | result.outcome = result.outcome.then( |
9596aa53digeff10 years ago | 205 | () => |
0a68f8dbArtem Egorov8 years ago | 206 | this.logger.debug(CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End)), |
10873e11digeff10 years ago | 207 | reason => |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 208 | this.generateRejectionForCommand(commandWithArgs, reason)); |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 209 | return result; |
3fb37ad5unknown10 years ago | 210 | } |
10873e11digeff10 years ago | 211 | |
3677173cdigeff10 years ago | 212 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 213 | return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command)); |
10873e11digeff10 years ago | 214 | } |
0a68f8dbArtem Egorov8 years ago | 215 | |
| 216 | private static getCommandStatusString(command: string, status: CommandStatus) { | |
| 217 | switch (status) { | |
| 218 | case CommandStatus.Start: | |
| 219 | return `Executing command: ${command}`; | |
| 220 | | |
| 221 | case CommandStatus.End: | |
| 222 | return `Finished executing: ${command}`; | |
| 223 | | |
| 224 | default: | |
| 225 | throw new Error("Unsupported command status"); | |
| 226 | } | |
| 227 | } | |
3fb37ad5unknown10 years ago | 228 | } |