From c555d9a864b351b16239f0332fc112fd68166131 Mon Sep 17 00:00:00 2001 From: Christian Emmer <10749361+emmercm@users.noreply.github.com> Date: Thu, 11 Jul 2024 09:46:06 -0400 Subject: [PATCH] Fix: write fixdats to the output dir root (#1205) --- src/modules/fixdatCreator.ts | 25 +++++++++++++++++++++---- src/types/options.ts | 4 ++-- src/types/outputFactory.ts | 3 +-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/modules/fixdatCreator.ts b/src/modules/fixdatCreator.ts index 9a93cfbc2..0ec411e15 100644 --- a/src/modules/fixdatCreator.ts +++ b/src/modules/fixdatCreator.ts @@ -47,7 +47,7 @@ export default class FixdatCreator extends Module { .flatMap((releaseCandidate) => releaseCandidate.getRomsWithFiles()) .map((romWithFiles) => romWithFiles.getRom()) .map((rom) => rom.hashCode())); - // Find all the games who have at least one missing ROM + // Find all the games that have at least one missing ROM const gamesWithMissingRoms = originalDat.getGames() .filter((game) => !game.getRoms().every((rom) => writtenRomHashCodes.has(rom.hashCode()))); if (gamesWithMissingRoms.length === 0) { @@ -55,9 +55,14 @@ export default class FixdatCreator extends Module { return undefined; } - const fixdatDir = this.options.shouldWrite() - ? OutputFactory.getDir(this.options, originalDat) - : process.cwd(); + let fixdatDir = process.cwd(); + if (this.options.shouldWrite()) { + try { + fixdatDir = this.getDatOutputDirRoot(originalDat); + } catch (error) { + this.progressBar.logWarn(`failed to: ${error}`); + } + } if (!await fsPoly.exists(fixdatDir)) { await fsPoly.mkdir(fixdatDir, { recursive: true }); } @@ -88,4 +93,16 @@ export default class FixdatCreator extends Module { this.progressBar.logTrace(`${originalDat.getNameShort()}: done generating a fixdat`); return fixdatPath; } + + private getDatOutputDirRoot(dat: DAT): string { + return OutputFactory.getDir( + new Options({ + ...this.options, + // Chop off any path slugs that contain replaceable tokens, such that OutputFactory won't + // throw an exception + output: this.options.getOutputDirRoot(), + }), + dat, + ); + } } diff --git a/src/types/options.ts b/src/types/options.ts index 3fa155953..8a9a2ac25 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -853,9 +853,9 @@ export default class Options implements OptionsProps { * Get the "root" sub-path of the output dir, the sub-path up until the first replaceable token. */ getOutputDirRoot(): string { - const outputSplit = path.normalize(this.getOutput()).split(path.sep); + const outputSplit = path.normalize(this.getOutput()).split(/[\\/]/); for (let i = 0; i < outputSplit.length; i += 1) { - if (outputSplit[i].match(/\{[a-zA-Z]+\}/g) !== null) { + if (outputSplit[i].match(/\{[a-zA-Z]+\}/) !== null) { return path.normalize(outputSplit.slice(0, i).join(path.sep)); } } diff --git a/src/types/outputFactory.ts b/src/types/outputFactory.ts index a6ca66b5d..fc4b0d19c 100644 --- a/src/types/outputFactory.ts +++ b/src/types/outputFactory.ts @@ -132,8 +132,7 @@ export default class OutputFactory { if (options.getDirMirror() && inputFile?.getFilePath()) { const mirroredDir = path.dirname(inputFile.getFilePath()) - .replace(/[\\/]/g, path.sep) - .split(path.sep) + .split(/[\\/]/) .splice(1) .join(path.sep); output = path.join(output, mirroredDir);