Skip to content

Commit 0335bbc

Browse files
committed
fsmerge
1 parent 9bc0432 commit 0335bbc

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/diflow.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import { Processor } from './processor';
33
import { Command } from 'commander';
4+
import { filesystemMerge } from './fsmerge';
45

56
const program = new Command();
67

@@ -20,7 +21,7 @@ program
2021
'secret for accessing repo. URLs of git repos should be in shape https://DIFLOW_GIT_SECRET@<url>. You could also use DIFLOW_GIT_SECRET env variable.'
2122
)
2223
.option('--clear', 'clear work repos before running')
23-
.action(options => {
24+
.action(async options => {
2425
// console.log('repo:', options.repo);
2526
// console.log('branch:', options.branch);
2627
// return;
@@ -29,7 +30,18 @@ program
2930
clear: options.clear,
3031
secret: options.secret ?? process.env.DIFLOW_GIT_SECRET,
3132
});
32-
processor.process();
33+
await processor.process();
34+
console.log('Processing complete.');
35+
});
36+
37+
program
38+
.command('fsmerge')
39+
.description('Run simple merge between directories (base+diff=merged)')
40+
.requiredOption('-b, --base <string>', 'Foder with base repository')
41+
.requiredOption('-d, --diff <string>', 'Foder with diff repository')
42+
.requiredOption('-m, --merged <string>', 'Foder with merged repository (output folder)')
43+
.action(async options => {
44+
await filesystemMerge(options.base, options.diff, options.merged);
3345
console.log('Processing complete.');
3446
});
3547

src/fsmerge.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)