microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
252lines · 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"; |
34472878RedMickey5 years ago | 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"; | |
e2644f37Yuri Skorokhodov7 years ago | 13 | import * as nls from "vscode-nls"; |
ce5e88eeYuri Skorokhodov5 years ago | 14 | import { Node } from "./node/node"; |
34472878RedMickey5 years ago | 15 | nls.config({ |
| 16 | messageFormat: nls.MessageFormat.bundle, | |
| 17 | bundleFormat: nls.BundleFormat.standalone, | |
| 18 | })(); | |
e2644f37Yuri Skorokhodov7 years ago | 19 | const localize = nls.loadMessageBundle(); |
3fb37ad5unknown10 years ago | 20 | |
831f4a85Patricio Beltran9 years ago | 21 | export enum CommandVerbosity { |
| 22 | OUTPUT, | |
| 23 | SILENT, | |
| 24 | PROGRESS, | |
| 25 | } | |
| 26 | | |
3fb37ad5unknown10 years ago | 27 | interface EnvironmentOptions { |
| 28 | REACT_DEBUGGER?: string; | |
| 29 | } | |
| 30 | | |
| 31 | interface Options { | |
| 32 | env?: EnvironmentOptions; | |
831f4a85Patricio Beltran9 years ago | 33 | verbosity?: CommandVerbosity; |
0d827d9bJimmy Thomson9 years ago | 34 | cwd?: string; |
3fb37ad5unknown10 years ago | 35 | } |
| 36 | | |
17161993Meena Kunnathur Balakrishnan10 years ago | 37 | export enum CommandStatus { |
| 38 | Start = 0, | |
27710197Vladimir Kotikov8 years ago | 39 | End = 1, |
17161993Meena Kunnathur Balakrishnan10 years ago | 40 | } |
| 41 | | |
3fb37ad5unknown10 years ago | 42 | export class CommandExecutor { |
af1474acRedMickey6 years ago | 43 | public static ReactNativeCommand: string | null; |
9596aa53digeff10 years ago | 44 | private childProcess = new Node.ChildProcess(); |
3fb37ad5unknown10 years ago | 45 | |
0a68f8dbArtem Egorov8 years ago | 46 | constructor( |
| 47 | private currentWorkingDirectory: string = process.cwd(), | |
34472878RedMickey5 years ago | 48 | private logger: ILogger = new NullLogger(), |
| 49 | ) {} | |
3fb37ad5unknown10 years ago | 50 | |
ce5e88eeYuri Skorokhodov5 years ago | 51 | public execute(command: string, options: Options = {}): Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 52 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start)); |
34472878RedMickey5 years ago | 53 | return this.childProcess |
| 54 | .execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) | |
| 55 | .then( | |
| 56 | stdout => { | |
| 57 | this.logger.info(stdout); | |
| 58 | this.logger.debug( | |
| 59 | CommandExecutor.getCommandStatusString(command, CommandStatus.End), | |
| 60 | ); | |
| 61 | }, | |
| 62 | (reason: Error) => this.generateRejectionForCommand(command, reason), | |
| 63 | ); | |
3fb37ad5unknown10 years ago | 64 | } |
| 65 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 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 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 73 | public spawn(command: string, args: string[], options: Options = {}): Promise<any> { |
9596aa53digeff10 years ago | 74 | return this.spawnChildProcess(command, args, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 75 | } |
| 76 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 77 | /** |
| 78 | * Spawns the React Native packager in a child process. | |
| 79 | */ | |
9596aa53digeff10 years ago | 80 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 81 | return this.spawnReactCommand("start", args, options); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 82 | } |
| 83 | | |
ce5e88eeYuri Skorokhodov5 years ago | 84 | public getReactNativeVersion(): Promise<string> { |
34472878RedMickey5 years ago | 85 | return ProjectVersionHelper.getReactNativeVersions(this.currentWorkingDirectory).then( |
| 86 | versions => versions.reactNativeVersion, | |
| 87 | ); | |
b8a56999Patricio Beltran10 years ago | 88 | } |
| 89 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 90 | /** |
| 91 | * Kills the React Native packager in a child process. | |
| 92 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 93 | public killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> { |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 94 | if (packagerProcess) { |
34472878RedMickey5 years ago | 95 | return new Promise(resolve => { |
a1005420dlebu10 years ago | 96 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { |
34472878RedMickey5 years ago | 97 | return resolve( |
| 98 | this.childProcess | |
| 99 | .exec("taskkill /pid " + packagerProcess.pid + " /T /F") | |
| 100 | .then(res => res.outcome), | |
| 101 | ); | |
a1005420dlebu10 years ago | 102 | } else { |
| 103 | packagerProcess.kill(); | |
ce5e88eeYuri Skorokhodov5 years ago | 104 | return resolve(); |
a1005420dlebu10 years ago | 105 | } |
| 106 | }).then(() => { | |
e2644f37Yuri Skorokhodov7 years ago | 107 | this.logger.info(localize("PackagerStopped", "Packager stopped")); |
a1005420dlebu10 years ago | 108 | }); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 109 | } else { |
e2644f37Yuri Skorokhodov7 years ago | 110 | this.logger.warning(localize("PackagerNotFound", "Packager not found")); |
ce5e88eeYuri Skorokhodov5 years ago | 111 | return Promise.resolve(); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 112 | } |
| 113 | } | |
| 114 | | |
f8d32439dlebu10 years ago | 115 | /** |
| 116 | * Executes a react native command and waits for its completion. | |
| 117 | */ | |
34472878RedMickey5 years ago | 118 | public spawnReactCommand( |
| 119 | command: string, | |
| 120 | args: string[] = [], | |
| 121 | options: Options = {}, | |
| 122 | ): ISpawnResult { | |
af1474acRedMickey6 years ago | 123 | const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI()); |
94cd5149Artem Egorov8 years ago | 124 | return this.spawnChildProcess(reactCommand, [command, ...args], options); |
5b0582f3digeff10 years ago | 125 | } |
| 126 | | |
831f4a85Patricio Beltran9 years ago | 127 | /** |
| 128 | * Spawns a child process with the params passed | |
| 129 | * This method has logic to do while the command is executing | |
| 130 | * {command} - The command to be invoked in the child process | |
| 131 | * {args} - Arguments to be passed to the command | |
| 132 | * {options} - additional options with which the child process needs to be spawned | |
| 133 | */ | |
34472878RedMickey5 years ago | 134 | public spawnWithProgress( |
| 135 | command: string, | |
| 136 | args: string[], | |
| 137 | options: Options = { verbosity: CommandVerbosity.OUTPUT }, | |
| 138 | ): Promise<void> { | |
831f4a85Patricio Beltran9 years ago | 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) { | |
34472878RedMickey5 years ago | 153 | this.logger.debug( |
| 154 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), | |
| 155 | ); | |
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 | | |
ce5e88eeYuri Skorokhodov5 years ago | 176 | return new Promise((resolve, reject) => { |
| 177 | result.outcome = result.outcome.then( | |
| 178 | () => { | |
| 179 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
34472878RedMickey5 years ago | 180 | this.logger.debug( |
| 181 | CommandExecutor.getCommandStatusString( | |
| 182 | commandWithArgs, | |
| 183 | CommandStatus.End, | |
| 184 | ), | |
| 185 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 186 | } |
| 187 | this.logger.logStream("\n", process.stdout); | |
| 188 | resolve(); | |
| 189 | }, | |
| 190 | reason => { | |
| 191 | reject(reason); | |
| 192 | return this.generateRejectionForCommand(commandWithArgs, reason); | |
34472878RedMickey5 years ago | 193 | }, |
| 194 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 195 | }); |
831f4a85Patricio Beltran9 years ago | 196 | } |
| 197 | | |
af1474acRedMickey6 years ago | 198 | public selectReactNativeCLI(): string { |
34472878RedMickey5 years ago | 199 | return ( |
| 200 | CommandExecutor.ReactNativeCommand || | |
| 201 | path.resolve(this.currentWorkingDirectory, "node_modules", ".bin", "react-native") | |
| 202 | ); | |
af1474acRedMickey6 years ago | 203 | } |
| 204 | | |
34472878RedMickey5 years ago | 205 | private spawnChildProcess( |
| 206 | command: string, | |
| 207 | args: string[], | |
| 208 | options: Options = {}, | |
| 209 | ): ISpawnResult { | |
9596aa53digeff10 years ago | 210 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 211 | const commandWithArgs = command + " " + args.join(" "); | |
3fb37ad5unknown10 years ago | 212 | |
34472878RedMickey5 years ago | 213 | this.logger.debug( |
| 214 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), | |
| 215 | ); | |
9596aa53digeff10 years ago | 216 | const result = this.childProcess.spawn(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 217 | |
| 218 | result.stderr.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 219 | this.logger.logStream(data, process.stderr); |
3fb37ad5unknown10 years ago | 220 | }); |
| 221 | | |
| 222 | result.stdout.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 223 | this.logger.logStream(data, process.stdout); |
3fb37ad5unknown10 years ago | 224 | }); |
| 225 | | |
10873e11digeff10 years ago | 226 | result.outcome = result.outcome.then( |
9596aa53digeff10 years ago | 227 | () => |
34472878RedMickey5 years ago | 228 | this.logger.debug( |
| 229 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End), | |
| 230 | ), | |
| 231 | reason => this.generateRejectionForCommand(commandWithArgs, reason), | |
| 232 | ); | |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 233 | return result; |
3fb37ad5unknown10 years ago | 234 | } |
10873e11digeff10 years ago | 235 | |
ce5e88eeYuri Skorokhodov5 years ago | 236 | private generateRejectionForCommand(command: string, reason: any): Promise<void> { |
34472878RedMickey5 years ago | 237 | return Promise.reject<void>( |
| 238 | ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command), | |
| 239 | ); | |
10873e11digeff10 years ago | 240 | } |
0a68f8dbArtem Egorov8 years ago | 241 | |
| 242 | private static getCommandStatusString(command: string, status: CommandStatus) { | |
| 243 | switch (status) { | |
| 244 | case CommandStatus.Start: | |
fc602bb6Yuri Skorokhodov7 years ago | 245 | return `Executing command: ${command}`; |
0a68f8dbArtem Egorov8 years ago | 246 | case CommandStatus.End: |
fc602bb6Yuri Skorokhodov7 years ago | 247 | return `Finished executing: ${command}`; |
0a68f8dbArtem Egorov8 years ago | 248 | default: |
fc602bb6Yuri Skorokhodov7 years ago | 249 | throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus); |
0a68f8dbArtem Egorov8 years ago | 250 | } |
| 251 | } | |
3fb37ad5unknown10 years ago | 252 | } |