From 696d50e951c6fe8fb876d976085a2d154812734e Mon Sep 17 00:00:00 2001 From: Christian Emmer Date: Thu, 22 Sep 2022 18:35:13 -0700 Subject: [PATCH] Refactor: don't filter or write for DATs with no candidates (#71) --- src/console/progressBar.ts | 1 + src/constants.ts | 13 +++++++------ src/modules/argumentsParser.ts | 2 +- src/modules/candidateFilter.ts | 7 ++++++- src/modules/romWriter.ts | 19 +++++++++---------- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/console/progressBar.ts b/src/console/progressBar.ts index c5f4cdf52..f30a4eb0d 100644 --- a/src/console/progressBar.ts +++ b/src/console/progressBar.ts @@ -2,6 +2,7 @@ import chalk from 'chalk'; import LogLevel from './logLevel.js'; +// https://www.toptal.com/designers/htmlarrows/symbols/ export const Symbols: { [key: string]: string } = { WAITING: chalk.grey('⋯'), SEARCHING: chalk.magenta('↻'), diff --git a/src/constants.ts b/src/constants.ts index 593642476..4903b70ea 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -29,12 +29,13 @@ function scanUpPathForFile(filePath: string, fileName: string): string | undefin export default class Constants { static readonly COMMAND_NAME = 'igir'; - static readonly COMMAND_VERSION = JSON.parse( - fs.readFileSync(scanUpPathForFile( - url.fileURLToPath(new URL('.', import.meta.url)), - 'package.json', - ) as string).toString(), - ).version; + static readonly COMMAND_VERSION = process.env.npm_package_version + || JSON.parse( + fs.readFileSync(scanUpPathForFile( + url.fileURLToPath(new URL('.', import.meta.url)), + 'package.json', + ) as string).toString(), + ).version; static readonly GLOBAL_TEMP_DIR = globalTempDir + path.sep; diff --git a/src/modules/argumentsParser.ts b/src/modules/argumentsParser.ts index 0c7e66586..3f035efbb 100644 --- a/src/modules/argumentsParser.ts +++ b/src/modules/argumentsParser.ts @@ -96,7 +96,7 @@ export default class ArgumentsParser { group: groupInputOutputPaths, alias: 'i', // TODO(cemmer): add a warning when input and output directories are the same, but also - // have a "yes" flag + // have a "yes" flag description: 'Path(s) to ROM files or archives, these files will not be modified', demandOption: true, type: 'array', diff --git a/src/modules/candidateFilter.ts b/src/modules/candidateFilter.ts index 67726fe38..bdad6f2c4 100644 --- a/src/modules/candidateFilter.ts +++ b/src/modules/candidateFilter.ts @@ -31,7 +31,12 @@ export default class CandidateFilter { return output; } - // TODO(cemmer): return early if there are no candidates to filter + // Return early if there aren't any candidates + const totalReleaseCandidates = [...parentsToCandidates.values()] + .reduce((sum, rcs) => sum + rcs.length, 0); + if (!totalReleaseCandidates) { + return output; + } await this.progressBar.setSymbol(Symbols.FILTERING); await this.progressBar.reset(parentsToCandidates.size); diff --git a/src/modules/romWriter.ts b/src/modules/romWriter.ts index 2f5768384..9b479c961 100644 --- a/src/modules/romWriter.ts +++ b/src/modules/romWriter.ts @@ -43,7 +43,14 @@ export default class ROMWriter { return output; } - // TODO(cemmer): different symbol if shouldn't write? + // Return early if we shouldn't write (are only reporting) + if (!this.options.shouldWrite()) { + return new Map( + [...parentsToCandidates.entries()] + .map(([parent]) => [parent, []]), + ); + } + await this.progressBar.setSymbol(Symbols.WRITING); await this.progressBar.reset(parentsToCandidates.size); @@ -55,8 +62,6 @@ export default class ROMWriter { [, [parent, releaseCandidates]], callback: AsyncResultCallback<[Parent, File[]], Error>, ) => { - // TODO(cemmer): this tends to lock so one DAT processes at a time, makes DATS with no true - // candidates look like they're queued for real writes await ROMWriter.semaphore.runExclusive(async () => { await this.progressBar.increment(); @@ -80,11 +85,6 @@ export default class ROMWriter { dat: DAT, releaseCandidate: ReleaseCandidate, ): Promise { - if (!this.options.shouldWrite()) { - await this.progressBar.logDebug(`${dat.getName()} | ${releaseCandidate.getName()}: not writing`); - return []; - } - const inputToOutput = await this.buildInputToOutput(dat, releaseCandidate); // Determine if a write is needed based on the output not equaling the input @@ -258,8 +258,7 @@ export default class ROMWriter { /* eslint-disable no-await-in-loop */ for (let i = 0; i < inputToOutputEntries.length; i += 1) { - const inputRomFile = inputToOutputEntries[i][0]; - const outputRomFile = inputToOutputEntries[i][1]; + const [inputRomFile, outputRomFile] = inputToOutputEntries[i]; await this.writeRawSingle(inputRomFile, outputRomFile); writtenRomFiles.push(outputRomFile); }