Skip to content

Commit

Permalink
Move Legendary config folder migration to migration system
Browse files Browse the repository at this point in the history
  • Loading branch information
CommandMC committed Jan 10, 2025
1 parent cf85b2f commit a5e8778
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/backend/components/migration/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TypeCheckedStoreBackend } from '../../electron_store'
import { logError, logInfo } from '../../logger/logger'

import { LegendaryGlobalConfigFolderMigration } from './migrations/legendary'

import type { TypeCheckedStore } from 'common/types/electron_store'

export interface Migration {
Expand Down Expand Up @@ -61,8 +63,7 @@ export default class MigrationComponent {
}

private getAllMigrations(): Migration[] {
// TODO: Actually add migrations
return []
return [new LegendaryGlobalConfigFolderMigration()]
}

private readonly migrationsStore: TypeCheckedStore<'migrationsStore'>
Expand Down
35 changes: 35 additions & 0 deletions src/backend/components/migration/migrations/legendary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { app } from 'electron'
import { type PathLike } from 'fs'
import { access, cp, mkdir } from 'fs/promises'
import { join } from 'path'

import { isLinux, legendaryConfigPath, userHome } from 'backend/constants'

import type { Migration } from '..'

const exists = async (path: PathLike) =>
access(path).then(
() => true,
() => false
)

export class LegendaryGlobalConfigFolderMigration implements Migration {
identifier = 'legendary-move-global-config-folder'
async run(): Promise<boolean> {
const hasHeroicSpecificConfig = await exists(legendaryConfigPath)
// Don't overwrite existing configuration
if (hasHeroicSpecificConfig) return true

const globalLegendaryConfig = isLinux
? join(app.getPath('appData'), 'legendary')
: join(userHome, '.config', 'legendary')

const hasGlobalConfig = await exists(globalLegendaryConfig)
// Nothing to migrate
if (!hasGlobalConfig) return true

await mkdir(legendaryConfigPath, { recursive: true })
await cp(globalLegendaryConfig, legendaryConfigPath, { recursive: true })
return true
}
}
13 changes: 0 additions & 13 deletions src/backend/storeManagers/legendary/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import {
legendaryConfigPath,
legendaryLogFile,
legendaryMetadata,
isLinux,
userHome,
isWindows
} from '../../constants'
import {
Expand All @@ -50,8 +48,6 @@ import { callRunner } from '../../launcher'
import { dirname, join } from 'path'
import { isOnline } from 'backend/online_monitor'
import { update } from './games'
import { app } from 'electron'
import { copySync } from 'fs-extra'
import { LegendaryCommand } from './commands'
import { LegendaryAppName, LegendaryPlatform } from './commands/base'
import { Path } from 'backend/schemas'
Expand All @@ -65,15 +61,6 @@ let installedGames: Map<string, InstalledJsonMetadata> = new Map()
const library: Map<string, GameInfo> = new Map()

export async function initLegendaryLibraryManager() {
// Migrate user data from global Legendary config if necessary
const globalLegendaryConfig = isLinux
? join(app.getPath('appData'), 'legendary')
: join(userHome, '.config', 'legendary')
if (!existsSync(legendaryConfigPath) && existsSync(globalLegendaryConfig)) {
mkdirSync(legendaryConfigPath, { recursive: true })
copySync(globalLegendaryConfig, legendaryConfigPath)
}

loadGamesInAccount()
refreshInstalled()
}
Expand Down

0 comments on commit a5e8778

Please sign in to comment.