Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: dir-letter-count option #849

Merged
merged 10 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/modules/argumentsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,35 @@ export default class ArgumentsParser {
})
.option('dir-letter', {
group: groupRomOutput,
description: 'Append the first letter of the ROM name as an output subdirectory',
description: 'Group games in an output subdirectory by the first --dir-letter-count letters in their name',
type: 'boolean',
})
.option('dir-letter-count', {
group: groupRomOutput,
description: 'How many game name letters to use for the subdirectory name',
type: 'number',
coerce: (val: number) => Math.max(ArgumentsParser.getLastValue(val), 1),
requiresArg: true,
default: 1,
})
.check((checkArgv) => {
// Re-implement `implies: 'dir-letter'`, which isn't possible with a default value
if (checkArgv['dir-letter-count'] > 1 && !checkArgv['dir-letter']) {
throw new Error('Missing dependent arguments:\n dir-letter-count -> dir-letter');
}
return true;
})
.option('dir-letter-limit', {
group: groupRomOutput,
description: 'Limit the number ROMs in letter subdirectories, splitting into multiple if necessary',
description: 'Limit the number of games in letter subdirectories, splitting into multiple subdirectories if necessary',
type: 'number',
coerce: (val: number) => Math.max(ArgumentsParser.getLastValue(val), 1),
requiresArg: true,
implies: 'dir-letter',
})
.option('dir-game-subdir', {
group: groupRomOutput,
description: 'Append the name of the game as an output directory depending on its ROMs',
description: 'Append the name of the game as an output subdirectory depending on its ROMs',
choices: Object.keys(GameSubdirMode)
.filter((mode) => Number.isNaN(Number(mode)))
.map((mode) => mode.toLowerCase()),
Expand Down
8 changes: 8 additions & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface OptionsProps {
readonly dirDatName?: boolean,
readonly dirDatDescription?: boolean,
readonly dirLetter?: boolean,
readonly dirLetterCount?: number,
readonly dirLetterLimit?: number,
readonly dirGameSubdir?: string,
readonly overwrite?: boolean,
Expand Down Expand Up @@ -181,6 +182,8 @@ export default class Options implements OptionsProps {

readonly dirLetter: boolean;

readonly dirLetterCount: number;

readonly dirLetterLimit: number;

readonly dirGameSubdir?: string;
Expand Down Expand Up @@ -333,6 +336,7 @@ export default class Options implements OptionsProps {
this.dirDatName = options?.dirDatName ?? false;
this.dirDatDescription = options?.dirDatDescription ?? false;
this.dirLetter = options?.dirLetter ?? false;
this.dirLetterCount = options?.dirLetterCount ?? 0;
this.dirLetterLimit = options?.dirLetterLimit ?? 0;
this.dirGameSubdir = options?.dirGameSubdir;
this.overwrite = options?.overwrite ?? false;
Expand Down Expand Up @@ -756,6 +760,10 @@ export default class Options implements OptionsProps {
return this.dirLetter;
}

getDirLetterCount(): number {
return this.dirLetterCount;
}

getDirLetterLimit(): number {
return this.dirLetterLimit;
}
Expand Down
18 changes: 11 additions & 7 deletions src/types/outputFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,18 @@ export default class OutputFactory {

// Find the letter for every ROM filename
let lettersToFilenames = (romBasenames ?? [romBasename]).reduce((map, filename) => {
let letter = filename.charAt(0).toUpperCase();
if (letter.match(/[^A-Z]/)) {
letter = '#';
}

const existing = map.get(letter) ?? new Set();
const filenameParsed = path.parse(filename);
let letters = (filenameParsed.dir || filenameParsed.name)
.substring(0, options.getDirLetterCount())
.padEnd(options.getDirLetterCount(), 'A')
.toUpperCase()
.replace(/[^A-Z0-9]/g, '#');
// TODO(cemmer): only do this when not --dir-letter-group
letters = letters.replace(/[^A-Z]/g, '#');

const existing = map.get(letters) ?? new Set();
existing.add(filename);
map.set(letter, existing);
map.set(letters, existing);
return map;
}, new Map<string, Set<string>>());

Expand Down
12 changes: 12 additions & 0 deletions test/modules/argumentsParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe('options', () => {
expect(options.getDirDatName()).toEqual(false);
expect(options.getDirDatDescription()).toEqual(false);
expect(options.getDirLetter()).toEqual(false);
expect(options.getDirLetterCount()).toEqual(1);
expect(options.getDirLetterLimit()).toEqual(0);
expect(options.getDirGameSubdir()).toEqual(GameSubdirMode.MULTIPLE);
expect(options.getOverwrite()).toEqual(false);
Expand Down Expand Up @@ -346,6 +347,17 @@ describe('options', () => {
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', 'true', '--dir-letter', 'false']).getDirLetter()).toEqual(false);
});

it('should parse "dir-letter-count"', () => {
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter-count'])).toThrow(/not enough arguments/i);
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter-count', '1'])).not.toThrow();
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter-count', '2'])).toThrow(/dependent|implication/i);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', '--dir-letter-count', '-1']).getDirLetterCount()).toEqual(1);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', '--dir-letter-count', '0']).getDirLetterCount()).toEqual(1);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', '--dir-letter-count', '1']).getDirLetterCount()).toEqual(1);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', '--dir-letter-count', '5']).getDirLetterCount()).toEqual(5);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', '--dir-letter-count', '5', '--dir-letter-count', '10']).getDirLetterCount()).toEqual(10);
});

it('should parse "dir-letter-limit"', () => {
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter-limit'])).toThrow(/not enough arguments/i);
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter-limit', '1'])).toThrow(/dependent|implication/i);
Expand Down
1 change: 1 addition & 0 deletions test/modules/candidatePostProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe('dirLetterLimit', () => {
commands: ['copy'],
output: 'Output',
dirLetter: true,
dirLetterCount: 1,
dirLetterLimit: limit,
dirGameSubdir: GameSubdirMode[GameSubdirMode.MULTIPLE].toLowerCase(),
});
Expand Down
26 changes: 21 additions & 5 deletions test/outputFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,26 @@ describe('should respect "--dir-dat-description"', () => {
describe('should respect "--dir-letter"', () => {
describe('games with one ROM', () => {
test.each([
['', os.devNull],
['file.rom', path.join(os.devNull, 'F', 'file.rom')],
['🙂.rom', path.join(os.devNull, '#', '🙂.rom')],
])('option is true: %s', async (romName, expectedPath) => {
const options = new Options({ commands: ['copy'], output: os.devNull, dirLetter: true });
[0, '', os.devNull],
[1, '', os.devNull],
[2, '', os.devNull],
[999, '', os.devNull],
[1, 'file.rom', path.join(os.devNull, 'F', 'file.rom')],
[3, 'file.rom', path.join(os.devNull, 'FIL', 'file.rom')],
[10, 'file.rom', path.join(os.devNull, 'FILEAAAAAA', 'file.rom')],
[1, '007.rom', path.join(os.devNull, '#', '007.rom')],
[2, '007.rom', path.join(os.devNull, '##', '007.rom')],
[10, '007.rom', path.join(os.devNull, '###AAAAAAA', '007.rom')],
[1, '🙂.rom', path.join(os.devNull, '#', '🙂.rom')],
[3, '🙂.rom', path.join(os.devNull, '##A', '🙂.rom')],
[10, '🙂.rom', path.join(os.devNull, '##AAAAAAAA', '🙂.rom')],
])('option is true: %s', async (dirLetterCount, romName, expectedPath) => {
const options = new Options({
commands: ['copy'],
output: os.devNull,
dirLetter: true,
dirLetterCount,
});
const rom = new ROM({ name: romName, size: 0, crc: '' });

const outputPath = OutputFactory.getPath(
Expand Down Expand Up @@ -939,6 +954,7 @@ describe('should respect "--dir-letter"', () => {
commands: ['copy'],
output: os.devNull,
dirLetter: true,
dirLetterCount: 1,
dirGameSubdir: GameSubdirMode[GameSubdirMode.MULTIPLE].toLowerCase(),
});

Expand Down
Loading