|
| 1 | +const fs = require('fs-extra'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +function skipPath(src: string) { |
| 5 | + if (src.includes('/node_modules/')) return true; |
| 6 | + if (src.includes('\\node_modules\\')) return true; |
| 7 | + |
| 8 | + if (src.includes('/.git/')) return true; |
| 9 | + if (src.includes('\\.git\\')) return true; |
| 10 | + |
| 11 | + return false; |
| 12 | +} |
| 13 | + |
| 14 | +async function copyDir(src: string, dest: string) { |
| 15 | + // Check if the source directory exists |
| 16 | + if (!await fs.exists(src)) { |
| 17 | + console.error(`Source directory "${src}" does not exist.`); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + // Create the destination directory if it does not exist |
| 22 | + if (!await fs.exists(dest)) { |
| 23 | + await fs.mkdir(dest, { recursive: true }); |
| 24 | + } |
| 25 | + |
| 26 | + // Read the contents of the source directory |
| 27 | + const entries = fs.readdirSync(src, { withFileTypes: true }); |
| 28 | + |
| 29 | + for (let entry of entries) { |
| 30 | + const srcPath = path.join(src, entry.name); |
| 31 | + const destPath = path.join(dest, entry.name); |
| 32 | + |
| 33 | + if (skipPath(srcPath)) continue; |
| 34 | + if (skipPath(destPath)) continue; |
| 35 | + |
| 36 | + if (entry.isDirectory()) { |
| 37 | + await copyDir(srcPath, destPath); |
| 38 | + } else { |
| 39 | + await fs.copy(destPath, srcPath); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export async function filesystemMerge(base: string, diff: string, merged: string) { |
| 45 | + await fs.ensureDir(merged); |
| 46 | + console.log('Copying:', base, '=>', merged); |
| 47 | + await copyDir(base, merged); |
| 48 | + console.log('Copying:', diff, '=>', merged); |
| 49 | + await copyDir(diff, merged); |
| 50 | + console.log('Directories merged successfully'); |
| 51 | +} |
0 commit comments