2024-10-16 07:29:28 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const { execSync } = require("child_process");
|
|
|
|
const delay = require("../../utils/delay");
|
|
|
|
|
2024-10-16 09:02:16 +00:00
|
|
|
/** @type {any} */
|
|
|
|
let timeout;
|
2024-10-16 11:39:14 +00:00
|
|
|
const UPDATE_TIMEOUT = 2000;
|
2024-10-16 09:02:16 +00:00
|
|
|
|
2024-10-16 07:29:28 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {SyncFoldersFnParams} param0
|
|
|
|
*/
|
|
|
|
async function watchFolders({ folders, options }) {
|
|
|
|
try {
|
|
|
|
const dirs = folders;
|
|
|
|
|
|
|
|
console.log(`Now handling ${dirs.length} Directories`);
|
|
|
|
|
2024-10-16 11:39:14 +00:00
|
|
|
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
|
|
|
|
|
2024-10-16 07:29:28 +00:00
|
|
|
for (let i = 0; i < dirs.length; i++) {
|
|
|
|
const dir = dirs[i];
|
|
|
|
|
|
|
|
if (!dir) {
|
|
|
|
console.log(`Dir: ${dir} doesn't exist`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dirPath = typeof dir == "string" ? dir : dir.path;
|
|
|
|
if (
|
|
|
|
(typeof dir == "string" && !fs.existsSync(dirPath)) ||
|
|
|
|
(typeof dir == "object" &&
|
|
|
|
dir.path &&
|
|
|
|
!dir.host &&
|
|
|
|
!fs.existsSync(dir.path))
|
|
|
|
) {
|
|
|
|
console.log(`Dir ${dirPath} does not exist. Creating ...`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const existingDirPath = dirs.find((dr) => {
|
|
|
|
if (typeof dr == "string") return fs.existsSync(dr);
|
|
|
|
if (!dr.host) return fs.existsSync(dr.path); // TODO handle remote
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`Existing Dir to clone: ${existingDirPath}`);
|
|
|
|
|
|
|
|
if (!existingDirPath) {
|
|
|
|
throw new Error(
|
|
|
|
"No existing Directories for reference"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.mkdirSync(dirPath, {
|
|
|
|
recursive: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (typeof existingDirPath == "string") {
|
|
|
|
sync({
|
|
|
|
dirPath: existingDirPath,
|
|
|
|
dirs,
|
|
|
|
options,
|
|
|
|
init: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sync({
|
|
|
|
dirPath: existingDirPath.path,
|
|
|
|
dirs,
|
|
|
|
options,
|
|
|
|
init: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error:", error.message);
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof dir == "string") {
|
|
|
|
sync({ dirPath, dirs, options });
|
|
|
|
|
|
|
|
await delay();
|
|
|
|
|
|
|
|
fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
|
2024-10-16 09:02:16 +00:00
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
sync({ dirPath, dirs, options });
|
|
|
|
process.exit(1);
|
2024-10-16 11:39:14 +00:00
|
|
|
}, INTERVAL);
|
2024-10-16 07:29:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log("ERROR:", error.message);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {SyncFoldersSyncFnParams} param0
|
|
|
|
*/
|
|
|
|
function sync({ options, dirs, dirPath, init }) {
|
|
|
|
const dstDirs = dirs.filter((dr) => {
|
|
|
|
if (typeof dr == "string") return dr !== dirPath;
|
|
|
|
if (dr?.path) return dr.path !== dirPath;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let j = 0; j < dstDirs.length; j++) {
|
2024-10-16 11:39:14 +00:00
|
|
|
let cmdArray = ["rsync", "-avh"];
|
|
|
|
|
|
|
|
if (options?.delete) {
|
|
|
|
cmdArray.push("--delete");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options?.exclude?.[0]) {
|
|
|
|
options.exclude.forEach((excl) => {
|
|
|
|
cmdArray.push(`--exclude '${excl}'`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-16 07:29:28 +00:00
|
|
|
const dstDr = dstDirs[j];
|
|
|
|
if (typeof dstDr == "string") {
|
|
|
|
if (!fs.existsSync(dstDr)) continue;
|
2024-10-16 08:40:21 +00:00
|
|
|
|
|
|
|
if (dirPath === dstDr) {
|
|
|
|
console.log(
|
|
|
|
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
|
|
|
|
);
|
|
|
|
process.exit(6);
|
|
|
|
}
|
|
|
|
|
2024-10-16 07:29:28 +00:00
|
|
|
cmdArray.push(
|
|
|
|
path.normalize(dirPath) + "/",
|
|
|
|
path.normalize(dstDr) + "/"
|
|
|
|
);
|
|
|
|
const cmd = cmdArray.join(" ");
|
|
|
|
execSync(cmd, {
|
|
|
|
stdio: "inherit",
|
|
|
|
});
|
|
|
|
} else if (dstDr.path) {
|
|
|
|
if (!dstDr.host && !fs.existsSync(dstDr.path)) continue;
|
|
|
|
|
2024-10-16 08:40:21 +00:00
|
|
|
if (dirPath === dstDr.path) {
|
|
|
|
console.log(
|
|
|
|
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
|
|
|
|
);
|
|
|
|
process.exit(6);
|
|
|
|
}
|
|
|
|
|
2024-10-16 07:29:28 +00:00
|
|
|
if (dstDr.host && dstDr.ssh_key && dstDr.user) {
|
|
|
|
cmdArray.push("-e", `'ssh -i ${dstDr.ssh_key}'`);
|
|
|
|
cmdArray.push(
|
|
|
|
path.normalize(dirPath) + "/",
|
|
|
|
`${dstDr.user}@${dstDr.host}:${dstDr.path}/`
|
|
|
|
);
|
|
|
|
const cmd = cmdArray.join(" ");
|
|
|
|
execSync(cmd, {
|
|
|
|
stdio: "inherit",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cmdArray.push(
|
|
|
|
path.normalize(dirPath),
|
|
|
|
path.normalize(dstDr.path)
|
|
|
|
);
|
|
|
|
const cmd = cmdArray.join(" ");
|
|
|
|
execSync(cmd, {
|
|
|
|
stdio: "inherit",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = watchFolders;
|