Files
turbo-sync/dist/utils/sync.js
T
2026-08-10 11:07:39 +01:00

153 lines
6.6 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 os_1 = __importDefault(require("os"));
const util_1 = __importDefault(require("util"));
const crypto_1 = __importDefault(require("crypto"));
const child_process_1 = require("child_process");
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
const grab_folders_files_string_paths_1 = require("./grab-folders-files-string-paths");
const delay_1 = __importDefault(require("./delay"));
const execPromise = util_1.default.promisify(child_process_1.exec);
const LOCK_STALE_MS = 5 * 60 * 1000;
const LOCK_RETRY_MS = 100;
function lockPathFor(dirPath) {
const hash = crypto_1.default
.createHash("sha1")
.update(path_1.default.resolve(dirPath))
.digest("hex")
.slice(0, 16);
return path_1.default.join(os_1.default.tmpdir(), `turbosync-${hash}.lock`);
}
function acquireLock(lockPath) {
return __awaiter(this, void 0, void 0, function* () {
while (true) {
try {
const fd = fs_1.default.openSync(lockPath, "wx");
fs_1.default.writeFileSync(fd, String(process.pid));
fs_1.default.closeSync(fd);
return;
}
catch (error) {
if (error.code !== "EEXIST")
throw error;
try {
const { mtimeMs } = fs_1.default.statSync(lockPath);
if (Date.now() - mtimeMs > LOCK_STALE_MS) {
fs_1.default.unlinkSync(lockPath);
continue;
}
}
catch (_a) {
continue;
}
yield (0, delay_1.default)(LOCK_RETRY_MS);
}
}
});
}
function acquireLocks(lockPaths) {
return __awaiter(this, void 0, void 0, function* () {
for (const lockPath of lockPaths) {
yield acquireLock(lockPath);
}
});
}
function releaseLocks(lockPaths) {
for (const lockPath of lockPaths) {
try {
fs_1.default.unlinkSync(lockPath);
}
catch (_a) { }
}
}
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 { ignoreFileName } = (0, grab_dir_names_1.default)();
const rsyncIgnoreFile = path_1.default.join(dirPath, ignoreFileName);
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 (fs_1.default.existsSync(rsyncIgnoreFile)) {
cmdArray.push(`--exclude-from=${rsyncIgnoreFile}`);
}
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);
}
const lockPaths = [dirPath, ...dstDirs.map((dr) => (0, grab_folders_files_string_paths_1.fldFileToStr)(dr))]
.filter((pth) => Boolean(pth))
.map((pth) => lockPathFor(pth))
.sort();
yield acquireLocks(lockPaths);
try {
yield Promise.all(allCommandsArr.map((cmdArr) => {
return execPromise(cmdArr.join(" "));
}));
}
finally {
releaseLocks(lockPaths);
}
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
});
}