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