turbo-sync/lib/sync.js

217 lines
7.9 KiB
JavaScript
Raw Normal View History

2024-10-16 04:44:48 +00:00
#! /usr/bin/env node
// @ts-check
const fs = require("fs");
const path = require("path");
const { execSync, spawn } = require("child_process");
const confFileProvidedJSON = process.argv[process.argv.length - 1];
try {
/** @type {TurboSyncConfigObject} */
const configFileObject = JSON.parse(confFileProvidedJSON);
console.log(`Running '${configFileObject.title}' ...`);
if (
Array.isArray(configFileObject.files) &&
Array.isArray(configFileObject.folders)
) {
throw new Error("Choose wither `files` or `folders`. Not both");
}
const files = configFileObject?.files;
const firstFile = files?.[0];
const folders = configFileObject?.folders;
const firstFolder = folders?.[0];
const options = configFileObject.options;
if (firstFile && files) {
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath =
typeof file == "string" ? file : file?.path ? file.path : null;
const interval = typeof file == "object" ? file.interval : null;
if (!filePath) continue;
if (typeof file == "string" && !fs.existsSync(filePath))
throw new Error("File Doesn't exist");
if (typeof file == "string" && !fs.statSync(filePath).isFile()) {
throw new Error(`'${filePath}' is not a File!`);
}
if (typeof file == "object" && file.host) {
// TODO Handle SSH
} else if (typeof file == "string") {
fs.watchFile(
filePath,
{
interval: interval || 500,
},
(curr, prev) => {
let cmdArray = ["rsync", "-avh"];
if (options?.delete) {
cmdArray.push("--delete");
}
if (options?.exclude?.[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude '${excl}'`);
});
}
const destFiles = files.filter((fl) => {
if (typeof fl == "string") return fl !== filePath;
if (fl?.path) return fl.path !== filePath;
return false;
});
for (let j = 0; j < destFiles.length; j++) {
const dstFl = destFiles[j];
if (typeof dstFl == "string") {
if (!fs.existsSync(dstFl)) continue;
cmdArray.push(filePath, dstFl);
const cmd = cmdArray.join(" ");
execSync(cmd, {
stdio: "inherit",
});
} else if (dstFl.path) {
if (!dstFl.host && !fs.existsSync(dstFl.path))
continue;
if (dstFl.host && dstFl.ssh_key && dstFl.user) {
cmdArray.push(
"-e",
`'ssh -i ${dstFl.ssh_key}'`
);
cmdArray.push(
filePath,
`${dstFl.user}@${dstFl.host}:${dstFl.path}`
);
const cmd = cmdArray.join(" ");
execSync(cmd, {
stdio: "inherit",
});
} else {
cmdArray.push(filePath, dstFl.path);
const cmd = cmdArray.join(" ");
execSync(cmd, {
stdio: "inherit",
});
}
}
}
process.exit(1);
}
);
}
}
} else if (firstFolder && folders?.[0]) {
const dirs = folders;
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
if (!dir) continue;
const dirPath = typeof dir == "string" ? dir : dir.path;
if (typeof dir == "string") {
}
if (typeof dir == "string" && !fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, {
recursive: true,
});
}
if (typeof dir == "string") {
fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
let cmdArray = ["rsync", "-avh"];
if (options?.delete) {
cmdArray.push("--delete");
}
if (options?.exclude?.[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude '${excl}'`);
});
}
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++) {
const dstDr = dstDirs[j];
if (typeof dstDr == "string") {
if (!fs.existsSync(dstDr)) continue;
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;
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",
});
}
}
}
process.exit(1);
});
}
}
}
} catch (error) {
console.log(error);
process.exit();
}
process.on("exit", (code) => {
if (code == 1) {
const args = process.argv;
const cmd = args.shift();
if (cmd) {
spawn(cmd, args, {
stdio: "inherit",
});
}
} else {
process.exit(0);
}
});