From 9963f674330866525c8a2e3446f115070a27ce92 Mon Sep 17 00:00:00 2001 From: xmk23333 Date: Tue, 6 Jan 2026 02:39:35 +0800 Subject: [PATCH] refactor: extract createBackupZip to reduce duplication in backup.ts --- src/main/resolve/backup.ts | 88 ++++++++++++++------------------------ 1 file changed, 33 insertions(+), 55 deletions(-) diff --git a/src/main/resolve/backup.ts b/src/main/resolve/backup.ts index 8066801..9710b94 100644 --- a/src/main/resolve/backup.ts +++ b/src/main/resolve/backup.ts @@ -55,37 +55,42 @@ async function getWebDAVClient(): Promise { return { client, webdavDir, webdavMaxBackups } } -export async function webdavBackup(): Promise { - const { client, webdavDir, webdavMaxBackups } = await getWebDAVClient() +function createBackupZip(): AdmZip { const zip = new AdmZip() - if (existsSync(appConfigPath())) { - zip.addLocalFile(appConfigPath()) + const files = [ + appConfigPath(), + controledMihomoConfigPath(), + profileConfigPath(), + overrideConfigPath() + ] + + const folders = [ + { path: themesDir(), name: 'themes' }, + { path: profilesDir(), name: 'profiles' }, + { path: overrideDir(), name: 'override' }, + { path: rulesDir(), name: 'rules' }, + { path: subStoreDir(), name: 'substore' } + ] + + for (const file of files) { + if (existsSync(file)) { + zip.addLocalFile(file) + } } - if (existsSync(controledMihomoConfigPath())) { - zip.addLocalFile(controledMihomoConfigPath()) - } - if (existsSync(profileConfigPath())) { - zip.addLocalFile(profileConfigPath()) - } - if (existsSync(overrideConfigPath())) { - zip.addLocalFile(overrideConfigPath()) - } - if (existsSync(themesDir())) { - zip.addLocalFolder(themesDir(), 'themes') - } - if (existsSync(profilesDir())) { - zip.addLocalFolder(profilesDir(), 'profiles') - } - if (existsSync(overrideDir())) { - zip.addLocalFolder(overrideDir(), 'override') - } - if (existsSync(rulesDir())) { - zip.addLocalFolder(rulesDir(), 'rules') - } - if (existsSync(subStoreDir())) { - zip.addLocalFolder(subStoreDir(), 'substore') + + for (const { path, name } of folders) { + if (existsSync(path)) { + zip.addLocalFolder(path, name) + } } + + return zip +} + +export async function webdavBackup(): Promise { + const { client, webdavDir, webdavMaxBackups } = await getWebDAVClient() + const zip = createBackupZip() const date = new Date() const zipFileName = `${process.platform}_${dayjs(date).format('YYYY-MM-DD_HH-mm-ss')}.zip` @@ -214,34 +219,7 @@ export async function reinitScheduler(): Promise { * 导出本地备份 */ export async function exportLocalBackup(): Promise { - const zip = new AdmZip() - if (existsSync(appConfigPath())) { - zip.addLocalFile(appConfigPath()) - } - if (existsSync(controledMihomoConfigPath())) { - zip.addLocalFile(controledMihomoConfigPath()) - } - if (existsSync(profileConfigPath())) { - zip.addLocalFile(profileConfigPath()) - } - if (existsSync(overrideConfigPath())) { - zip.addLocalFile(overrideConfigPath()) - } - if (existsSync(themesDir())) { - zip.addLocalFolder(themesDir(), 'themes') - } - if (existsSync(profilesDir())) { - zip.addLocalFolder(profilesDir(), 'profiles') - } - if (existsSync(overrideDir())) { - zip.addLocalFolder(overrideDir(), 'override') - } - if (existsSync(subStoreDir())) { - zip.addLocalFolder(subStoreDir(), 'substore') - } - if (existsSync(rulesDir())) { - zip.addLocalFolder(rulesDir(), 'rules') - } + const zip = createBackupZip() const date = new Date() const zipFileName = `clash-party-backup-${dayjs(date).format('YYYY-MM-DD_HH-mm-ss')}.zip`