turbo-sync/dist/utils/sync.js
Benjamin Toby 822778d43b Updates
2025-07-21 13:51:59 +01:00

81 lines
4.1 KiB
JavaScript

"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 ...`);
});
}