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

88 lines
4.4 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 = watchFiles;
const fs_1 = __importDefault(require("fs"));
const delay_1 = __importDefault(require("../../utils/delay"));
const sync_1 = __importDefault(require("../../utils/sync"));
const sync_scheduler_1 = __importDefault(require("../../utils/sync-scheduler"));
function watchFiles(_a) {
return __awaiter(this, arguments, void 0, function* ({ files, options, }) {
const UPDATE_TIMEOUT = 1000;
try {
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
const scheduler = new sync_scheduler_1.default((filePath, firstRun) => (0, sync_1.default)({
options,
dirPath: filePath,
dirs: files,
isFiles: true,
firstRun,
}), INTERVAL);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = typeof file == "string" ? file : (file === null || file === void 0 ? void 0 : file.path) ? file.path : null;
const interval = typeof file == "object" ? file.interval : null;
if (!filePath)
continue;
if (typeof file == "string" && !fs_1.default.existsSync(filePath)) {
try {
const existingFilePath = files.find((fl) => {
if (typeof fl == "string")
return fs_1.default.existsSync(fl);
if (!fl.host)
return fs_1.default.existsSync(fl.path); // TODO handle remote
});
if (!existingFilePath) {
throw new Error("No existing Files for reference");
}
const fileDirPath = typeof existingFilePath == "string"
? existingFilePath
: existingFilePath.path;
if (!fs_1.default.existsSync(fileDirPath)) {
fs_1.default.mkdirSync(fileDirPath, { recursive: true });
}
fs_1.default.writeFileSync(filePath, "");
}
catch (error) {
throw new Error(`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
}
}
if (typeof file == "string" && !fs_1.default.statSync(filePath).isFile()) {
throw new Error(`'${filePath}' is not a File!`);
}
if (typeof file == "object" && file.host) {
// TODO Handle SSH
}
else if (typeof file == "string") {
yield (0, delay_1.default)();
fs_1.default.watchFile(filePath, {
interval: interval || 200,
}, (curr, prev) => {
scheduler.schedule(filePath);
});
}
}
const lastUpdatedFile = files[0];
const lastUpdatedFilePath = typeof lastUpdatedFile == "string"
? lastUpdatedFile
: lastUpdatedFile.path;
scheduler.enqueue(lastUpdatedFilePath, true);
}
catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
});
}