refactor: moved queries to their own file

This commit is contained in:
Simeon Radivoev 2026-03-17 12:57:11 +02:00
parent 364bc9d0be
commit cf6fff6fac
Signed by: simeonradivoev
GPG key ID: 7611A451D2A5D37A
83 changed files with 1107 additions and 852 deletions

View file

@ -1,11 +1,12 @@
import * as appSchema from '@schema/app';
import { findExec, findExecByName } from "../games/services/launchGameService";
import { findExecByName } from "../games/services/launchGameService";
import * as emulatorSchema from "@schema/emulators";
import { eq, inArray } from 'drizzle-orm';
import { customEmulators, db, emulatorsDb } from '../app';
import fs from 'node:fs/promises';
import { cores } from '../emulatorjs/emulatorjs';
import { FrontEndEmulator } from '@/shared/constants';
/**
* Get emulators based on local games. Only the ones we probably need.
@ -77,117 +78,40 @@ export async function getRelevantEmulators ()
systems.forEach(s => platformViability.set(s, true));
}
return {
emulator: emulator,
path: execPath,
const em: FrontEndEmulator & { isCritical: boolean; path?: { path: string, type: string; }; } = {
name: emulator,
exists: exists,
logo: platform ? `/api/romm/platform/local/${platform}/cover` : '',
systems: systems.map(s => platformLookup.get(s)).filter(s => !!s).map(e => ({ icon: `/api/romm/image/romm/assets/platforms/${e.es_slug}.svg`, name: e.platform_name ?? 'Unknown', id: e.es_slug ?? '' })),
gameCount: 0,
description: '',
homepage: '',
type: 'emulator',
os: [process.platform as any],
isCritical: false,
path_cover: platform ? `/api/romm/platform/local/${platform}/cover` : null,
systems: systems.map(s => platformLookup.get(s)).filter(s => !!s)
path: execPath,
};
return em;
}));
finalEmulators.push({
emulator: 'emulatorjs',
name: 'emulatorjs',
exists: true,
path: { path: 'localhost', type: 'js' },
path_cover: `/api/romm/image?url=${encodeURIComponent('https://emulatorjs.org/logo/EmulatorJS.png')}`,
isCritical: false,
systems: []
logo: `/api/romm/image?url=${encodeURIComponent('https://emulatorjs.org/logo/EmulatorJS.png')}`,
systems: [],
gameCount: 0,
type: 'emulator',
description: '',
homepage: '',
os: [process.platform as any],
isCritical: false
});
return finalEmulators.map(e =>
{
e.isCritical = !e.systems.filter(s => s?.es_slug).some(s => !!platformViability.get(s?.es_slug!));
e.isCritical = !e.systems.filter(s => s?.id).some(s => !!platformViability.get(s?.id!));
return e;
});
}
/**
* Only emulators we strictly need based on local games. Emulator JS is included as bundled.
* If there is even single emulator for a system don't include emulators for that system.
*/
/*export async function getMissingEmulators ()
{
const localGames = await db.query.games.findMany({
columns: {
platform_id: true,
slug: true
},
with: {
platform: {
columns: {
name: true,
es_slug: true
}
},
}
});
const platformLookup = new Map(localGames.map(g => [g.platform.es_slug, g]));
const platformViability = new Map(localGames.map(g => [g.platform.es_slug, false]));
// all commands based on the local games
const commands = await emulatorsDb.query.commands.findMany({
columns: { command: true },
where: inArray(emulatorSchema.commands.system, Array.from(new Set(localGames.filter(g => g.platform.es_slug).map(s => s.platform.es_slug!)))),
with: { system: { columns: { name: true } } }
});
// get all emulators in said commands
const emulators = commands
.flatMap(command =>
{
const matches = command.command.match(/(?<=%EMULATOR_)[\w-]+(?=%)/);
if (!matches)
{
return undefined;
}
return matches?.map(m => ({ emulator: m, system: command.system?.name }));
}
).filter(c => !!c);
const groupedEmulators = Map.groupBy(emulators, ({ emulator }) => emulator);
const finalEmulators = await Promise.all(Array.from(groupedEmulators.entries()).map(async ([emulator, system_slug]) =>
{
let execPath: { path: string; type: string, } | undefined;
if (customEmulators.has(emulator))
{
execPath = { path: customEmulators.get(emulator), type: 'custom' };
} else
{
execPath = await findExecByName(emulator);
}
let platform: number | null | undefined = null;
if (system_slug.length <= 1)
{
platform = platformLookup.get(system_slug[0].system)?.platform_id;
}
// check if automatic or custom path found existing binary.
// This might not be the actual emulator but I don't care.
const exists = !!execPath && await fs.exists(execPath.path);
const systems = Array.from(new Set(system_slug.map(s => s.system)));
if (exists)
{
systems.forEach(s => platformViability.set(s, true));
}
return {
emulator: emulator,
path: execPath,
exists: exists,
isCritical: false,
path_cover: platform ? `/api/romm/platform/local/${platform}/cover` : null,
systems: systems.map(s => platformLookup.get(s)).filter(s => !!s)
};
}));
return finalEmulators.map(e =>
{
e.isCritical = !e.systems.filter(s => s?.es_slug).some(s => !!platformViability.get(s?.es_slug!));
return e;
});
}*/
}