Updates
This commit is contained in:
Vendored
+28
-92
@@ -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 ...");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user