This commit is contained in:
Benjamin Toby
2025-07-21 13:51:59 +01:00
parent 5de4d1dc73
commit 822778d43b
39 changed files with 944 additions and 421 deletions
+27 -77
View File
@@ -13,13 +13,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFiles;
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const delay_1 = __importDefault(require("../../utils/delay"));
const sync_1 = __importDefault(require("../../utils/sync"));
function watchFiles(_a) {
return __awaiter(this, arguments, void 0, function* ({ files, options, }) {
let timeout;
const UPDATE_TIMEOUT = 2000;
const UPDATE_TIMEOUT = 1000;
try {
for (let i = 0; i < files.length; i++) {
const file = files[i];
@@ -45,16 +45,6 @@ function watchFiles(_a) {
fs_1.default.mkdirSync(fileDirPath, { recursive: true });
}
fs_1.default.writeFileSync(filePath, "");
if (typeof existingFilePath == "string") {
sync({ filePath: existingFilePath, files, options });
}
else {
sync({
filePath: existingFilePath.path,
files,
options,
});
}
}
catch (error) {
throw new Error(`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
@@ -67,25 +57,45 @@ function watchFiles(_a) {
// TODO Handle SSH
}
else if (typeof file == "string") {
sync({ options, filePath, files });
yield (0, delay_1.default)();
fs_1.default.watchFile(filePath, {
interval: interval || 200,
}, (curr, prev) => {
if (fs_1.default.existsSync(global.SYNCING_FILE))
if (global.SYNCING)
return;
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval)
? options.interval
: UPDATE_TIMEOUT;
clearTimeout(timeout);
timeout = setTimeout(() => {
fs_1.default.writeFileSync(global.SYNCING_FILE, `SYNCING FILE: curr:${curr} :: prev:${prev}`);
sync({ options, filePath, files });
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
global.SYNCING = true;
(0, sync_1.default)({
options,
dirPath: filePath,
dirs: files,
isFiles: true,
}).finally(() => {
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
});
}, INTERVAL);
});
}
}
const lastUpdatedFile = files[0];
const lastUpdatedFilePath = typeof lastUpdatedFile == "string"
? lastUpdatedFile
: lastUpdatedFile.path;
global.SYNCING = true;
yield (0, sync_1.default)({
dirPath: lastUpdatedFilePath,
dirs: files,
options,
isFiles: true,
firstRun: true,
});
setTimeout(() => {
global.SYNCING = false;
}, UPDATE_TIMEOUT);
}
catch (error) {
console.log("ERROR:", error.message);
@@ -93,63 +103,3 @@ function watchFiles(_a) {
}
});
}
function sync({ options, filePath, files }) {
var _a;
const destFiles = files.filter((fl) => {
if (typeof fl == "string")
return fl !== filePath;
if (fl === null || fl === void 0 ? void 0 : fl.path)
return fl.path !== filePath;
return false;
});
for (let j = 0; j < destFiles.length; j++) {
let cmdArray = ["rsync", "-avh"];
if (options === null || options === void 0 ? void 0 : options.delete) {
cmdArray.push("--delete");
}
if ((_a = options === null || options === void 0 ? void 0 : options.exclude) === null || _a === void 0 ? void 0 : _a[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude '${excl}'`);
});
}
const dstFl = destFiles[j];
if (typeof dstFl == "string") {
if (!fs_1.default.existsSync(dstFl))
continue;
if (filePath === dstFl) {
console.log(`You can't sync the same paths. Please check your configuration and resolve duplicate paths`);
process.exit(6);
}
cmdArray.push(filePath, dstFl);
const cmd = cmdArray.join(" ");
console.log(`Running cmd 1 => ${cmd}`);
(0, child_process_1.execSync)(cmd, {
stdio: "inherit",
});
}
else if (dstFl.path) {
if (!dstFl.host && !fs_1.default.existsSync(dstFl.path))
continue;
if (filePath === dstFl.path) {
console.log(`You can't sync the same paths. Please check your configuration and resolve duplicate paths`);
process.exit(6);
}
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(" ");
(0, child_process_1.execSync)(cmd, {
stdio: "inherit",
});
}
else {
cmdArray.push(filePath, dstFl.path);
const cmd = cmdArray.join(" ");
console.log(`Running cmd 2 => ${cmd}`);
(0, child_process_1.execSync)(cmd, {
stdio: "inherit",
});
}
}
}
}