diff --git a/src/common/domain/valueobject/Directory.ts b/src/common/domain/valueobject/Directory.ts index 0509b52..e76c262 100644 --- a/src/common/domain/valueobject/Directory.ts +++ b/src/common/domain/valueobject/Directory.ts @@ -37,6 +37,10 @@ export class Directory implements ValueObject { ): Promise { for await (const dirEntry of Deno.readDir(directory)) { if (dirEntry.isDirectory) { + if (dirEntry.name === "@eaDir") { + continue; + } + await Directory.scanDirectory( `${directory}/${dirEntry.name}`, pattern, diff --git a/src/scan.ts b/src/scan.ts index efa51ec..16287f6 100644 --- a/src/scan.ts +++ b/src/scan.ts @@ -4,6 +4,7 @@ import type { Configuration } from "./configuration/domain/aggregate/Configurati import type { ConfigurationScanWithPattern } from "./configuration/domain/valueobject/ConfigurationScanWithPattern.ts"; import { ImageDirectory } from "./scanner/domain/aggregate/ImageDirectory.ts"; import type { VideoGame } from "./scanner/domain/entity/VideoGame.ts"; +import { VideoGamePlatform } from "./scanner/domain/valueobject/VideoGamePlatform.ts"; import { KvImageRepository } from "./scanner/repository/ImageRepository.ts"; import { KvRelationRepository, @@ -21,6 +22,12 @@ export const runScanner = async ( kvDriver: KvDriver, debugDatabase: boolean, ): Promise => { + const hasError: boolean = await preScan(configuration); + if (hasError) { + console.error("An error occurred while pre-scanning."); + return; + } + const videoGameRepository = new KvVideoGameRepository(kvDriver); const relationRepository = new KvRelationRepository(kvDriver); @@ -108,3 +115,56 @@ export async function scan( throw new Error("An error occurred while scanning."); } } + +async function preScan(configuration: Configuration): Promise { + console.log("Pre-scan..."); + + let filesCount = 0; + let warningCount = 0; + let errorsCount = 0; + + for (const scan of configuration.scans) { + const directory: string = scan.directory.path.value; + console.log(`Pre-scanning ${directory}...`); + + await scanDirectory(directory, scan.pattern.regex, (filepath) => { + const regexResult: RegExpExecArray | null = + scan.pattern.regex.exec(filepath); + + if (regexResult) { + const group3: string = regexResult[3]; + try { + new VideoGamePlatform(group3); + filesCount++; + } catch (_) { + console.error(` - "${filepath}" has an invalid platform: ${group3}`); + errorsCount++; + } + } else { + warningCount++; + console.warn(` - "${filepath}" does not match the pattern.`); + } + }); + } + + console.log("Pre-scan completed!"); + console.log(`Found ${filesCount} files.`); + console.log(`Had ${errorsCount} errors and ${warningCount} warnings.`); + + return errorsCount > 0; +} + +async function scanDirectory( + directory: string, + pattern: RegExp, + onFile: (filepath: string) => void, +): Promise { + for await (const dirEntry of Deno.readDir(directory)) { + if (dirEntry.isDirectory && dirEntry.name !== "@eaDir") { + await scanDirectory(`${directory}/${dirEntry.name}`, pattern, onFile); + } else if (dirEntry.isFile && dirEntry.name !== ".DS_Store") { + const fullPath = `${directory}/${dirEntry.name}`; + onFile(fullPath); + } + } +} diff --git a/src/scanner/domain/valueobject/VideoGamePlatform.ts b/src/scanner/domain/valueobject/VideoGamePlatform.ts index 9fe793c..c58df40 100644 --- a/src/scanner/domain/valueobject/VideoGamePlatform.ts +++ b/src/scanner/domain/valueobject/VideoGamePlatform.ts @@ -3,16 +3,23 @@ import type { ValueObject } from "../../../common/domain/ValueObject.ts"; const platforms = [ "Android", + "Android (preview)", "iOS", "Nintendo Switch", "PC", + "PC (alpha)", + "PC (demo)", + "PC (early access)", "PlayStation 4", + "PlayStation 4 (beta)", "PlayStation 5", "Steam Deck", + "unknown", "Xbox 360", "Xbox Game Cloud", "Xbox One", "Xbox Series X", + "Xbox Series X (beta)", ]; export class VideoGamePlatform implements ValueObject { diff --git a/test/scanner/domain/valueobject/VideoGamePlatform.test.ts b/test/scanner/domain/valueobject/VideoGamePlatform.test.ts index 1998b82..94acdbd 100644 --- a/test/scanner/domain/valueobject/VideoGamePlatform.test.ts +++ b/test/scanner/domain/valueobject/VideoGamePlatform.test.ts @@ -8,7 +8,7 @@ Deno.test(function wrongPlatform() { assert(error instanceof DomainError); assertEquals( error.message, - "Platform (foo) must be one of the following: Android, Nintendo Switch, PC, PlayStation 4, PlayStation 5, Steam Deck, Xbox 360, Xbox Game Cloud, Xbox One, Xbox Series X, iOS", + "Platform (foo) must be one of the following: Android, Android (preview), Nintendo Switch, PC, PC (alpha), PC (demo), PC (early access), PlayStation 4, PlayStation 4 (beta), PlayStation 5, Steam Deck, Xbox 360, Xbox Game Cloud, Xbox One, Xbox Series X, Xbox Series X (beta), iOS, unknown", ); });