MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / migrateFromLegacyDir

Function migrateFromLegacyDir

apps/desktop/main/paths.ts:496–551  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

494 * 只有在所有数据都成功迁移后才删除旧目录
495 */
496export function migrateFromLegacyDir(): { success: boolean; migratedDirs: string[]; error?: string } {
497 const legacyDir = getLegacyDataDir()
498 const newDir = getUserDataDir()
499
500 try {
501 if (!fs.existsSync(legacyDir)) {
502 return { success: true, migratedDirs: [] }
503 }
504
505 // 获取旧目录下的所有子目录和文件
506 const entries = fs.readdirSync(legacyDir, { withFileTypes: true })
507 const dirsToMigrate = entries.filter((e) => e.isDirectory() && !e.name.startsWith('.')).map((e) => e.name)
508 const filesToMigrate = entries.filter((e) => e.isFile() && !e.name.startsWith('.')).map((e) => e.name)
509
510 const result = migrateDirectory(legacyDir, newDir, dirsToMigrate)
511
512 // 迁移根目录下的文件
513 ensureDir(newDir)
514 for (const file of filesToMigrate) {
515 const srcPath = path.join(legacyDir, file)
516 const destPath = path.join(newDir, file)
517 if (!fs.existsSync(destPath)) {
518 fs.copyFileSync(srcPath, destPath)
519 }
520 }
521
522 // 构建迁移摘要
523 const summary: string[] = []
524 summary.push(`Migration from ${legacyDir} to ${newDir}`)
525
526 // 迁移成功,删除旧目录
527 fs.rmSync(legacyDir, { recursive: true, force: true })
528 summary.push('Status: Success, legacy directory removed')
529
530 if (result.migratedDirs.length > 0) {
531 summary.push(`Migrated dirs: ${result.migratedDirs.join(', ')}`)
532 }
533 if (filesToMigrate.length > 0) {
534 summary.push(`Migrated files: ${filesToMigrate.length}`)
535 }
536
537 // 写入迁移日志
538 writeMigrationLog(getLogsDir(), summary.join(' | '), ensureDir)
539
540 return { success: true, migratedDirs: result.migratedDirs }
541 } catch (error) {
542 console.error('[Paths] Migration failed:', error)
543 const errorMsg = error instanceof Error ? error.message : String(error)
544 writeMigrationLog(getLogsDir(), `Migration failed: ${errorMsg}`, ensureDir)
545 return {
546 success: false,
547 migratedDirs: [],
548 error: errorMsg,
549 }
550 }
551}
552
553/**

Callers 1

migrateDataIfNeededMethod · 0.90

Calls 7

writeMigrationLogFunction · 0.90
getLegacyDataDirFunction · 0.85
getUserDataDirFunction · 0.85
migrateDirectoryFunction · 0.85
getLogsDirFunction · 0.85
ensureDirFunction · 0.70
errorMethod · 0.65

Tested by

no test coverage detected