Skip to content

Commit

Permalink
Fix: write fixdats to the output dir root (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Jul 11, 2024
1 parent 17b6860 commit c555d9a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/modules/fixdatCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ 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) {
this.progressBar.logDebug(`${originalDat.getNameShort()}: not creating a fixdat, all games were found`);
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 });
}
Expand Down Expand Up @@ -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,
);
}
}
4 changes: 2 additions & 2 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/types/outputFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c555d9a

Please sign in to comment.