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 {
const configJSON = fs.existsSync(defaultConfigFilePath)
? fs.readFileSync(defaultConfigFilePath, "utf8")
: confFileComputedPath
? fs.readFileSync(confFileComputedPath, "utf8")
const configFinalPath = fs.existsSync(defaultConfigFilePath)
? defaultConfigFilePath
: confFileComputedPath && fs.existsSync(confFileComputedPath)
? confFileComputedPath
: null;
const configJSON = configFinalPath
? fs.readFileSync(configFinalPath, "utf8")
: null;
if (!configJSON)
@ -87,6 +91,14 @@ try {
}
);
}
// setTimeout(() => {
// if (configFinalPath) {
// fs.watchFile(configFinalPath, { interval: 2000 }, (curr, prev) => {
// process.exit(7);
// });
// }
// }, 1000);
} catch (error) {
console.log(`Process Error =>`, error.message);
process.exit();
@ -97,3 +109,17 @@ setInterval(() => {
`Turbo Sync Running for ${process.uptime().toLocaleString()}s ...`
);
}, 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 delay = require("../../utils/delay");
/** @type {any} */
let timeout;
const UPDATE_TIMEOUT = 2000;
/**
*
* @param {SyncFilesFnParams} param0
@ -72,8 +76,16 @@ async function watchFiles({ files, options }) {
interval: interval || 500,
},
(curr, prev) => {
const INTERVAL = options?.interval
? options.interval
: UPDATE_TIMEOUT;
clearTimeout(timeout);
timeout = setTimeout(() => {
sync({ options, filePath, files });
process.exit(1);
}, INTERVAL);
}
);
}
@ -89,6 +101,13 @@ async function watchFiles({ files, options }) {
* @param {SyncFilesSyncFnParams} param0
*/
function sync({ options, filePath, files }) {
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++) {
let cmdArray = ["rsync", "-avh"];
if (options?.delete) {
@ -101,13 +120,6 @@ function sync({ options, filePath, files }) {
});
}
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;
@ -121,6 +133,8 @@ function sync({ options, filePath, files }) {
cmdArray.push(filePath, dstFl);
const cmd = cmdArray.join(" ");
console.log(`Running cmd 1 => ${cmd}`);
execSync(cmd, {
stdio: "inherit",
});
@ -146,7 +160,11 @@ function sync({ options, filePath, files }) {
});
} else {
cmdArray.push(filePath, dstFl.path);
const cmd = cmdArray.join(" ");
console.log(`Running cmd 2 => ${cmd}`);
execSync(cmd, {
stdio: "inherit",
});

View File

@ -7,7 +7,7 @@ const delay = require("../../utils/delay");
/** @type {any} */
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`);
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
@ -91,7 +93,7 @@ async function watchFolders({ folders, options }) {
timeout = setTimeout(() => {
sync({ dirPath, dirs, options });
process.exit(1);
}, UPDATE_TIMEOUT);
}, INTERVAL);
});
}
}
@ -106,6 +108,13 @@ async function watchFolders({ folders, options }) {
* @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++) {
let cmdArray = ["rsync", "-avh"];
if (options?.delete) {
@ -118,13 +127,6 @@ 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++) {
const dstDr = dstDirs[j];
if (typeof dstDr == "string") {
if (!fs.existsSync(dstDr)) continue;

View File

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

View File

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