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
+59 -11
View File
@@ -1,23 +1,59 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const child_process_1 = require("child_process");
const files_1 = __importDefault(require("./watch/files"));
const folders_1 = __importDefault(require("./watch/folders"));
const path_1 = __importDefault(require("path"));
const get_last_edited_src_1 = __importDefault(require("../utils/get-last-edited-src"));
const grab_folders_files_string_paths_1 = __importStar(require("../utils/grab-folders-files-string-paths"));
const confFileProvidedJSON = process.argv[process.argv.length - 1];
global.SYNC_SUCCESS_EXIT_CODE = 32;
global.CONFIG_DIR = process.cwd();
global.SYNCING_FILE = path_1.default.join(global.CONFIG_DIR, "syncing.txt");
global.SYNCING = false;
const REWATCH_TIMEOUT = 1000;
try {
if (fs_1.default.existsSync(global.SYNCING_FILE)) {
process.exit(0);
}
const configFileObject = JSON.parse(confFileProvidedJSON);
const lastUpdated = (0, get_last_edited_src_1.default)({
dirs: (0, grab_folders_files_string_paths_1.default)(configFileObject.folders),
files: (0, grab_folders_files_string_paths_1.default)(configFileObject.files),
});
console.log(`Running '${configFileObject.title}' ...`);
console.log(`Last Updated Path => '${lastUpdated || "N/A"}' ...`);
if (Array.isArray(configFileObject.files) &&
Array.isArray(configFileObject.folders)) {
throw new Error("Choose wither `files` or `folders`. Not both");
@@ -27,11 +63,24 @@ try {
const folders = configFileObject === null || configFileObject === void 0 ? void 0 : configFileObject.folders;
const firstFolder = folders === null || folders === void 0 ? void 0 : folders[0];
const options = configFileObject.options;
if (firstFile && (files === null || files === void 0 ? void 0 : files[0])) {
(0, files_1.default)({ files, options });
const sortedFoldersByLastUpdated = (folders === null || folders === void 0 ? void 0 : folders[0]) && lastUpdated
? [
lastUpdated,
...((folders === null || folders === void 0 ? void 0 : folders.filter((fl) => (0, grab_folders_files_string_paths_1.fldFileToStr)(fl) !== lastUpdated)) || []),
]
: folders;
const sortedFilesByLastUpdated = (files === null || files === void 0 ? void 0 : files[0]) && lastUpdated
? [
lastUpdated,
...((files === null || files === void 0 ? void 0 : files.filter((fl) => (0, grab_folders_files_string_paths_1.fldFileToStr)(fl) !== lastUpdated)) ||
[]),
]
: files;
if (firstFile && (sortedFilesByLastUpdated === null || sortedFilesByLastUpdated === void 0 ? void 0 : sortedFilesByLastUpdated[0])) {
(0, files_1.default)({ files: sortedFilesByLastUpdated, options });
}
else if (firstFolder && (folders === null || folders === void 0 ? void 0 : folders[0])) {
(0, folders_1.default)({ folders, options });
else if (firstFolder && (sortedFoldersByLastUpdated === null || sortedFoldersByLastUpdated === void 0 ? void 0 : sortedFoldersByLastUpdated[0])) {
(0, folders_1.default)({ folders: sortedFoldersByLastUpdated, options });
}
}
catch (error) {
@@ -40,7 +89,6 @@ catch (error) {
}
process.on("exit", (code) => {
if (code == global.SYNC_SUCCESS_EXIT_CODE) {
fs_1.default.unlinkSync(global.SYNCING_FILE);
const args = process.argv;
const cmd = args.shift();
if (cmd) {
+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",
});
}
}
}
}
+28 -92
View File
@@ -14,17 +14,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFolders;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process");
const delay_1 = __importDefault(require("../../utils/delay"));
const sync_1 = __importDefault(require("../../utils/sync"));
function watchFolders(_a) {
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
let timeout;
let isSyncing = false;
const UPDATE_TIMEOUT = 500;
const UPDATE_TIMEOUT = 1000;
try {
const dirs = folders;
console.log("global.SYNCING", global.SYNCING);
console.log(`Now handling ${dirs.length} Directories`);
/**
* # Watch Directories
*/
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
@@ -33,6 +35,7 @@ function watchFolders(_a) {
continue;
}
const dirPath = typeof dir == "string" ? dir : dir.path;
console.log("global.SYNCING", global.SYNCING);
if ((typeof dir == "string" && !fs_1.default.existsSync(dirPath)) ||
(typeof dir == "object" &&
dir.path &&
@@ -54,22 +57,6 @@ function watchFolders(_a) {
fs_1.default.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);
@@ -77,28 +64,41 @@ function watchFolders(_a) {
}
}
if (typeof dir == "string") {
sync({ dirPath, dirs, options });
yield (0, delay_1.default)();
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
console.log("Folder Changed", evt, fileName);
if (fs_1.default.existsSync(global.SYNCING_FILE) || isSyncing) {
if (global.SYNCING) {
console.log("Existing Sync found. Returning ...");
return;
}
clearTimeout(timeout);
timeout = setTimeout(() => {
console.log("Folder Syncing in progress ...");
console.log(`Writing Sync File =>${global.SYNCING_FILE}`);
fs_1.default.writeFileSync(global.SYNCING_FILE, `SYNCING Folder: evt:${evt} :: fileName:${fileName}`);
isSyncing = true;
sync({ dirPath, dirs, options });
setTimeout(() => {
global.SYNCING = true;
(0, sync_1.default)({ dirPath, dirs, options }).finally(() => {
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
}, INTERVAL);
});
}, INTERVAL);
});
}
}
/**
* # Sync Last Updated
*/
const lastUpdatedDir = dirs[0];
const lastUpdatedDirPath = typeof lastUpdatedDir == "string"
? lastUpdatedDir
: lastUpdatedDir.path;
global.SYNCING = true;
yield (0, sync_1.default)({
dirPath: lastUpdatedDirPath,
dirs,
options,
firstRun: true,
});
setTimeout(() => {
global.SYNCING = false;
}, UPDATE_TIMEOUT);
}
catch (error) {
console.log("ERROR:", error.message);
@@ -106,67 +106,3 @@ function watchFolders(_a) {
}
});
}
function sync({ options, dirs, dirPath, init }) {
var _a, _b;
const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string")
return dr !== dirPath;
if (dr === null || dr === void 0 ? void 0 : dr.path)
return dr.path !== dirPath;
return false;
});
for (let j = 0; j < dstDirs.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.include) === null || _a === void 0 ? void 0 : _a[0]) {
options.include.forEach((incl) => {
cmdArray.push(`--include='${incl}'`);
});
}
if ((_b = options === null || options === void 0 ? void 0 : options.exclude) === null || _b === void 0 ? void 0 : _b[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude='${excl}'`);
});
}
const dstDr = dstDirs[j];
if (typeof dstDr == "string") {
if (!fs_1.default.existsSync(dstDr))
continue;
if (dirPath === dstDr) {
console.log(`You can't sync the same paths. Please check your configuration and resolve duplicate paths`);
process.exit(6);
}
cmdArray.push(path_1.default.normalize(dirPath) + "/", path_1.default.normalize(dstDr) + "/");
const cmd = cmdArray.join(" ");
(0, child_process_1.execSync)(cmd, {
stdio: "inherit",
});
}
else if (dstDr.path) {
if (!dstDr.host && !fs_1.default.existsSync(dstDr.path))
continue;
if (dirPath === dstDr.path) {
console.log(`You can't sync the same paths. Please check your configuration and resolve duplicate paths`);
process.exit(6);
}
if (dstDr.host && dstDr.ssh_key && dstDr.user) {
cmdArray.push("-e", `'ssh -i ${dstDr.ssh_key}'`);
cmdArray.push(path_1.default.normalize(dirPath) + "/", `${dstDr.user}@${dstDr.host}:${dstDr.path}/`);
const cmd = cmdArray.join(" ");
(0, child_process_1.execSync)(cmd, {
stdio: "inherit",
});
}
else {
cmdArray.push(path_1.default.normalize(dirPath), path_1.default.normalize(dstDr.path));
const cmd = cmdArray.join(" ");
(0, child_process_1.execSync)(cmd, {
stdio: "inherit",
});
}
}
}
console.log("Folder Sync Complete. Exiting ...");
}