feat(library manager): automatically create library folder if it doesn't exist

This commit is contained in:
DecDuck
2024-11-05 19:03:14 +11:00
parent a8a152e578
commit 39fe9d55fd

View File

@ -20,6 +20,7 @@ class LibraryManager {
constructor() { constructor() {
this.basePath = process.env.LIBRARY ?? "./.data/library"; this.basePath = process.env.LIBRARY ?? "./.data/library";
fs.mkdirSync(this.basePath, { recursive: true });
} }
fetchLibraryPath() { fetchLibraryPath() {
@ -49,13 +50,13 @@ class LibraryManager {
async fetchUnimportedGameVersions( async fetchUnimportedGameVersions(
libraryBasePath: string, libraryBasePath: string,
versions: Array<GameVersion>, versions: Array<GameVersion>
) { ) {
const gameDir = path.join(this.basePath, libraryBasePath); const gameDir = path.join(this.basePath, libraryBasePath);
const versionsDirs = fs.readdirSync(gameDir); const versionsDirs = fs.readdirSync(gameDir);
const importedVersionDirs = versions.map((e) => e.versionName); const importedVersionDirs = versions.map((e) => e.versionName);
const unimportedVersions = versionsDirs.filter( const unimportedVersions = versionsDirs.filter(
(e) => !importedVersionDirs.includes(e), (e) => !importedVersionDirs.includes(e)
); );
return unimportedVersions; return unimportedVersions;
@ -83,10 +84,10 @@ class LibraryManager {
noVersions: e.versions.length == 0, noVersions: e.versions.length == 0,
unimportedVersions: await this.fetchUnimportedGameVersions( unimportedVersions: await this.fetchUnimportedGameVersions(
e.libraryBasePath, e.libraryBasePath,
e.versions, e.versions
), ),
}, },
})), }))
); );
} }
@ -107,13 +108,13 @@ class LibraryManager {
const targetDir = path.join(this.basePath, game.libraryBasePath); const targetDir = path.join(this.basePath, game.libraryBasePath);
if (!fs.existsSync(targetDir)) if (!fs.existsSync(targetDir))
throw new Error( throw new Error(
"Game in database, but no physical directory? Something is very very wrong...", "Game in database, but no physical directory? Something is very very wrong..."
); );
const versions = fs.readdirSync(targetDir); const versions = fs.readdirSync(targetDir);
const currentVersions = game.versions.map((e) => e.versionName); const currentVersions = game.versions.map((e) => e.versionName);
const unimportedVersions = versions.filter( const unimportedVersions = versions.filter(
(e) => !currentVersions.includes(e), (e) => !currentVersions.includes(e)
); );
return unimportedVersions; return unimportedVersions;
} }
@ -127,7 +128,7 @@ class LibraryManager {
const targetDir = path.join( const targetDir = path.join(
this.basePath, this.basePath,
game.libraryBasePath, game.libraryBasePath,
versionName, versionName
); );
if (!fs.existsSync(targetDir)) return undefined; if (!fs.existsSync(targetDir)) return undefined;
@ -175,7 +176,7 @@ class LibraryManager {
const finalChoice = sortedOptions[0]; const finalChoice = sortedOptions[0];
const finalChoiceRelativePath = path.relative( const finalChoiceRelativePath = path.relative(
targetDir, targetDir,
finalChoice.filename, finalChoice.filename
); );
startupGuess = finalChoiceRelativePath; startupGuess = finalChoiceRelativePath;
platformGuess = finalChoice.platform; platformGuess = finalChoice.platform;
@ -200,7 +201,7 @@ class LibraryManager {
gameId: string, gameId: string,
versionName: string, versionName: string,
metadata: { platform: string; setup: string; startup: string }, metadata: { platform: string; setup: string; startup: string },
delta = false, delta = false
) { ) {
const taskId = `import:${gameId}:${versionName}`; const taskId = `import:${gameId}:${versionName}`;
@ -237,7 +238,7 @@ class LibraryManager {
(err, manifest) => { (err, manifest) => {
if (err) return reject(err); if (err) return reject(err);
resolve(manifest); resolve(manifest);
}, }
); );
}); });