UNPKG

ipull

Version:

The only file downloader you'll ever need. For node.js and the browser, CLI and library for fast and reliable file downloads.

92 lines 3.28 kB
import UpdateManager from "stdout-update"; import switchCliProgressStyle from "./progress-bars/switch-cli-progress-style.js"; import { BaseMultiProgressBar } from "./multiProgressBars/BaseMultiProgressBar.js"; import { abortableDebounce } from "../utils/abortableDebounce.js"; export const DEFAULT_TRANSFER_CLI_OPTIONS = { maxViewDownloads: 10, truncateName: true, debounceWait: 20, maxDebounceWait: process.platform === "win32" ? 500 : 100, createProgressBar: switchCliProgressStyle("auto", { truncateName: true }), loadingAnimation: "dots", createMultiProgressBar: BaseMultiProgressBar }; export default class TransferCli { options; stdoutManager = UpdateManager.getInstance(); latestProgress = null; _cliStopped = true; _updateStatuesDebounce = this._updateStatues; _abortDebounce = new AbortController(); _multiProgressBar; isFirstPrint = true; _lastProgressLong = ""; constructor(options) { this.options = { ...DEFAULT_TRANSFER_CLI_OPTIONS, ...options }; this._multiProgressBar = new this.options.createProgressBar.multiProgressBar(this.options); this._updateStatues = this._updateStatues.bind(this); this._processExit = this._processExit.bind(this); this._resetDebounce(); } _resetDebounce() { const maxDebounceWait = this._multiProgressBar.updateIntervalMs || this.options.maxDebounceWait; this._abortDebounce = new AbortController(); this._updateStatuesDebounce = abortableDebounce(this._updateStatues.bind(this), { wait: maxDebounceWait, signal: this._abortDebounce.signal }); } start() { if (!this._cliStopped) return; this._cliStopped = false; if (this._multiProgressBar.printType === "update") { this.stdoutManager.hook(); } process.on("SIGINT", this._processExit); } stop() { if (this._cliStopped) return; this._cliStopped = true; this._updateStatues(); if (this._multiProgressBar.printType === "update") { this.stdoutManager.unhook(false); } process.off("SIGINT", this._processExit); this._abortDebounce.abort(); this._resetDebounce(); } _processExit() { this.stop(); process.exit(0); } updateStatues(statues, oneStatus, loadingDownloads = 0) { this.latestProgress = [statues, oneStatus, loadingDownloads]; if (this.isFirstPrint) { this.isFirstPrint = false; this._updateStatues(); } else { this._updateStatuesDebounce(); } } _updateStatues() { if (!this.latestProgress) return; const printLog = this._multiProgressBar.createMultiProgressBar(...this.latestProgress); if (printLog && this._lastProgressLong != printLog) { this._lastProgressLong = printLog; this._logUpdate(printLog); } } _logUpdate(text) { if (this._multiProgressBar.printType === "update") { this.stdoutManager.update(text.split("\n")); } else { console.log(text); } } } //# sourceMappingURL=transfer-cli.js.map