fix: theme migration overwrite existing theme (#2975)

This commit is contained in:
Louis 2024-05-31 13:36:28 +07:00 committed by GitHub
parent 4edef30e0e
commit 19e35e63c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,15 @@
import { app } from 'electron'
import { join } from 'path'
import { rmdirSync, cpSync, existsSync } from 'fs'
import {
rmdirSync,
readFileSync,
existsSync,
mkdirSync,
readdirSync,
cpSync,
lstatSync,
} from 'fs'
import Store from 'electron-store'
import {
getJanExtensionsPath,
@ -20,8 +28,8 @@ export async function migrate() {
if (store.get('migrated_version') !== app.getVersion()) {
console.debug('start migration:', store.get('migrated_version'))
if (existsSync(getJanExtensionsPath()))
rmdirSync(getJanExtensionsPath(), { recursive: true })
// if (existsSync(getJanExtensionsPath()))
// rmdirSync(getJanExtensionsPath(), { recursive: true })
await migrateThemes()
store.set('migrated_version', app.getVersion())
@ -32,11 +40,48 @@ export async function migrate() {
}
async function migrateThemes() {
if (existsSync(join(getJanDataFolderPath(), 'themes')))
rmdirSync(join(getJanDataFolderPath(), 'themes'), { recursive: true })
cpSync(
join(await appResourcePath(), 'themes'),
join(getJanDataFolderPath(), 'themes'),
{ recursive: true }
)
if (!existsSync(join(getJanDataFolderPath(), 'themes')))
mkdirSync(join(getJanDataFolderPath(), 'themes'), { recursive: true })
const themes = readdirSync(join(await appResourcePath(), 'themes'))
for (const theme of themes) {
const themePath = join(await appResourcePath(), 'themes', theme)
if (existsSync(themePath) && !lstatSync(themePath).isDirectory()) {
continue
}
await checkAndMigrateTheme(theme, themePath)
}
}
async function checkAndMigrateTheme(
sourceThemeName: string,
sourceThemePath: string
) {
const janDataThemesFolder = join(getJanDataFolderPath(), 'themes')
const existingTheme = readdirSync(janDataThemesFolder).find(
(theme) => theme === sourceThemeName
)
if (existingTheme) {
const desTheme = join(janDataThemesFolder, existingTheme)
if (!existsSync(desTheme) || !lstatSync(desTheme).isDirectory()) return
const desThemeData = JSON.parse(
readFileSync(join(desTheme, 'theme.json'), 'utf-8')
)
const sourceThemeData = JSON.parse(
readFileSync(join(sourceThemePath, 'theme.json'), 'utf-8')
)
if (desThemeData.version !== sourceThemeData.version) {
console.debug('Updating theme', existingTheme)
rmdirSync(desTheme, { recursive: true })
cpSync(sourceThemePath, join(janDataThemesFolder, sourceThemeName), {
recursive: true,
})
}
} else {
console.debug('Adding new theme', sourceThemeName)
cpSync(sourceThemePath, join(janDataThemesFolder, sourceThemeName), {
recursive: true,
})
}
}