Skip to content

Commit

Permalink
Merge branch 'feature/37-huge-test' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhykos committed Oct 20, 2024
2 parents 0a0bab1 + 2a5d481 commit 3d62a8d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/common/domain/valueobject/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class Directory implements ValueObject {
): Promise<void> {
for await (const dirEntry of Deno.readDir(directory)) {
if (dirEntry.isDirectory) {
if (dirEntry.name === "@eaDir") {
continue;
}

await Directory.scanDirectory(
`${directory}/${dirEntry.name}`,
pattern,
Expand Down
60 changes: 60 additions & 0 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +22,12 @@ export const runScanner = async (
kvDriver: KvDriver,
debugDatabase: boolean,
): Promise<void> => {
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);

Expand Down Expand Up @@ -108,3 +115,56 @@ export async function scan(
throw new Error("An error occurred while scanning.");
}
}

async function preScan(configuration: Configuration): Promise<boolean> {
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<void> {
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);
}
}
}
7 changes: 7 additions & 0 deletions src/scanner/domain/valueobject/VideoGamePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/scanner/domain/valueobject/VideoGamePlatform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});

Expand Down

0 comments on commit 3d62a8d

Please sign in to comment.