| 355 | * If there are no progress states, stops the renderer.spinner. |
| 356 | */ |
| 357 | const writeProgress = ({ namespace = null, isComposeMessage } = {}) => { |
| 358 | /** |
| 359 | * It's common for progress states to be created, |
| 360 | * but have no messages. So, we only want to count progress states |
| 361 | * that have messages to determine if we should stop the spinner, |
| 362 | * or what it should display. |
| 363 | */ |
| 364 | const progressStates = Array.from(renderer.state.progressTasks.values()) |
| 365 | const progressStatesWithMessages = progressStates.filter( |
| 366 | (progressState) => progressState.message, |
| 367 | ) |
| 368 | |
| 369 | // If no progress states with messages exist, stop/destroy the renderer.spinner |
| 370 | if (!isComposeMessage && progressStatesWithMessages.length === 0) { |
| 371 | renderer.spinner.stop() |
| 372 | renderer.spinner._spinner = null |
| 373 | return |
| 374 | } |
| 375 | |
| 376 | // If progress states with messages exist, handle display |
| 377 | let content |
| 378 | if (progressStatesWithMessages.length > 1) { |
| 379 | // If there are multiple progress states, update the renderer.spinner text |
| 380 | const additionalTaskCount = progressStatesWithMessages.length - 1 |
| 381 | content = `${ |
| 382 | progressStatesWithMessages[0].message |
| 383 | } (and ${additionalTaskCount} more ${ |
| 384 | additionalTaskCount < 2 ? 'task' : 'tasks' |
| 385 | })` |
| 386 | } else if (progressStatesWithMessages.length === 1) { |
| 387 | // If there is only one progress state, update the renderer.spinner text with its value |
| 388 | content = progressStatesWithMessages[0].message |
| 389 | } |
| 390 | |
| 391 | // If renderer is set to debug mode and there are progress states, log the progress states |
| 392 | if (renderer.logLevel === 'debug') { |
| 393 | // Use the existing prefix if set, otherwise default to the namespacePrefix |
| 394 | writeStdErr({ |
| 395 | level: 'debug', |
| 396 | type: 'debug', |
| 397 | messageTokens: [content], |
| 398 | prefix: `${namespace || 'unknown'}:`, |
| 399 | }) |
| 400 | return |
| 401 | } |
| 402 | |
| 403 | if (!renderer.spinner.isInitialized()) { |
| 404 | renderer.spinner.start({ content, isComposeMessage }) |
| 405 | } else { |
| 406 | renderer.spinner.update({ content, isComposeMessage }) |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Handles writing text to the console. |