|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:async'; |
| 3 | +import '../libs/swagger/auto_openapi_generator.dart'; |
| 4 | + |
| 5 | +class SwaggerWatcher { |
| 6 | + Timer? _debounceTimer; |
| 7 | + bool _isGenerating = false; |
| 8 | + |
| 9 | + Future<void> startWatching() async { |
| 10 | + print('🔍 Khởi động file watcher cho routes...'); |
| 11 | + print('📁 Đang theo dõi: routes/ directory'); |
| 12 | + print('📄 Output: public/swagger/openapi.yaml'); |
| 13 | + print('🌐 Swagger UI: http://localhost:8080/swagger'); |
| 14 | + print(''); |
| 15 | + |
| 16 | + // Tạo OpenAPI ban đầu |
| 17 | + await _generateOpenAPI(); |
| 18 | + |
| 19 | + // Theo dõi thay đổi file |
| 20 | + _watchDirectory('routes'); |
| 21 | + |
| 22 | + // Giữ process hoạt động |
| 23 | + print('👀 Đang theo dõi thay đổi... (Nhấn Ctrl+C để dừng)'); |
| 24 | + await _keepAlive(); |
| 25 | + } |
| 26 | + |
| 27 | + void _watchDirectory(String path) { |
| 28 | + final directory = Directory(path); |
| 29 | + if (!directory.existsSync()) { |
| 30 | + print('❌ Không tìm thấy thư mục routes: $path'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + directory.watch(recursive: true).listen((event) { |
| 35 | + if (event.path.endsWith('.dart') && !event.path.contains('_middleware')) { |
| 36 | + final fileName = event.path.split(Platform.pathSeparator).last; |
| 37 | + print('📝 File thay đổi: $fileName'); |
| 38 | + _debounceGeneration(); |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + void _debounceGeneration() { |
| 44 | + _debounceTimer?.cancel(); |
| 45 | + _debounceTimer = Timer(const Duration(milliseconds: 800), () { |
| 46 | + if (!_isGenerating) { |
| 47 | + _generateOpenAPI(); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + Future<void> _generateOpenAPI() async { |
| 53 | + if (_isGenerating) return; |
| 54 | + |
| 55 | + _isGenerating = true; |
| 56 | + print('⚙️ Đang tạo lại OpenAPI spec...'); |
| 57 | + |
| 58 | + try { |
| 59 | + final generator = EnhancedAutoOpenAPIGenerator(); |
| 60 | + await generator.generateFromRoutes('routes'); |
| 61 | + print('✅ Cập nhật OpenAPI thành công!'); |
| 62 | + print('🔗 Xem tại: http://localhost:8080/swagger'); |
| 63 | + } catch (e) { |
| 64 | + print('❌ Lỗi khi tạo OpenAPI: $e'); |
| 65 | + } finally { |
| 66 | + _isGenerating = false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Future<void> _keepAlive() async { |
| 71 | + final completer = Completer<void>(); |
| 72 | + |
| 73 | + ProcessSignal.sigint.watch().listen((signal) { |
| 74 | + print('\n👋 Đang dừng watcher...'); |
| 75 | + _debounceTimer?.cancel(); |
| 76 | + completer.complete(); |
| 77 | + }); |
| 78 | + |
| 79 | + return completer.future; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void main() async { |
| 84 | + try { |
| 85 | + final watcher = SwaggerWatcher(); |
| 86 | + await watcher.startWatching(); |
| 87 | + } catch (e) { |
| 88 | + print('❌ Lỗi khởi động watcher: $e'); |
| 89 | + exit(1); |
| 90 | + } |
| 91 | +} |
0 commit comments