microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
255lines · 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( |
4dfb1c4cetatanova5 years ago | 47 | private nodeModulesRoot: string, |
0a68f8dbArtem Egorov8 years ago | 48 | private currentWorkingDirectory: string = process.cwd(), |
34472878RedMickey5 years ago | 49 | private logger: ILogger = new NullLogger(), |
| 50 | ) {} | |
3fb37ad5unknown10 years ago | 51 | |
ce5e88eeYuri Skorokhodov5 years ago | 52 | public execute(command: string, options: Options = {}): Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 53 | this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start)); |
34472878RedMickey5 years ago | 54 | return this.childProcess |
| 55 | .execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) | |
| 56 | .then( | |
| 57 | stdout => { | |
| 58 | this.logger.info(stdout); | |
| 59 | this.logger.debug( | |
| 60 | CommandExecutor.getCommandStatusString(command, CommandStatus.End), | |
| 61 | ); | |
| 62 | }, | |
| 63 | (reason: Error) => this.generateRejectionForCommand(command, reason), | |
| 64 | ); | |
3fb37ad5unknown10 years ago | 65 | } |
| 66 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 67 | /** |
| 68 | * Spawns a child process with the params passed | |
| 69 | * This method waits until the spawned process finishes execution | |
| 70 | * {command} - The command to be invoked in the child process | |
| 71 | * {args} - Arguments to be passed to the command | |
| 72 | * {options} - additional options with which the child process needs to be spawned | |
| 73 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 74 | public spawn(command: string, args: string[], options: Options = {}): Promise<any> { |
9596aa53digeff10 years ago | 75 | return this.spawnChildProcess(command, args, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 76 | } |
| 77 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 78 | /** |
| 79 | * Spawns the React Native packager in a child process. | |
| 80 | */ | |
9596aa53digeff10 years ago | 81 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 82 | return this.spawnReactCommand("start", args, options); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 83 | } |
| 84 | | |
ce5e88eeYuri Skorokhodov5 years ago | 85 | public getReactNativeVersion(): Promise<string> { |
34472878RedMickey5 years ago | 86 | return ProjectVersionHelper.getReactNativeVersions(this.currentWorkingDirectory).then( |
| 87 | versions => versions.reactNativeVersion, | |
| 88 | ); | |
b8a56999Patricio Beltran10 years ago | 89 | } |
| 90 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 91 | /** |
| 92 | * Kills the React Native packager in a child process. | |
| 93 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 94 | public killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> { |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 95 | if (packagerProcess) { |
34472878RedMickey5 years ago | 96 | return new Promise(resolve => { |
a1005420dlebu10 years ago | 97 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { |
34472878RedMickey5 years ago | 98 | return resolve( |
| 99 | this.childProcess | |
| 100 | .exec("taskkill /pid " + packagerProcess.pid + " /T /F") | |
| 101 | .then(res => res.outcome), | |
| 102 | ); | |
a1005420dlebu10 years ago | 103 | } else { |
| 104 | packagerProcess.kill(); | |
4dfb1c4cetatanova5 years ago | 105 | return resolve(void 0); |
a1005420dlebu10 years ago | 106 | } |
| 107 | }).then(() => { | |
e2644f37Yuri Skorokhodov7 years ago | 108 | this.logger.info(localize("PackagerStopped", "Packager stopped")); |
a1005420dlebu10 years ago | 109 | }); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 110 | } else { |
e2644f37Yuri Skorokhodov7 years ago | 111 | this.logger.warning(localize("PackagerNotFound", "Packager not found")); |
ce5e88eeYuri Skorokhodov5 years ago | 112 | return Promise.resolve(); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 113 | } |
| 114 | } | |
| 115 | | |
f8d32439dlebu10 years ago | 116 | /** |
| 117 | * Executes a react native command and waits for its completion. | |
| 118 | */ | |
34472878RedMickey5 years ago | 119 | public spawnReactCommand( |
| 120 | command: string, | |
| 121 | args: string[] = [], | |
| 122 | options: Options = {}, | |
| 123 | ): ISpawnResult { | |
af1474acRedMickey6 years ago | 124 | const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI()); |
94cd5149Artem Egorov8 years ago | 125 | return this.spawnChildProcess(reactCommand, [command, ...args], options); |
5b0582f3digeff10 years ago | 126 | } |
| 127 | | |
831f4a85Patricio Beltran9 years ago | 128 | /** |
| 129 | * Spawns a child process with the params passed | |
| 130 | * This method has logic to do while the command is executing | |
| 131 | * {command} - The command to be invoked in the child process | |
| 132 | * {args} - Arguments to be passed to the command | |
| 133 | * {options} - additional options with which the child process needs to be spawned | |
| 134 | */ | |
34472878RedMickey5 years ago | 135 | public spawnWithProgress( |
| 136 | command: string, | |
| 137 | args: string[], | |
| 138 | options: Options = { verbosity: CommandVerbosity.OUTPUT }, | |
| 139 | ): Promise<void> { | |
831f4a85Patricio Beltran9 years ago | 140 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 141 | const commandWithArgs = command + " " + args.join(" "); | |
| 142 | const timeBetweenDots = 1500; | |
b0af599cJimmy Thomson9 years ago | 143 | let lastDotTime = 0; |
831f4a85Patricio Beltran9 years ago | 144 | |
| 145 | const printDot = () => { | |
b0af599cJimmy Thomson9 years ago | 146 | const now = Date.now(); |
| 147 | if (now - lastDotTime > timeBetweenDots) { | |
| 148 | lastDotTime = now; | |
0a68f8dbArtem Egorov8 years ago | 149 | this.logger.logStream(".", process.stdout); |
b0af599cJimmy Thomson9 years ago | 150 | } |
831f4a85Patricio Beltran9 years ago | 151 | }; |
| 152 | | |
| 153 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
34472878RedMickey5 years ago | 154 | this.logger.debug( |
| 155 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), | |
| 156 | ); | |
831f4a85Patricio Beltran9 years ago | 157 | } |
| 158 | | |
| 159 | const result = this.childProcess.spawn(command, args, spawnOptions); | |
| 160 | | |
| 161 | result.stdout.on("data", (data: Buffer) => { | |
| 162 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 163 | this.logger.logStream(data, process.stdout); |
b0af599cJimmy Thomson9 years ago | 164 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 165 | printDot(); |
| 166 | } | |
| 167 | }); | |
| 168 | | |
| 169 | result.stderr.on("data", (data: Buffer) => { | |
| 170 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
0a68f8dbArtem Egorov8 years ago | 171 | this.logger.logStream(data, process.stderr); |
b0af599cJimmy Thomson9 years ago | 172 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 173 | printDot(); |
| 174 | } | |
| 175 | }); | |
| 176 | | |
ce5e88eeYuri Skorokhodov5 years ago | 177 | return new Promise((resolve, reject) => { |
| 178 | result.outcome = result.outcome.then( | |
| 179 | () => { | |
| 180 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
34472878RedMickey5 years ago | 181 | this.logger.debug( |
| 182 | CommandExecutor.getCommandStatusString( | |
| 183 | commandWithArgs, | |
| 184 | CommandStatus.End, | |
| 185 | ), | |
| 186 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 187 | } |
| 188 | this.logger.logStream("\n", process.stdout); | |
| 189 | resolve(); | |
| 190 | }, | |
| 191 | reason => { | |
| 192 | reject(reason); | |
| 193 | return this.generateRejectionForCommand(commandWithArgs, reason); | |
34472878RedMickey5 years ago | 194 | }, |
| 195 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 196 | }); |
831f4a85Patricio Beltran9 years ago | 197 | } |
| 198 | | |
af1474acRedMickey6 years ago | 199 | public selectReactNativeCLI(): string { |
34472878RedMickey5 years ago | 200 | return ( |
| 201 | CommandExecutor.ReactNativeCommand || | |
4dfb1c4cetatanova5 years ago | 202 | path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native") |
34472878RedMickey5 years ago | 203 | ); |
af1474acRedMickey6 years ago | 204 | } |
| 205 | | |
34472878RedMickey5 years ago | 206 | private spawnChildProcess( |
| 207 | command: string, | |
| 208 | args: string[], | |
| 209 | options: Options = {}, | |
| 210 | ): ISpawnResult { | |
9596aa53digeff10 years ago | 211 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 212 | const commandWithArgs = command + " " + args.join(" "); | |
3fb37ad5unknown10 years ago | 213 | |
34472878RedMickey5 years ago | 214 | this.logger.debug( |
| 215 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start), | |
| 216 | ); | |
9596aa53digeff10 years ago | 217 | const result = this.childProcess.spawn(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 218 | |
| 219 | result.stderr.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 220 | this.logger.logStream(data, process.stderr); |
3fb37ad5unknown10 years ago | 221 | }); |
| 222 | | |
| 223 | result.stdout.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 224 | this.logger.logStream(data, process.stdout); |
3fb37ad5unknown10 years ago | 225 | }); |
| 226 | | |
10873e11digeff10 years ago | 227 | result.outcome = result.outcome.then( |
9596aa53digeff10 years ago | 228 | () => |
34472878RedMickey5 years ago | 229 | this.logger.debug( |
| 230 | CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End), | |
| 231 | ), | |
| 232 | reason => this.generateRejectionForCommand(commandWithArgs, reason), | |
| 233 | ); | |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 234 | return result; |
3fb37ad5unknown10 years ago | 235 | } |
10873e11digeff10 years ago | 236 | |
ce5e88eeYuri Skorokhodov5 years ago | 237 | private generateRejectionForCommand(command: string, reason: any): Promise<void> { |
34472878RedMickey5 years ago | 238 | return Promise.reject<void>( |
60ad4ec0JiglioNero5 years ago | 239 | reason.errorCode === InternalErrorCode.CommandFailed |
| 240 | ? reason | |
| 241 | : ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command), | |
34472878RedMickey5 years ago | 242 | ); |
10873e11digeff10 years ago | 243 | } |
0a68f8dbArtem Egorov8 years ago | 244 | |
| 245 | private static getCommandStatusString(command: string, status: CommandStatus) { | |
| 246 | switch (status) { | |
| 247 | case CommandStatus.Start: | |
fc602bb6Yuri Skorokhodov7 years ago | 248 | return `Executing command: ${command}`; |
0a68f8dbArtem Egorov8 years ago | 249 | case CommandStatus.End: |
fc602bb6Yuri Skorokhodov7 years ago | 250 | return `Finished executing: ${command}`; |
0a68f8dbArtem Egorov8 years ago | 251 | default: |
fc602bb6Yuri Skorokhodov7 years ago | 252 | throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus); |
0a68f8dbArtem Egorov8 years ago | 253 | } |
| 254 | } | |
3fb37ad5unknown10 years ago | 255 | } |