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) • 2.6 kB
JavaScript
import { ProgressBarSymbol } from "../../console/progressBar.js";
import ArrayUtil from "../../utils/arrayUtil.js";
import Module from "../module.js";
class CandidateValidator extends Module {
options;
constructor(options, progressBar) {
super(progressBar, CandidateValidator.name);
this.options = options;
}
/**
* Validate the {@link WriteCandidate}s.
*/
validate(dat, candidates) {
if (!this.options.shouldWrite()) {
return [];
}
if (candidates.length === 0) {
this.prefixedLogger.trace(`${dat.getName()}: no candidates to validate`);
return [];
}
this.prefixedLogger.trace(`${dat.getName()}: validating candidates`);
this.progressBar.setSymbol(ProgressBarSymbol.CANDIDATE_VALIDATING);
this.progressBar.resetProgress(candidates.length);
const conflictedOutputPaths = this.validateUniqueOutputPaths(dat, candidates);
if (conflictedOutputPaths.length > 0) {
return conflictedOutputPaths;
}
this.prefixedLogger.trace(`${dat.getName()}: done validating candidates`);
return [];
}
validateUniqueOutputPaths(dat, candidates) {
const outputPathsToCandidates = candidates.reduce((map, candidate) => {
for (const romWithFiles of candidate.getRomsWithFiles()) {
const key = romWithFiles.getOutputFile().getFilePath();
if (map.has(key)) {
map.get(key)?.push(candidate);
} else {
map.set(key, [candidate]);
}
}
return map;
}, /* @__PURE__ */ new Map());
return [...outputPathsToCandidates].filter(([outputPath, candidates2]) => {
const uniqueCandidates = candidates2.filter(ArrayUtil.filterUniqueMapped((candidate) => candidate.getGame())).toSorted((a, b) => a.getName().localeCompare(b.getName()));
if (uniqueCandidates.length <= 1) {
return false;
}
const uniqueInputFiles = uniqueCandidates.flatMap((candidate) => candidate.getRomsWithFiles()).map((romWithFiles) => romWithFiles.getInputFile()).filter(ArrayUtil.filterUniqueMapped((inputFile) => inputFile.toString()));
if (uniqueInputFiles.length <= 1) {
return false;
}
let message = `${dat.getName()}: multiple games writing to the same output path: ${outputPath}`;
for (const candidate of uniqueCandidates) {
message += `
${candidate.getName()}`;
}
this.prefixedLogger.error(message);
return true;
}).flatMap(([, candidates2]) => candidates2).reduce(ArrayUtil.reduceUnique(), []);
}
}
export {
CandidateValidator as default
};
//# sourceMappingURL=candidateValidator.js.map