UNPKG

build-in-public-bot

Version:

AI-powered CLI bot for automating build-in-public tweets with code screenshots

157 lines 5.63 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WatcherService = void 0; const events_1 = require("events"); const chokidar_1 = __importDefault(require("chokidar")); const path = __importStar(require("path")); const fs = __importStar(require("fs/promises")); const logger_1 = require("../utils/logger"); class WatcherService extends events_1.EventEmitter { static instance; watcher = null; ignoredPatterns = [ '**/node_modules/**', '**/.git/**', '**/dist/**', '**/build/**', '**/.next/**', '**/coverage/**', '**/*.log', '**/.DS_Store', '**/package-lock.json', '**/yarn.lock', ]; constructor() { super(); } static getInstance() { if (!WatcherService.instance) { WatcherService.instance = new WatcherService(); } return WatcherService.instance; } async start(watchPath = process.cwd()) { if (this.watcher) { await this.stop(); } logger_1.logger.info(`Starting file watcher on: ${watchPath}`); this.watcher = chokidar_1.default.watch(watchPath, { ignored: this.ignoredPatterns, persistent: true, ignoreInitial: true, awaitWriteFinish: { stabilityThreshold: 1000, pollInterval: 100, }, }); this.watcher .on('add', (filePath) => this.handleFileEvent('add', filePath)) .on('change', (filePath) => this.handleFileEvent('change', filePath)) .on('unlink', (filePath) => this.handleFileEvent('unlink', filePath)) .on('error', (error) => logger_1.logger.error('Watcher error:', error)); await new Promise((resolve) => { this.watcher.on('ready', () => { logger_1.logger.info('File watcher is ready'); resolve(); }); }); } async stop() { if (this.watcher) { await this.watcher.close(); this.watcher = null; logger_1.logger.info('File watcher stopped'); } } async handleFileEvent(type, filePath) { try { const change = { type, path: filePath, timestamp: new Date(), }; if (type !== 'unlink' && this.isCodeFile(filePath)) { try { change.content = await fs.readFile(filePath, 'utf-8'); } catch (error) { logger_1.logger.debug(`Could not read file ${filePath}:`, error); } } this.emit('fileChange', change); } catch (error) { logger_1.logger.error(`Error handling file event for ${filePath}:`, error); } } isCodeFile(filePath) { const codeExtensions = [ '.js', '.jsx', '.ts', '.tsx', '.py', '.java', '.cpp', '.c', '.h', '.cs', '.php', '.rb', '.go', '.rs', '.swift', '.kt', '.scala', '.html', '.css', '.scss', '.sass', '.less', '.vue', '.svelte', ]; const ext = path.extname(filePath).toLowerCase(); return codeExtensions.includes(ext); } addIgnorePattern(pattern) { this.ignoredPatterns.push(pattern); if (this.watcher) { const watchPath = this.watcher.options.cwd || process.cwd(); this.stop().then(() => this.start(watchPath)); } } removeIgnorePattern(pattern) { const index = this.ignoredPatterns.indexOf(pattern); if (index > -1) { this.ignoredPatterns.splice(index, 1); if (this.watcher) { const watchPath = this.watcher.options.cwd || process.cwd(); this.stop().then(() => this.start(watchPath)); } } } getIgnorePatterns() { return [...this.ignoredPatterns]; } isWatching() { return this.watcher !== null; } } exports.WatcherService = WatcherService; //# sourceMappingURL=watcher.js.map