This commit is contained in:
Benjamin Toby 2024-10-16 12:39:14 +01:00
parent 9518efef8a
commit f2892f1bf4
5 changed files with 80 additions and 33 deletions

View File

@ -55,10 +55,14 @@ if (
} }
try { try {
const configJSON = fs.existsSync(defaultConfigFilePath) const configFinalPath = fs.existsSync(defaultConfigFilePath)
? fs.readFileSync(defaultConfigFilePath, "utf8") ? defaultConfigFilePath
: confFileComputedPath : confFileComputedPath && fs.existsSync(confFileComputedPath)
? fs.readFileSync(confFileComputedPath, "utf8") ? confFileComputedPath
: null;
const configJSON = configFinalPath
? fs.readFileSync(configFinalPath, "utf8")
: null; : null;
if (!configJSON) if (!configJSON)
@ -87,6 +91,14 @@ try {
} }
); );
} }
// setTimeout(() => {
// if (configFinalPath) {
// fs.watchFile(configFinalPath, { interval: 2000 }, (curr, prev) => {
// process.exit(7);
// });
// }
// }, 1000);
} catch (error) { } catch (error) {
console.log(`Process Error =>`, error.message); console.log(`Process Error =>`, error.message);
process.exit(); process.exit();
@ -97,3 +109,17 @@ setInterval(() => {
`Turbo Sync Running for ${process.uptime().toLocaleString()}s ...` `Turbo Sync Running for ${process.uptime().toLocaleString()}s ...`
); );
}, 60000); }, 60000);
// process.on("exit", (code) => {
// if (code === 7) {
// console.log("Config file changed. Restarting Turbo Sync service ...");
// const args = process.argv;
// const cmd = args.shift();
// if (cmd) {
// spawn(cmd, args, {
// stdio: "inherit",
// });
// }
// }
// });

View File

@ -5,6 +5,10 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const delay = require("../../utils/delay"); const delay = require("../../utils/delay");
/** @type {any} */
let timeout;
const UPDATE_TIMEOUT = 2000;
/** /**
* *
* @param {SyncFilesFnParams} param0 * @param {SyncFilesFnParams} param0
@ -72,8 +76,16 @@ async function watchFiles({ files, options }) {
interval: interval || 500, interval: interval || 500,
}, },
(curr, prev) => { (curr, prev) => {
sync({ options, filePath, files }); const INTERVAL = options?.interval
process.exit(1); ? options.interval
: UPDATE_TIMEOUT;
clearTimeout(timeout);
timeout = setTimeout(() => {
sync({ options, filePath, files });
process.exit(1);
}, INTERVAL);
} }
); );
} }
@ -89,18 +101,6 @@ async function watchFiles({ files, options }) {
* @param {SyncFilesSyncFnParams} param0 * @param {SyncFilesSyncFnParams} param0
*/ */
function sync({ options, filePath, files }) { function sync({ options, filePath, files }) {
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) => { const destFiles = files.filter((fl) => {
if (typeof fl == "string") return fl !== filePath; if (typeof fl == "string") return fl !== filePath;
if (fl?.path) return fl.path !== filePath; if (fl?.path) return fl.path !== filePath;
@ -108,6 +108,18 @@ function sync({ options, filePath, files }) {
}); });
for (let j = 0; j < destFiles.length; j++) { for (let j = 0; j < destFiles.length; j++) {
let cmdArray = ["rsync", "-avh"];
if (options?.delete) {
cmdArray.push("--delete");
}
if (options?.exclude?.[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude '${excl}'`);
});
}
const dstFl = destFiles[j]; const dstFl = destFiles[j];
if (typeof dstFl == "string") { if (typeof dstFl == "string") {
if (!fs.existsSync(dstFl)) continue; if (!fs.existsSync(dstFl)) continue;
@ -121,6 +133,8 @@ function sync({ options, filePath, files }) {
cmdArray.push(filePath, dstFl); cmdArray.push(filePath, dstFl);
const cmd = cmdArray.join(" "); const cmd = cmdArray.join(" ");
console.log(`Running cmd 1 => ${cmd}`);
execSync(cmd, { execSync(cmd, {
stdio: "inherit", stdio: "inherit",
}); });
@ -146,7 +160,11 @@ function sync({ options, filePath, files }) {
}); });
} else { } else {
cmdArray.push(filePath, dstFl.path); cmdArray.push(filePath, dstFl.path);
const cmd = cmdArray.join(" "); const cmd = cmdArray.join(" ");
console.log(`Running cmd 2 => ${cmd}`);
execSync(cmd, { execSync(cmd, {
stdio: "inherit", stdio: "inherit",
}); });

View File

@ -7,7 +7,7 @@ const delay = require("../../utils/delay");
/** @type {any} */ /** @type {any} */
let timeout; let timeout;
const UPDATE_TIMEOUT = 1000; const UPDATE_TIMEOUT = 2000;
/** /**
* *
@ -19,6 +19,8 @@ async function watchFolders({ folders, options }) {
console.log(`Now handling ${dirs.length} Directories`); console.log(`Now handling ${dirs.length} Directories`);
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
for (let i = 0; i < dirs.length; i++) { for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i]; const dir = dirs[i];
@ -91,7 +93,7 @@ async function watchFolders({ folders, options }) {
timeout = setTimeout(() => { timeout = setTimeout(() => {
sync({ dirPath, dirs, options }); sync({ dirPath, dirs, options });
process.exit(1); process.exit(1);
}, UPDATE_TIMEOUT); }, INTERVAL);
}); });
} }
} }
@ -106,18 +108,6 @@ async function watchFolders({ folders, options }) {
* @param {SyncFoldersSyncFnParams} param0 * @param {SyncFoldersSyncFnParams} param0
*/ */
function sync({ options, dirs, dirPath, init }) { function sync({ options, dirs, dirPath, init }) {
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) => { const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string") return dr !== dirPath; if (typeof dr == "string") return dr !== dirPath;
if (dr?.path) return dr.path !== dirPath; if (dr?.path) return dr.path !== dirPath;
@ -125,6 +115,18 @@ function sync({ options, dirs, dirPath, init }) {
}); });
for (let j = 0; j < dstDirs.length; j++) { for (let j = 0; j < dstDirs.length; j++) {
let cmdArray = ["rsync", "-avh"];
if (options?.delete) {
cmdArray.push("--delete");
}
if (options?.exclude?.[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude '${excl}'`);
});
}
const dstDr = dstDirs[j]; const dstDr = dstDirs[j];
if (typeof dstDr == "string") { if (typeof dstDr == "string") {
if (!fs.existsSync(dstDr)) continue; if (!fs.existsSync(dstDr)) continue;

View File

@ -1,6 +1,6 @@
{ {
"name": "turbosync", "name": "turbosync",
"version": "1.3.2", "version": "1.3.4",
"module": "index.js", "module": "index.js",
"scripts": { "scripts": {
"start": "node index.ts", "start": "node index.ts",

View File

@ -25,6 +25,7 @@
* @typedef {object} TurboSyncOptions * @typedef {object} TurboSyncOptions
* @property {boolean} [delete] - Should files removed be deleted in all destinations? * @property {boolean} [delete] - Should files removed be deleted in all destinations?
* @property {string[]} [exclude] - Patterns that should be ignored. Eg "*.log" * @property {string[]} [exclude] - Patterns that should be ignored. Eg "*.log"
* @property {number} [interval] - Seconds to delay before update
*/ */
/** /**