-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (82 loc) · 2.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { File } from "./class/file";
import { Mirror } from "./class/mirror";
import { readdir } from "node:fs/promises";
const { readFile } = require("fs/promises");
const ignore = require("ignore");
import 'dotenv/config';
const defaultIgnore = [];
const readPath = process.env.READPATH;
const writePath = process.env.WRITEPATH;
console.log("Read Path:", readPath);
console.log("Write Path:", writePath);
(async () => {
try {
// First we will read the directory for the files
const files = await readdir(readPath, { recursive: true });
console.log("🚀 ~ files:", files)
// Now we will filter the files to remove the ones that are included within the gitignore ect
const filteredFiles = await filterFiles(files);
console.log("🚀 ~ filteredFiles:", filteredFiles)
// We will create a virtual file from the files found at source
const virtualFiles = await createVirtualFiles(filteredFiles)
console.log("🚀 ~ virtualFiles:", virtualFiles)
// Next we will mirror the virtual files into the output directory
await mirrorFiles(virtualFiles, writePath).forEach((mirror) => {
try {
mirror.initMirror()
} catch (error) {
console.log("🚀 ~ files.forEach : Mirror ~ error:", error)
}
});
} catch (err) {
console.log("🚀 ~ err:", err)
}
})();
function createVirtualFiles(files) {
let virtualFiles = []
files.forEach((file) => {
// console.log("🚀 ~ files.forEach ~ file:", file)
try {
const myFile = new File(readPath + "/" + file);
virtualFiles = [...virtualFiles, myFile]
} catch (error) {
throw error
// console.log("🚀 ~ files.forEach ~ error:", error)
}
});
return virtualFiles
// we wll return an array of virtual files
}
function mirrorFiles(files) {
let mirroredFiles = []
files.forEach((file) => {
// console.log("🚀 ~ files.forEach ~ file:", file)
try {
const myMirror = new Mirror(writePath, file)
mirroredFiles = [...mirroredFiles, myMirror]
} catch (error) {
}
});
return mirroredFiles
}
async function filterFiles(files) {
// Create an ignore manager with optional settings
const ig = ignore({
ignorecase: true, // Case-insensitive matching
});
// Read and parse .gitignore
ig.add(await readFile(".gitignore", "utf8").catch(() => ""));
// Add patterns to the ignore manager
ig.add([
...defaultIgnore,
".obsidian",
".git",
"*.md",
// '!important.log', // Exclude "important.log" from being ignored
]);
// Get all files and filter using .gitignore
const filteredFiles = files.filter((file) => !ig.ignores(file));
// console.log("🚀 ~ filterFiles ~ filteredFiles:", filteredFiles)
// Log files not ignored
return filteredFiles;
}