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
+2
View File
@@ -0,0 +1,2 @@
import { SyncFoldersFnParams } from "../../types";
export default function watchFolders({ folders, options, }: SyncFoldersFnParams): Promise<void>;
+167
View File
@@ -0,0 +1,167 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFolders;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const util_1 = __importDefault(require("util"));
const child_process_1 = require("child_process");
const delay_1 = __importDefault(require("../../utils/delay"));
const write_sync_config_1 = __importDefault(require("../../utils/write-sync-config"));
const execPromise = util_1.default.promisify(child_process_1.exec);
function watchFolders(_a) {
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
let timeout;
const UPDATE_TIMEOUT = 1000;
try {
const dirs = folders;
console.log(`Now handling ${dirs.length} Directories`);
/**
* # Sync Last Updated
*/
const lastUpdatedDir = dirs[0];
const lastUpdatedDirPath = typeof lastUpdatedDir == "string"
? lastUpdatedDir
: lastUpdatedDir.path;
yield sync({ dirPath: lastUpdatedDirPath, dirs, options });
/**
* # 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];
if (!dir) {
console.log(`Dir: ${dir} doesn't exist`);
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 &&
!dir.host &&
!fs_1.default.existsSync(dir.path))) {
console.log(`Dir ${dirPath} does not exist. Creating ...`);
try {
const existingDirPath = dirs.find((dr) => {
if (typeof dr == "string")
return fs_1.default.existsSync(dr);
if (!dr.host)
return fs_1.default.existsSync(dr.path); // TODO handle remote
return false;
});
console.log(`Existing Dir to clone: ${existingDirPath}`);
if (!existingDirPath) {
throw new Error("No existing Directories for reference");
}
fs_1.default.mkdirSync(dirPath, {
recursive: true,
});
}
catch (error) {
console.log("Error:", error.message);
throw new Error(`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
}
}
if (typeof dir == "string") {
yield (0, delay_1.default)();
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
console.log("Folder Changed", evt, fileName);
if (global.SYNCING) {
console.log("Existing Sync found. Returning ...");
return;
}
clearTimeout(timeout);
timeout = setTimeout(() => {
console.log("Folder Syncing in progress ...");
global.SYNCING = true;
(0, write_sync_config_1.default)({
status: "syncing",
lastSyncedPath: dirPath,
});
sync({ dirPath, dirs, options }).finally(() => {
setTimeout(() => {
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
}, INTERVAL);
});
}, INTERVAL);
});
}
}
}
catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
});
}
function sync(_a) {
return __awaiter(this, arguments, void 0, function* ({ options, dirs, dirPath, init }) {
var _b, _c;
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;
});
const allCommandsArr = [];
for (let j = 0; j < dstDirs.length; j++) {
const dstDr = dstDirs[j];
let cmdArray = ["rsync", "-azu", "--inplace"];
if (options === null || options === void 0 ? void 0 : options.delete) {
cmdArray.push("--delete");
}
if ((_b = options === null || options === void 0 ? void 0 : options.include) === null || _b === void 0 ? void 0 : _b[0]) {
options.include.forEach((incl) => {
cmdArray.push(`--include='${incl}'`);
});
}
if ((_c = options === null || options === void 0 ? void 0 : options.exclude) === null || _c === void 0 ? void 0 : _c[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude='${excl}'`);
});
}
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) + "/");
}
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}/`);
}
else {
cmdArray.push(path_1.default.normalize(dirPath), path_1.default.normalize(dstDr.path));
}
}
allCommandsArr.push(cmdArray);
}
yield Promise.all(allCommandsArr.map((cmdArr) => {
return execPromise(cmdArr.join(" "));
}));
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
});
}
+6
View File
@@ -0,0 +1,6 @@
type Params = {
dirs?: string[];
files?: string[];
};
export default function getLatestSource({ dirs, files, }: Params): string | undefined;
export {};
+57
View File
@@ -0,0 +1,57 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getLatestSource;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function getLatestSource({ dirs, files, }) {
let latestDir = undefined;
let latestMtime = 0;
const isFiles = files === null || files === void 0 ? void 0 : files[0];
const finalPaths = isFiles ? files : dirs;
if (!finalPaths)
return undefined;
for (const pth of finalPaths) {
try {
const stats = fs_1.default.statSync(pth);
const pathMtime = stats.isDirectory()
? getLatestDirMtime(pth)
: stats.mtimeMs;
if (pathMtime > latestMtime) {
latestMtime = pathMtime;
latestDir = pth;
}
}
catch (error) {
console.error(`Error accessing ${pth}: ${error.message}`);
}
}
return latestDir;
}
function getLatestDirMtime(dir) {
let latestMtime = 0;
try {
const stats = fs_1.default.statSync(dir);
if (stats.isDirectory()) {
latestMtime = stats.mtimeMs;
const entries = fs_1.default.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path_1.default.join(dir, entry.name);
if (entry.isDirectory()) {
const subMtime = getLatestDirMtime(entryPath);
latestMtime = Math.max(latestMtime, subMtime);
}
else if (entry.isFile()) {
const fileStats = fs_1.default.statSync(entryPath);
latestMtime = Math.max(latestMtime, fileStats.mtimeMs);
}
}
}
}
catch (error) {
console.error(`Error accessing ${dir}: ${error.message}`);
}
return latestMtime;
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFileConfig } from "../types";
export default function getSyncConfig(): SyncFileConfig;
+22
View File
@@ -0,0 +1,22 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getSyncConfig;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
function getSyncConfig() {
try {
const { syncConfigFilePath } = (0, grab_dir_names_1.default)();
if (!fs_1.default.existsSync(syncConfigFilePath)) {
fs_1.default.writeFileSync(syncConfigFilePath, JSON.stringify({}), "utf-8");
return {};
}
const syncConfigJSON = fs_1.default.readFileSync(syncConfigFilePath, "utf-8");
return JSON.parse(syncConfigJSON);
}
catch (error) {
return { status: "error" };
}
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFileConfig } from "../types";
export default function getSyncConfig(): SyncFileConfig;
+22
View File
@@ -0,0 +1,22 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getSyncConfig;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
function getSyncConfig() {
try {
const { syncConfigFilePath } = (0, grab_dir_names_1.default)();
if (!fs_1.default.existsSync(syncConfigFilePath)) {
fs_1.default.writeFileSync(syncConfigFilePath, JSON.stringify({}), "utf-8");
return {};
}
const syncConfigJSON = fs_1.default.readFileSync(syncConfigFilePath, "utf-8");
return JSON.parse(syncConfigJSON);
}
catch (error) {
return { status: "error" };
}
}
+5
View File
@@ -0,0 +1,5 @@
export default function grabDirNames(): {
rootDir: string;
configFileName: string;
configFilePath: string;
};
+13
View File
@@ -0,0 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDirNames;
const path_1 = __importDefault(require("path"));
function grabDirNames() {
const rootDir = process.cwd();
const configFileName = "turbosync.config.json";
const configFilePath = path_1.default.join(rootDir, configFileName);
return { rootDir, configFileName, configFilePath };
}
+5
View File
@@ -0,0 +1,5 @@
export default function grabDirNames(): {
rootDir: string;
syncConfigFileName: string;
syncConfigFilePath: string;
};
+13
View File
@@ -0,0 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDirNames;
const path_1 = __importDefault(require("path"));
function grabDirNames() {
const rootDir = process.cwd();
const syncConfigFileName = "__trsyc.json";
const syncConfigFilePath = path_1.default.join(rootDir, syncConfigFileName);
return { rootDir, syncConfigFileName, syncConfigFilePath };
}
+3
View File
@@ -0,0 +1,3 @@
import { TurboSyncFileObject } from "../types";
export default function fldFileToStrArr(srces?: (string | TurboSyncFileObject)[]): string[] | undefined;
export declare function fldFileToStr(src?: string | TurboSyncFileObject): string | undefined;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = fldFileToStrArr;
exports.fldFileToStr = fldFileToStr;
function fldFileToStrArr(srces) {
if (!srces)
return undefined;
let arr = [];
for (let i = 0; i < srces.length; i++) {
const src = srces[i];
const srcStr = fldFileToStr(src);
if (srcStr) {
arr.push(srcStr);
}
}
return arr;
}
function fldFileToStr(src) {
if (!src)
return undefined;
if (typeof src == "string") {
return src;
}
else if (typeof src == "object" && src.path) {
return src.path;
}
return undefined;
}
+5
View File
@@ -0,0 +1,5 @@
export default function grabDirNames(): {
rootDir: string;
configFileName: string;
configFilePath: string;
};
+13
View File
@@ -0,0 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDirNames;
const path_1 = __importDefault(require("path"));
function grabDirNames() {
const rootDir = process.cwd();
const configFileName = "turbosync.config.json";
const configFilePath = path_1.default.join(rootDir, configFileName);
return { rootDir, configFileName, configFilePath };
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFoldersSyncFnParams } from "../types";
export default function sync({ options, dirs, dirPath, isFiles, firstRun, }: SyncFoldersSyncFnParams): Promise<void>;
+80
View File
@@ -0,0 +1,80 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = sync;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const util_1 = __importDefault(require("util"));
const child_process_1 = require("child_process");
const execPromise = util_1.default.promisify(child_process_1.exec);
function sync(_a) {
return __awaiter(this, arguments, void 0, function* ({ options, dirs, dirPath, isFiles, firstRun, }) {
var _b, _c;
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;
});
const rsyncTrailingSlash = isFiles ? "" : "/";
const allCommandsArr = [];
for (let j = 0; j < dstDirs.length; j++) {
const dstDr = dstDirs[j];
let cmdArray = ["rsync", firstRun ? "-az" : "-azu", "--inplace"];
if (options === null || options === void 0 ? void 0 : options.delete) {
cmdArray.push("--delete");
}
if ((_b = options === null || options === void 0 ? void 0 : options.include) === null || _b === void 0 ? void 0 : _b[0]) {
options.include.forEach((incl) => {
cmdArray.push(`--include='${incl}'`);
});
}
if ((_c = options === null || options === void 0 ? void 0 : options.exclude) === null || _c === void 0 ? void 0 : _c[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude='${excl}'`);
});
}
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) + rsyncTrailingSlash, path_1.default.normalize(dstDr) + rsyncTrailingSlash);
}
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) + rsyncTrailingSlash, `${dstDr.user}@${dstDr.host}:${dstDr.path}${rsyncTrailingSlash}`);
}
else {
cmdArray.push(path_1.default.normalize(dirPath), path_1.default.normalize(dstDr.path));
}
}
allCommandsArr.push(cmdArray);
}
yield Promise.all(allCommandsArr.map((cmdArr) => {
return execPromise(cmdArr.join(" "));
}));
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
});
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFileConfig } from "../types";
export default function writeSyncConfig(config: SyncFileConfig): boolean;
+18
View File
@@ -0,0 +1,18 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = writeSyncConfig;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
function writeSyncConfig(config) {
try {
const { syncConfigFilePath } = (0, grab_dir_names_1.default)();
fs_1.default.writeFileSync(syncConfigFilePath, JSON.stringify(config), "utf-8");
return true;
}
catch (error) {
return false;
}
}