UNPKG

@masuidrive/ticket

Version:

Real-time ticket tracking viewer with Vite + Express

75 lines 2.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileWatcher = void 0; const chokidar_1 = require("chokidar"); const events_1 = require("events"); const path_1 = __importDefault(require("path")); const crypto_1 = require("crypto"); const promises_1 = require("fs/promises"); class FileWatcher extends events_1.EventEmitter { constructor(projectRoot) { super(); this.fileHashes = new Map(); this.projectRoot = projectRoot || process.cwd(); } async getFileHash(filePath) { try { const content = await (0, promises_1.readFile)(filePath, 'utf-8'); return (0, crypto_1.createHash)('sha256').update(content).digest('hex'); } catch (error) { return null; } } watchFile(filePath = 'current-ticket.md') { const fullPath = path_1.default.join(this.projectRoot, filePath); // Get initial hash this.getFileHash(fullPath).then(hash => { if (hash) { this.fileHashes.set(filePath, hash); } }); this.watcher = (0, chokidar_1.watch)(fullPath, { persistent: true, ignoreInitial: true, awaitWriteFinish: { stabilityThreshold: 500, pollInterval: 100 } }); this.watcher.on('change', async () => { // Check if content actually changed using SHA256 const newHash = await this.getFileHash(fullPath); const oldHash = this.fileHashes.get(filePath); if (newHash && newHash !== oldHash) { this.fileHashes.set(filePath, newHash); this.emit('fileChanged', filePath); } }); this.watcher.on('add', async () => { const hash = await this.getFileHash(fullPath); if (hash) { this.fileHashes.set(filePath, hash); } this.emit('fileAdded', filePath); }); this.watcher.on('unlink', () => { this.fileHashes.delete(filePath); this.emit('fileRemoved', filePath); }); this.watcher.on('error', (error) => { this.emit('error', error); }); } stop() { if (this.watcher) { this.watcher.close(); } this.fileHashes.clear(); } } exports.FileWatcher = FileWatcher; //# sourceMappingURL=fileWatcher.js.map