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