microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
240lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as path from "path"; |
| 5 | import * as cp from "child_process"; |
| 6 | import { ILogger } from "../extension/log/LogHelper"; |
| 7 | import { NullLogger } from "../extension/log/NullLogger"; |
| 8 | import { ProjectVersionHelper } from "../common/projectVersionHelper"; |
| 9 | import { ISpawnResult } from "./node/childProcess"; |
| 10 | import { HostPlatform, HostPlatformId } from "./hostPlatform"; |
| 11 | import { ErrorHelper } from "./error/errorHelper"; |
| 12 | import { InternalErrorCode } from "./error/internalErrorCode"; |
| 13 | import * as nls from "vscode-nls"; |
| 14 | import { Node } from "./node/node"; |
| 15 | nls.config({ |
| 16 | messageFormat: nls.MessageFormat.bundle, |
| 17 | bundleFormat: nls.BundleFormat.standalone, |
| 18 | })(); |
| 19 | const localize = nls.loadMessageBundle(); |
| 20 | |
| 21 | export enum CommandVerbosity { |
| 22 | OUTPUT, |
| 23 | SILENT, |
| 24 | PROGRESS, |
| 25 | } |
| 26 | |
| 27 | interface EnvironmentOptions { |
| 28 | REACT_DEBUGGER?: string; |
| 29 | } |
| 30 | |
| 31 | interface Options { |
| 32 | env?: EnvironmentOptions; |
| 33 | verbosity?: CommandVerbosity; |
| 34 | cwd?: string; |
| 35 | } |
| 36 | |
| 37 | export enum CommandStatus { |
| 38 | Start = 0, |
| 39 | End = 1, |
| 40 | } |
| 41 | |
| 42 | export class CommandExecutor { |
| 43 | public static ReactNativeCommand: string | null; |
| 44 | private childProcess = new Node.ChildProcess(); |
| 45 | |
| 46 | constructor( |
| 47 | private nodeModulesRoot: string, |
| 48 | private currentWorkingDirectory: string = process.cwd(), |
| 49 | private logger: ILogger = new NullLogger(), |
| 50 | ) {} |
| 51 | |
| 52 | public async execute(command: string, options: Options = {}): Promise<void> { |
| 53 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start)); |
| 54 | try { |
| 55 | const stdout = await this.childProcess.execToString(command, { |
| 56 | cwd: this.currentWorkingDirectory, |
| 57 | env: options.env, |
| 58 | }); |
| 59 | this.logger.info(stdout); |
| 60 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End)); |
| 61 | } catch (reason) { |
| 62 | return this.generateRejectionForCommand(command, reason); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Spawns a child process with the params passed |
| 68 | * This method waits until the spawned process finishes execution |
| 69 | * {command} - The command to be invoked in the child process |
| 70 | * {args} - Arguments to be passed to the command |
| 71 | * {options} - additional options with which the child process needs to be spawned |
| 72 | */ |
| 73 | public spawn(command: string, args: string[], options: Options = {}): Promise<any> { |
| 74 | return this.spawnChildProcess(command, args, options).outcome; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Spawns the React Native packager in a child process. |
| 79 | */ |
| 80 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 81 | return this.spawnReactCommand("start", args, options); |
| 82 | } |
| 83 | |
| 84 | public async getReactNativeVersion(): Promise<string> { |
| 85 | const versions = await ProjectVersionHelper.getReactNativeVersions( |
| 86 | this.currentWorkingDirectory, |
| 87 | ); |
| 88 | return versions.reactNativeVersion; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Kills the React Native packager in a child process. |
| 93 | */ |
| 94 | public async killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> { |
| 95 | if (packagerProcess) { |
| 96 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { |
| 97 | const res = await this.childProcess.exec( |
| 98 | "taskkill /pid " + packagerProcess.pid + " /T /F", |
| 99 | ); |
| 100 | await res.outcome; |
| 101 | } else { |
| 102 | packagerProcess.kill(); |
| 103 | } |
| 104 | this.logger.info(localize("PackagerStopped", "Packager stopped")); |
| 105 | } else { |
| 106 | this.logger.warning(localize("PackagerNotFound", "Packager not found")); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Executes a react native command and waits for its completion. |
| 112 | */ |
| 113 | public spawnReactCommand( |
| 114 | command: string, |
| 115 | args: string[] = [], |
| 116 | options: Options = {}, |
| 117 | ): ISpawnResult { |
| 118 | const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI()); |
| 119 | return this.spawnChildProcess(reactCommand, [command, ...args], options); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Spawns a child process with the params passed |
| 124 | * This method has logic to do while the command is executing |
| 125 | * {command} - The command to be invoked in the child process |
| 126 | * {args} - Arguments to be passed to the command |
| 127 | * {options} - additional options with which the child process needs to be spawned |
| 128 | */ |
| 129 | public async spawnWithProgress( |
| 130 | command: string, |
| 131 | args: string[], |
| 132 | options: Options = { verbosity: CommandVerbosity.OUTPUT }, |
| 133 | ): Promise<void> { |
| 134 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 135 | const commandWithArgs = command + " " + args.join(" "); |
| 136 | const timeBetweenDots = 1500; |
| 137 | let lastDotTime = 0; |
| 138 | |
| 139 | const printDot = () => { |
| 140 | const now = Date.now(); |
| 141 | if (now - lastDotTime > timeBetweenDots) { |
| 142 | lastDotTime = now; |
| 143 | this.logger.logStream(".", process.stdout); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | if (options.verbosity === CommandVerbosity.OUTPUT) { |
| 148 | this.logger.debug( |
| 149 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | const result = this.childProcess.spawn(command, args, spawnOptions); |
| 154 | |
| 155 | result.stdout.on("data", (data: Buffer) => { |
| 156 | if (options.verbosity === CommandVerbosity.OUTPUT) { |
| 157 | this.logger.logStream(data, process.stdout); |
| 158 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
| 159 | printDot(); |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | result.stderr.on("data", (data: Buffer) => { |
| 164 | if (options.verbosity === CommandVerbosity.OUTPUT) { |
| 165 | this.logger.logStream(data, process.stderr); |
| 166 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
| 167 | printDot(); |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | try { |
| 172 | await result.outcome; |
| 173 | if (options.verbosity === CommandVerbosity.OUTPUT) { |
| 174 | this.logger.debug( |
| 175 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End), |
| 176 | ); |
| 177 | } |
| 178 | this.logger.logStream("\n", process.stdout); |
| 179 | } catch (reason) { |
| 180 | return this.generateRejectionForCommand(commandWithArgs, reason); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public selectReactNativeCLI(): string { |
| 185 | return ( |
| 186 | CommandExecutor.ReactNativeCommand || |
| 187 | path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native") |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | private spawnChildProcess( |
| 192 | command: string, |
| 193 | args: string[], |
| 194 | options: Options = {}, |
| 195 | ): ISpawnResult { |
| 196 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 197 | const commandWithArgs = command + " " + args.join(" "); |
| 198 | |
| 199 | this.logger.debug( |
| 200 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), |
| 201 | ); |
| 202 | const result = this.childProcess.spawn(command, args, spawnOptions); |
| 203 | |
| 204 | result.stderr.on("data", (data: Buffer) => { |
| 205 | this.logger.logStream(data, process.stderr); |
| 206 | }); |
| 207 | |
| 208 | result.stdout.on("data", (data: Buffer) => { |
| 209 | this.logger.logStream(data, process.stdout); |
| 210 | }); |
| 211 | |
| 212 | result.outcome = result.outcome.then( |
| 213 | () => |
| 214 | this.logger.debug( |
| 215 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End), |
| 216 | ), |
| 217 | reason => this.generateRejectionForCommand(commandWithArgs, reason), |
| 218 | ); |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | private generateRejectionForCommand(command: string, reason: any): Promise<void> { |
| 223 | return Promise.reject<void>( |
| 224 | reason.errorCode === InternalErrorCode.CommandFailed |
| 225 | ? reason |
| 226 | : ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command), |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | private static getCommandStatusString(command: string, status: CommandStatus) { |
| 231 | switch (status) { |
| 232 | case CommandStatus.Start: |
| 233 | return `Executing command: ${command}`; |
| 234 | case CommandStatus.End: |
| 235 | return `Finished executing: ${command}`; |
| 236 | default: |
| 237 | throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |