155 lines
6.9 KiB
JavaScript
155 lines
6.9 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 = 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"));
|
|
let timeout;
|
|
const UPDATE_TIMEOUT = 2000;
|
|
function watchFolders(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
|
|
try {
|
|
const dirs = folders;
|
|
console.log(`Now handling ${dirs.length} 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;
|
|
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,
|
|
});
|
|
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);
|
|
throw new Error(`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
|
|
}
|
|
}
|
|
if (typeof dir == "string") {
|
|
sync({ dirPath, dirs, options });
|
|
yield (0, delay_1.default)();
|
|
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(() => {
|
|
sync({ dirPath, dirs, options });
|
|
process.exit(1);
|
|
}, INTERVAL);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.log("ERROR:", error.message);
|
|
process.exit(0);
|
|
}
|
|
});
|
|
}
|
|
function sync({ options, dirs, dirPath, init }) {
|
|
var _a;
|
|
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.exclude) === null || _a === void 0 ? void 0 : _a[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",
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|