UNPKG

igir

Version:

🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.

66 lines (65 loc) • 3.12 kB
import path from "node:path"; import DATStatus, { GameStatus } from "../models/datStatus.js"; import ArrayUtil from "../utils/arrayUtil.js"; import FsUtil from "../utils/fsUtil.js"; import IntlUtil from "../utils/intlUtil.js"; import Module from "./module.js"; class ReportGenerator extends Module { options; constructor(options, progressBar) { super(progressBar, ReportGenerator.name); this.options = options; } /** * Generate the report. */ async generate(scannedRomFiles, cleanedOutputFiles, datStatuses) { this.prefixedLogger.trace("generating report"); const reportPath = this.options.getReportOutput(); const hasAnyGamesFoundAtAll = datStatuses.some( (datStatus) => datStatus.anyGamesFound(this.options) ); const matchedFileCsvs = (await Promise.all( datStatuses.filter((datStatus) => datStatus.anyGamesFound(this.options) || !hasAnyGamesFoundAtAll).toSorted((a, b) => a.getDATName().localeCompare(b.getDATName())).map(async (datsStatus) => await datsStatus.toCsv(this.options)) )).filter((csv) => csv.length > 0).map((csv, idx) => { if (idx === 0) { return csv; } return csv.split("\n").slice(1).join("\n"); }); const usedFilePaths = new Set( datStatuses.flatMap((datStatus) => datStatus.getInputFiles()).map((file) => file.getFilePath()) ); const usedHashes = new Set( datStatuses.flatMap((datStatus) => datStatus.getInputFiles()).map((file) => file.hashCode()) ); const duplicateFilePaths = scannedRomFiles.filter( (inputFile) => !usedFilePaths.has(inputFile.getFilePath()) && usedHashes.has(inputFile.hashCode()) ).map((inputFile) => inputFile.getFilePath()).reduce(ArrayUtil.reduceUnique(), []).toSorted((a, b) => a.localeCompare(b)); const duplicateCsv = await DATStatus.filesToCsv(duplicateFilePaths, GameStatus.DUPLICATE); const unusedFilePaths = scannedRomFiles.filter( (inputFile) => !usedFilePaths.has(inputFile.getFilePath()) && !usedHashes.has(inputFile.hashCode()) ).map((inputFile) => inputFile.getFilePath()).reduce(ArrayUtil.reduceUnique(), []).toSorted((a, b) => a.localeCompare(b)); const unusedCsv = await DATStatus.filesToCsv(unusedFilePaths, GameStatus.UNUSED); const cleanedCsv = await DATStatus.filesToCsv(cleanedOutputFiles, GameStatus.DELETED); this.prefixedLogger.info(`writing report '${reportPath}'`); const reportPathDir = path.dirname(reportPath); if (!await FsUtil.exists(reportPathDir)) { await FsUtil.mkdir(reportPathDir, { recursive: true }); } const rows = [...matchedFileCsvs, duplicateCsv, unusedCsv, cleanedCsv].filter( (csv) => csv.length > 0 ); await FsUtil.writeFile(reportPath, rows.join("\n")); this.prefixedLogger.trace( `wrote ${IntlUtil.toLocaleString(datStatuses.length)} CSV row${datStatuses.length === 1 ? "" : "s"}: ${reportPath}` ); this.prefixedLogger.trace("done generating report"); this.progressBar.finish(reportPath); this.progressBar.freeze(); } } export { ReportGenerator as default }; //# sourceMappingURL=reportGenerator.js.map