MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / migrateDirectory

Function migrateDirectory

apps/desktop/main/paths.ts:427–489  ·  view source on GitHub ↗

* 从指定源目录迁移数据到目标目录 * 采用合并策略:只复制不存在的文件,不覆盖已存在的文件

(
  srcDir: string,
  destDir: string,
  subDirs: string[]
)

Source from the content-addressed store, hash-verified

425 * 采用合并策略:只复制不存在的文件,不覆盖已存在的文件
426 */
427function migrateDirectory(
428 srcDir: string,
429 destDir: string,
430 subDirs: string[]
431): { migratedDirs: string[]; skippedDirs: string[] } {
432 const migratedDirs: string[] = []
433 const skippedDirs: string[] = []
434
435 for (const subDir of subDirs) {
436 const srcSubPath = path.join(srcDir, subDir)
437 const destSubPath = path.join(destDir, subDir)
438
439 // 如果源子目录不存在或为空,跳过
440 if (!fs.existsSync(srcSubPath)) {
441 continue
442 }
443
444 const srcFiles = fs.readdirSync(srcSubPath).filter((f) => !f.startsWith('.'))
445 if (srcFiles.length === 0) {
446 continue
447 }
448
449 // 确保目标子目录存在
450 ensureDir(destSubPath)
451
452 // 获取目标目录中已存在的文件
453 const existingFiles = new Set(fs.readdirSync(destSubPath))
454
455 // 合并策略:只复制目标目录中不存在的文件
456 let copiedCount = 0
457 let skippedCount = 0
458
459 for (const file of srcFiles) {
460 const srcPath = path.join(srcSubPath, file)
461 const destPath = path.join(destSubPath, file)
462
463 // 如果目标文件已存在,跳过(不覆盖)
464 if (existingFiles.has(file)) {
465 console.log(`[Paths] Skipping ${subDir}/${file}: already exists in destination`)
466 skippedCount++
467 continue
468 }
469
470 const stat = fs.statSync(srcPath)
471 if (stat.isDirectory()) {
472 copyDirRecursive(srcPath, destPath, ensureDir)
473 } else {
474 fs.copyFileSync(srcPath, destPath)
475 }
476 copiedCount++
477 }
478
479 if (copiedCount > 0) {
480 migratedDirs.push(subDir)
481 console.log(`[Paths] Migrated ${subDir}: ${copiedCount} items copied, ${skippedCount} skipped`)
482 } else if (skippedCount > 0) {
483 skippedDirs.push(subDir)
484 console.log(`[Paths] ${subDir}: all ${skippedCount} items already exist in destination`)

Callers 1

migrateFromLegacyDirFunction · 0.85

Calls 2

copyDirRecursiveFunction · 0.90
ensureDirFunction · 0.70

Tested by

no test coverage detected