From 82c82bed94bb0fbf623eaa86b86ed6af8f900453 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Thu, 3 Nov 2022 16:35:41 +0800 Subject: [PATCH] fix: print stats when error(fix #12) --- src/app.ts | 59 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/src/app.ts b/src/app.ts index e14fdd1..494a0ec 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,6 +43,16 @@ type Progress = { total: number; }; +function printStats(stats: Record) { + console.log( + `Exported ${ + Object.entries(stats) + .map(([name, count]) => `${name}: ${count}`) + .join(", ") + }`, + ); +} + export class App { state: State = DEFAULT_STATE; stateBackend: StateBackend; @@ -118,8 +128,29 @@ export class App { return out; } - exportOnce() { - return retryRecoverableError(() => this._exportOnce(), this.recoveryToken); + async exportOnce() { + const exporters = await this.getExporters(); + const stats: Record = Object.fromEntries( + exporters.map((e) => [e.name, 0]), + ); + + await retryRecoverableError(() => + this._exportOnce({ + stats, + }), { + ...this.recoveryToken, + recovery: () => { + if (Object.values(stats).some((i) => i > 0)) { + printStats(stats); + for (const key of Object.keys(stats)) { + stats[key] = 0; + } + } + return this.recoveryToken.recovery(); + }, + }); + + printStats(stats); } exporterProgress(title: string) { const bar = !this.opts.noProgress @@ -150,12 +181,13 @@ export class App { return { redraw, endBar }; } - async _exportOnce() { + private async _exportOnce( + { stats }: { + stats: Record; + }, + ) { const exporters = await this.getExporters(); const skipMode = this.getSkipMode(); - const stats: Record = Object.fromEntries( - exporters.map((e) => [e.name, 0]), - ); if (skipMode.includes("vs")) { console.log("Skip exporting VS games."); @@ -201,7 +233,9 @@ export class App { endBar(); } - if (skipMode.includes("coop")) { + // TODO: remove this filter when stat.ink support coop export + const coopExporter = exporters.filter((e) => e.name !== "stat.ink"); + if (skipMode.includes("coop") || coopExporter.length === 0) { console.log("Skip exporting Coop games."); } else { console.log("Fetching coop battle list..."); @@ -217,8 +251,7 @@ export class App { }); await Promise.all( - // TODO: remove this filter when stat.ink support coop export - exporters.filter((e) => e.name !== "stat.ink").map((e) => + coopExporter.map((e) => showError( this.exportGameList({ type: "CoopInfo", @@ -239,14 +272,6 @@ export class App { endBar(); } - - console.log( - `Exported ${ - Object.entries(stats) - .map(([name, count]) => `${name}: ${count}`) - .join(", ") - }`, - ); } async monitor() { while (true) {