private startProgressBar()

in packages/libs/common/src/logging/console-logger-sink.ts [63:147]


  private startProgressBar(initialName?: string): ProgressTracker {
    // If in a non TTY stream do not even start the progress bar.
    if (!this.isTTY() && !this.options.progressNoTTYOutput) {
      return {
        update: (progress: Progress) => {},
        stop: () => {},
      };
    }

    if (this.currentProgressBar === undefined) {
      this.currentProgressBar = new progressBar.MultiBar(
        {
          hideCursor: true,
          stream: this.stream,
          noTTYOutput: this.options.progressNoTTYOutput,
          format: "{name} [{bar}] {percentage}% | {value}/{total}",
          forceRedraw: true, // without this the bar is flickering,
        },
        progressBar.Presets.legacy,
      );
    }
    const multiBar = this.currentProgressBar;

    multiBar.on("redraw-pre", () => {
      if (this.pendingLogs.length > 0) {
        if ("clearLine" in this.stream) {
          (this.stream as WriteStream).clearLine(1);
        }
      }
      while (this.pendingLogs.length > 0) {
        this.writeLine(this.pendingLogs.shift());
      }
    });

    multiBar.on("stop", () => {
      this.currentProgressBar = undefined;
      while (this.pendingLogs.length > 0) {
        this.writeLine(this.pendingLogs.shift());
      }
    });

    let bar: progressBar.SingleBar | typeof ProgressBarUnavailable | undefined;

    const update = (progress: Progress) => {
      if (bar === ProgressBarUnavailable) {
        return;
      }
      const name = progress.name ?? initialName ?? "progress";
      if (bar === undefined) {
        const createdBar = multiBar.create(progress.total, 0, { name });
        // Can return undefined if the stream is not TTY.
        if (createdBar === undefined) {
          bar = ProgressBarUnavailable;
          return;
        } else {
          bar = createdBar;
          this.bars.push(bar);
        }
      } else {
        bar.setTotal(progress.total);
      }

      bar.update(progress.current, { name });
    };

    const stop = () => {
      if (bar && bar !== ProgressBarUnavailable) {
        bar.update(bar.getTotal());
        bar.render();
        bar.stop();
        multiBar.remove(bar);
        this.bars = this.bars.filter((x) => x !== bar);
      }

      if (this.bars.length === 0) {
        multiBar.stop();
        this.currentProgressBar = undefined;
      }
    };

    return {
      update,
      stop,
    };
  }