Skip to content

Commit

Permalink
Support games.pgn without game-x.pgn (#41)
Browse files Browse the repository at this point in the history
* Support games.pgn without game-x.pgn

* fix test
  • Loading branch information
fitztrev authored Nov 15, 2024
1 parent 11be42e commit 24eb7e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/components/FolderWatcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { open } from '@tauri-apps/api/dialog';
import { DebouncedEvent, watch } from 'tauri-plugin-fs-watch-api';
import { useLogStore } from '../stores/logs';
import { useStatusStore } from '../stores/status';
import { add_to_queue, isMultiGamePgn, isSingleGamePgn, lichessFetch, openPath } from '../utils';
import { add_to_queue, isMultiGamePgn, lichessFetch, multiOrSingleFilter, openPath } from '../utils';
import { LichessRound } from '../types';
import { getIndividualGamePgns, getMultiGamePgns } from '../upload';
import { computed } from 'vue';
Expand Down Expand Up @@ -60,18 +60,17 @@ function handleFolderChange(events: DebouncedEvent): void {
status.setRoundHasMultiGamePgn(props.round.round.id);
}
const files = events
.filter(event => event.kind === 'Any')
.filter(event => isSingleGamePgn(event.path))
.map(event => event.path);
const files = events.filter(event => event.kind === 'Any').map(event => event.path);
if (files.length === 0) {
const toUpload = multiOrSingleFilter(files);
if (toUpload.length === 0) {
return;
}
add_to_queue(props.round.round.id, files);
add_to_queue(props.round.round.id, toUpload);
const paths = files.map(file => file.split('/').pop());
const paths = toUpload.map(file => file.split('/').pop());
logs.info(`Modified: ${paths.join(', ')}`);
}
Expand Down
28 changes: 27 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,22 @@ export function isMultiGamePgn(path: string): boolean {
return path.endsWith('games.pgn');
}

/**
* Determine which files to upload.
* If there is a `games.pgn` file, upload only that.
* Otherwise, upload the individual game PGNs.
*/
export function multiOrSingleFilter(files: string[]): string[] {
const multiGamePgn = files.find(isMultiGamePgn);
if (multiGamePgn) {
return [multiGamePgn];
}

return files.filter(isSingleGamePgn);
}

if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
const { it, expect, describe } = import.meta.vitest;
it('finds pgn files', () => {
expect(isSingleGamePgn('path/to/game-1.pgn')).toBe(true);
expect(isMultiGamePgn('path/to/game-1.pgn')).toBe(false);
Expand All @@ -166,6 +180,18 @@ if (import.meta.vitest) {
expect(isSingleGamePgn('path/to/index.json')).toBe(false);
expect(isMultiGamePgn('path/to/index.json')).toBe(false);
});

describe('multiOrSingleFilter', () => {
it('only includes games.pgn', () => {
const files = ['game-1.pgn', 'game-2.pgn', 'games.pgn'];
expect(multiOrSingleFilter(files)).toEqual(['games.pgn']);
});

it('includes all individual games', () => {
const files = ['game-1.pgn', 'game-2.pgn', 'game-3.pgn'];
expect(multiOrSingleFilter(files)).toEqual(['game-1.pgn', 'game-2.pgn', 'game-3.pgn']);
});
});
}

export async function recursiveFileList(folder: string): Promise<string[]> {
Expand Down

0 comments on commit 24eb7e3

Please sign in to comment.