microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
281lines · 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 | | |
af1474acRedMickey6 years ago | 4 | import * as path from "path"; |
ce5e88eeYuri Skorokhodov5 years ago | 5 | import * as cp from "child_process"; |
09f6024fHeniker4 years ago | 6 | import * as nls from "vscode-nls"; |
34472878RedMickey5 years ago | 7 | import { ILogger } from "../extension/log/LogHelper"; |
| 8 | import { NullLogger } from "../extension/log/NullLogger"; | |
09f6024fHeniker4 years ago | 9 | import { ProjectVersionHelper } from "./projectVersionHelper"; |
34472878RedMickey5 years ago | 10 | import { ISpawnResult } from "./node/childProcess"; |
| 11 | import { HostPlatform, HostPlatformId } from "./hostPlatform"; | |
| 12 | import { ErrorHelper } from "./error/errorHelper"; | |
| 13 | import { InternalErrorCode } from "./error/internalErrorCode"; | |
ce5e88eeYuri Skorokhodov5 years ago | 14 | import { Node } from "./node/node"; |
09f6024fHeniker4 years ago | 15 | |
34472878RedMickey5 years ago | 16 | nls.config({ |
| 17 | messageFormat: nls.MessageFormat.bundle, | |
| 18 | bundleFormat: nls.BundleFormat.standalone, | |
| 19 | })(); | |
e2644f37Yuri Skorokhodov7 years ago | 20 | const localize = nls.loadMessageBundle(); |
3fb37ad5unknown10 years ago | 21 | |
831f4a85Patricio Beltran9 years ago | 22 | export enum CommandVerbosity { |
| 23 | OUTPUT, | |
| 24 | SILENT, | |
| 25 | PROGRESS, | |
| 26 | } | |
| 27 | | |
3fb37ad5unknown10 years ago | 28 | interface EnvironmentOptions { |
| 29 | REACT_DEBUGGER?: string; | |
| 30 | } | |
| 31 | | |
| 32 | interface Options { | |
| 33 | env?: EnvironmentOptions; | |
831f4a85Patricio Beltran9 years ago | 34 | verbosity?: CommandVerbosity; |
0d827d9bJimmy Thomson9 years ago | 35 | cwd?: string; |
3fb37ad5unknown10 years ago | 36 | } |
| 37 | | |
17161993Meena Kunnathur Balakrishnan10 years ago | 38 | export enum CommandStatus { |
| 39 | Start = 0, | |
27710197Vladimir Kotikov8 years ago | 40 | End = 1, |
17161993Meena Kunnathur Balakrishnan10 years ago | 41 | } |
| 42 | | |
3fb37ad5unknown10 years ago | 43 | export class CommandExecutor { |
af1474acRedMickey6 years ago | 44 | public static ReactNativeCommand: string | null; |
9596aa53digeff10 years ago | 45 | private childProcess = new Node.ChildProcess(); |
3fb37ad5unknown10 years ago | 46 | |
0a68f8dbArtem Egorov8 years ago | 47 | constructor( |
4dfb1c4cetatanova5 years ago | 48 | private nodeModulesRoot: string, |
0a68f8dbArtem Egorov8 years ago | 49 | private currentWorkingDirectory: string = process.cwd(), |
34472878RedMickey5 years ago | 50 | private logger: ILogger = new NullLogger(), |
| 51 | ) {} | |
3fb37ad5unknown10 years ago | 52 | |
0d77292aJiglioNero4 years ago | 53 | public async execute(command: string, options: Options = {}): Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 54 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start)); |
0d77292aJiglioNero4 years ago | 55 | try { |
| 56 | const stdout = await this.childProcess.execToString(command, { | |
| 57 | cwd: this.currentWorkingDirectory, | |
| 58 | env: options.env, | |
| 59 | }); | |
| 60 | this.logger.info(stdout); | |
| 61 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End)); | |
| 62 | } catch (reason) { | |
| 63 | return this.generateRejectionForCommand(command, reason); | |
| 64 | } | |
3fb37ad5unknown10 years ago | 65 | } |
| 66 | | |
57fee98eEzio Li3 years ago | 67 | public async executeToString(command: string, options: Options = {}): Promise<string> { |
| 68 | try { | |
| 69 | const stdout = await this.childProcess.execToString(command, { | |
| 70 | cwd: this.currentWorkingDirectory, | |
| 71 | env: options.env, | |
| 72 | }); | |
| 73 | return stdout; | |
| 74 | } catch (reason) { | |
| 75 | return reason; | |
| 76 | } | |
| 77 | } | |
| 78 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 79 | /** |
| 80 | * Spawns a child process with the params passed | |
| 81 | * This method waits until the spawned process finishes execution | |
| 82 | * {command} - The command to be invoked in the child process | |
| 83 | * {args} - Arguments to be passed to the command | |
| 84 | * {options} - additional options with which the child process needs to be spawned | |
| 85 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 86 | public spawn(command: string, args: string[], options: Options = {}): Promise<any> { |
9596aa53digeff10 years ago | 87 | return this.spawnChildProcess(command, args, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 88 | } |
| 89 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 90 | /** |
| 91 | * Spawns the React Native packager in a child process. | |
| 92 | */ | |
9596aa53digeff10 years ago | 93 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 94 | return this.spawnReactCommand("start", args, options); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 95 | } |
| 96 | | |
efb84653Ezio Li2 years ago | 97 | /** |
| 98 | * Spawns the React Native packager in a child process. | |
| 99 | */ | |
| 100 | public spawnExpoPackager(args: string[], options: Options = {}): ISpawnResult { | |
| 101 | return this.spawnExpoCommand("start", args, options); | |
| 102 | } | |
| 103 | | |
0d77292aJiglioNero4 years ago | 104 | public async getReactNativeVersion(): Promise<string> { |
| 105 | const versions = await ProjectVersionHelper.getReactNativeVersions( | |
| 106 | this.currentWorkingDirectory, | |
34472878RedMickey5 years ago | 107 | ); |
0d77292aJiglioNero4 years ago | 108 | return versions.reactNativeVersion; |
b8a56999Patricio Beltran9 years ago | 109 | } |
| 110 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 111 | /** |
| 112 | * Kills the React Native packager in a child process. | |
| 113 | */ | |
0d77292aJiglioNero4 years ago | 114 | public async killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> { |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 115 | if (packagerProcess) { |
0d77292aJiglioNero4 years ago | 116 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { |
| 117 | const res = await this.childProcess.exec( | |
09f6024fHeniker4 years ago | 118 | `taskkill /pid ${packagerProcess.pid} /T /F`, |
0d77292aJiglioNero4 years ago | 119 | ); |
| 120 | await res.outcome; | |
| 121 | } else { | |
| 122 | packagerProcess.kill(); | |
| 123 | } | |
| 124 | this.logger.info(localize("PackagerStopped", "Packager stopped")); | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 125 | } else { |
e2644f37Yuri Skorokhodov7 years ago | 126 | this.logger.warning(localize("PackagerNotFound", "Packager not found")); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 127 | } |
| 128 | } | |
| 129 | | |
f8d32439dlebu10 years ago | 130 | /** |
| 131 | * Executes a react native command and waits for its completion. | |
| 132 | */ | |
34472878RedMickey5 years ago | 133 | public spawnReactCommand( |
| 134 | command: string, | |
| 135 | args: string[] = [], | |
| 136 | options: Options = {}, | |
| 137 | ): ISpawnResult { | |
af1474acRedMickey6 years ago | 138 | const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI()); |
94cd5149Artem Egorov8 years ago | 139 | return this.spawnChildProcess(reactCommand, [command, ...args], options); |
5b0582f3digeff10 years ago | 140 | } |
| 141 | | |
efb84653Ezio Li2 years ago | 142 | /** |
| 143 | * Executes a react native command and waits for its completion. | |
| 144 | */ | |
| 145 | public spawnExpoCommand( | |
| 146 | command: string, | |
| 147 | args: string[] = [], | |
| 148 | options: Options = {}, | |
| 149 | ): ISpawnResult { | |
| 150 | const expoCommand = HostPlatform.getNpmCliCommand(this.selectExpoCLI()); | |
| 151 | return this.spawnChildProcess(expoCommand, [command, ...args], options); | |
| 152 | } | |
| 153 | | |
831f4a85Patricio Beltran9 years ago | 154 | /** |
| 155 | * Spawns a child process with the params passed | |
| 156 | * This method has logic to do while the command is executing | |
| 157 | * {command} - The command to be invoked in the child process | |
| 158 | * {args} - Arguments to be passed to the command | |
| 159 | * {options} - additional options with which the child process needs to be spawned | |
| 160 | */ | |
0d77292aJiglioNero4 years ago | 161 | public async spawnWithProgress( |
34472878RedMickey5 years ago | 162 | command: string, |
| 163 | args: string[], | |
| 164 | options: Options = { verbosity: CommandVerbosity.OUTPUT }, | |
| 165 | ): Promise<void> { | |
831f4a85Patricio Beltran9 years ago | 166 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
09f6024fHeniker4 years ago | 167 | const commandWithArgs = `${command} ${args.join(" ")}`; |
831f4a85Patricio Beltran9 years ago | 168 | const timeBetweenDots = 1500; |
b0af599cJimmy Thomson9 years ago | 169 | let lastDotTime = 0; |
831f4a85Patricio Beltran9 years ago | 170 | |
| 171 | const printDot = () => { | |
b0af599cJimmy Thomson9 years ago | 172 | const now = Date.now(); |
| 173 | if (now - lastDotTime > timeBetweenDots) { | |
| 174 | lastDotTime = now; | |
0a68f8dbArtem Egorov8 years ago | 175 | this.logger.logStream(".", process.stdout); |
b0af599cJimmy Thomson9 years ago | 176 | } |
831f4a85Patricio Beltran9 years ago | 177 | }; |
| 178 | | |
| 179 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
34472878RedMickey5 years ago | 180 | this.logger.debug( |
| 181 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), | |
| 182 | ); | |
831f4a85Patricio Beltran9 years ago | 183 | } |
| 184 | | |
| 185 | const result = this.childProcess.spawn(command, args, spawnOptions); | |
| 186 | | |
| 187 | result.stdout.on("data", (data: Buffer) => { | |
| 188 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 189 | this.logger.logStream(data, process.stdout); |
b0af599cJimmy Thomson9 years ago | 190 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 191 | printDot(); |
| 192 | } | |
| 193 | }); | |
| 194 | | |
| 195 | result.stderr.on("data", (data: Buffer) => { | |
| 196 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 197 | this.logger.logStream(data, process.stderr); |
b0af599cJimmy Thomson9 years ago | 198 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 199 | printDot(); |
| 200 | } | |
| 201 | }); | |
| 202 | | |
0d77292aJiglioNero4 years ago | 203 | try { |
| 204 | await result.outcome; | |
| 205 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
| 206 | this.logger.debug( | |
| 207 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End), | |
| 208 | ); | |
| 209 | } | |
| 210 | this.logger.logStream("\n", process.stdout); | |
| 211 | } catch (reason) { | |
| 212 | return this.generateRejectionForCommand(commandWithArgs, reason); | |
| 213 | } | |
831f4a85Patricio Beltran9 years ago | 214 | } |
| 215 | | |
af1474acRedMickey6 years ago | 216 | public selectReactNativeCLI(): string { |
34472878RedMickey5 years ago | 217 | return ( |
| 218 | CommandExecutor.ReactNativeCommand || | |
4dfb1c4cetatanova5 years ago | 219 | path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native") |
34472878RedMickey5 years ago | 220 | ); |
af1474acRedMickey6 years ago | 221 | } |
| 222 | | |
efb84653Ezio Li2 years ago | 223 | public selectExpoCLI(): string { |
| 224 | return ( | |
| 225 | CommandExecutor.ReactNativeCommand || | |
| 226 | path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "expo") | |
| 227 | ); | |
| 228 | } | |
| 229 | | |
34472878RedMickey5 years ago | 230 | private spawnChildProcess( |
| 231 | command: string, | |
| 232 | args: string[], | |
| 233 | options: Options = {}, | |
| 234 | ): ISpawnResult { | |
77e86943lexie0111 years ago | 235 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options, { |
| 236 | shell: true, | |
| 237 | }); | |
09f6024fHeniker4 years ago | 238 | const commandWithArgs = `${command} ${args.join(" ")}`; |
3fb37ad5unknown10 years ago | 239 | |
34472878RedMickey5 years ago | 240 | this.logger.debug( |
| 241 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), | |
| 242 | ); | |
9596aa53digeff10 years ago | 243 | const result = this.childProcess.spawn(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 244 | |
| 245 | result.stderr.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 246 | this.logger.logStream(data, process.stderr); |
3fb37ad5unknown10 years ago | 247 | }); |
| 248 | | |
| 249 | result.stdout.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 250 | this.logger.logStream(data, process.stdout); |
3fb37ad5unknown10 years ago | 251 | }); |
| 252 | | |
10873e11digeff10 years ago | 253 | result.outcome = result.outcome.then( |
9596aa53digeff10 years ago | 254 | () => |
34472878RedMickey5 years ago | 255 | this.logger.debug( |
| 256 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End), | |
| 257 | ), | |
| 258 | reason => this.generateRejectionForCommand(commandWithArgs, reason), | |
| 259 | ); | |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 260 | return result; |
3fb37ad5unknown10 years ago | 261 | } |
10873e11digeff10 years ago | 262 | |
ce5e88eeYuri Skorokhodov5 years ago | 263 | private generateRejectionForCommand(command: string, reason: any): Promise<void> { |
34472878RedMickey5 years ago | 264 | return Promise.reject<void>( |
60ad4ec0JiglioNero5 years ago | 265 | reason.errorCode === InternalErrorCode.CommandFailed |
| 266 | ? reason | |
| 267 | : ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command), | |
34472878RedMickey5 years ago | 268 | ); |
10873e11digeff10 years ago | 269 | } |
0a68f8dbArtem Egorov8 years ago | 270 | |
| 271 | private static getCommandStatusString(command: string, status: CommandStatus) { | |
| 272 | switch (status) { | |
| 273 | case CommandStatus.Start: | |
fc602bb6Yuri Skorokhodov7 years ago | 274 | return `Executing command: ${command}`; |
0a68f8dbArtem Egorov8 years ago | 275 | case CommandStatus.End: |
fc602bb6Yuri Skorokhodov7 years ago | 276 | return `Finished executing: ${command}`; |
0a68f8dbArtem Egorov8 years ago | 277 | default: |
fc602bb6Yuri Skorokhodov7 years ago | 278 | throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus); |
0a68f8dbArtem Egorov8 years ago | 279 | } |
| 280 | } | |
3fb37ad5unknown10 years ago | 281 | } |