Fix: fixed fs import and started using logger.warn

This commit is contained in:
Moyasee
2025-09-29 20:36:05 +03:00
parent 96d6b90356
commit 7e22344f77
3 changed files with 21 additions and 17 deletions

View File

@@ -2,29 +2,30 @@ import { registerEvent } from "../register-event";
import { HydraApi } from "@main/services";
import { gamesSublevel, gamesShopAssetsSublevel, levelKeys } from "@main/level";
import type { GameShop, Game } from "@types";
import fs from "node:fs";
import { logger } from "@main/services";
const collectAssetPathsToDelete = (game: Game): string[] => {
const assetPathsToDelete: string[] = [];
const assetUrls =
game.shop === "custom"
? [game.iconUrl, game.logoImageUrl, game.libraryHeroImageUrl]
: [
game.customIconUrl,
game.customLogoImageUrl,
game.customHeroImageUrl,
];
: [game.customIconUrl, game.customLogoImageUrl, game.customHeroImageUrl];
for (const url of assetUrls) {
if (url?.startsWith("local:")) {
assetPathsToDelete.push(url.replace("local:", ""));
}
}
return assetPathsToDelete;
};
const updateGameAsDeleted = async (game: Game, gameKey: string): Promise<void> => {
const updateGameAsDeleted = async (
game: Game,
gameKey: string
): Promise<void> => {
const updatedGame = {
...game,
isDeleted: true,
@@ -50,17 +51,18 @@ const resetShopAssets = async (gameKey: string): Promise<void> => {
}
};
const deleteAssetFiles = async (assetPathsToDelete: string[]): Promise<void> => {
const deleteAssetFiles = async (
assetPathsToDelete: string[]
): Promise<void> => {
if (assetPathsToDelete.length === 0) return;
const fs = await import("node:fs");
for (const assetPath of assetPathsToDelete) {
try {
if (fs.existsSync(assetPath)) {
await fs.promises.unlink(assetPath);
}
} catch (error) {
console.warn(`Failed to delete asset ${assetPath}:`, error);
logger.warn(`Failed to delete asset ${assetPath}:`, error);
}
}
};
@@ -76,7 +78,7 @@ const removeGameFromLibrary = async (
if (!game) return;
const assetPathsToDelete = collectAssetPathsToDelete(game);
await updateGameAsDeleted(game, gameKey);
if (game.shop !== "custom") {

View File

@@ -1,6 +1,8 @@
import { registerEvent } from "../register-event";
import { gamesSublevel, gamesShopAssetsSublevel, levelKeys } from "@main/level";
import type { GameShop } from "@types";
import fs from "node:fs";
import { logger } from "@main/services";
const updateCustomGame = async (
_event: Electron.IpcMainInvokeEvent,
@@ -58,14 +60,13 @@ const updateCustomGame = async (
}
if (oldAssetPaths.length > 0) {
const fs = await import("node:fs");
for (const assetPath of oldAssetPaths) {
try {
if (fs.existsSync(assetPath)) {
await fs.promises.unlink(assetPath);
}
} catch (error) {
console.warn(`Failed to delete old asset ${assetPath}:`, error);
logger.warn(`Failed to delete old asset ${assetPath}:`, error);
}
}
}

View File

@@ -1,6 +1,8 @@
import { registerEvent } from "../register-event";
import { gamesSublevel, gamesShopAssetsSublevel, levelKeys } from "@main/level";
import type { GameShop, Game } from "@types";
import fs from "node:fs";
import { logger } from "@main/services";
const collectOldAssetPaths = (
existingGame: Game,
@@ -64,14 +66,13 @@ const updateShopAssets = async (gameKey: string, title: string): Promise<void> =
const deleteOldAssetFiles = async (oldAssetPaths: string[]): Promise<void> => {
if (oldAssetPaths.length === 0) return;
const fs = await import("node:fs");
for (const assetPath of oldAssetPaths) {
try {
if (fs.existsSync(assetPath)) {
await fs.promises.unlink(assetPath);
}
} catch (error) {
console.warn(`Failed to delete old custom asset ${assetPath}:`, error);
logger.warn(`Failed to delete old custom asset ${assetPath}:`, error);
}
}
};