"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 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; 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]; 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, sync_1.default)({ dirPath, dirs, options }).finally(() => { process.exit(global.SYNC_SUCCESS_EXIT_CODE); }); }, 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); process.exit(0); } }); }