11import { serve } from './serve' ;
2+ import { generateTypes } from './generate' ;
23import chalk from 'chalk' ;
4+ import * as fs from 'fs' ;
5+ import * as path from 'path' ;
36
47/**
58 * Start development server with hot reload
@@ -12,19 +15,68 @@ export async function dev(options: {
1215 modules ?: string ;
1316 watch ?: boolean ;
1417} ) {
15- console . log ( chalk . cyan ( '🚀 Starting ObjectQL Development Server...\n' ) ) ;
18+ console . log ( chalk . cyan ( '🚀 Starting ObjectQL Development Server...' ) ) ;
19+
20+ const sourceDir = path . resolve ( process . cwd ( ) , options . dir || '.' ) ;
21+ // Default output for types in dev mode
22+ const typeOutputDir = path . resolve ( process . cwd ( ) , 'src/generated' ) ;
23+
24+ // 1. Initial Type Generation
25+ try {
26+ await generateTypes ( sourceDir , typeOutputDir ) ;
27+ } catch ( e ) {
28+ console . error ( chalk . red ( 'Initial type generation failed:' ) , e ) ;
29+ // Continue anyway, maybe it's just a starting project
30+ }
1631
17- // For now, delegate to serve command
18- // In future, can add file watching and auto-reload
32+ // 2. Setup Watcher (Before serve, in case serve blocks)
33+ if ( options . watch !== false ) {
34+ startWatcher ( sourceDir , typeOutputDir ) ;
35+ }
36+
37+ console . log ( chalk . blue ( `\n🌐 Server context: ${ sourceDir } ` ) ) ;
38+
39+ // 3. Start Server
1940 await serve ( {
2041 port : options . port ,
2142 dir : options . dir ,
2243 config : options . config ,
2344 modules : options . modules
2445 } ) ;
46+ }
47+
48+ let debounceTimer : NodeJS . Timeout | null = null ;
49+
50+ function startWatcher ( sourceDir : string , outputDir : string ) {
51+ console . log ( chalk . yellow ( '👀 Watching for metadata changes...' ) ) ;
2552
26- if ( options . watch !== false ) {
27- console . log ( chalk . yellow ( '\n👀 Watching for file changes... (Not yet implemented)' ) ) ;
28- console . log ( chalk . gray ( ' Tip: Use --no-watch to disable file watching' ) ) ;
53+ try {
54+ // Recursive watch if supported (macOS/Windows support strict recursive)
55+ // Linux might need fallback or libraries like chokidar, but we use fs.watch for zero-dep strictness
56+ const watcher = fs . watch ( sourceDir , { recursive : true } , ( eventType , filename ) => {
57+ if ( ! filename ) return ;
58+
59+ // Only care about YAML
60+ if ( filename . endsWith ( '.yml' ) || filename . endsWith ( '.yaml' ) ) {
61+ // Debounce
62+ if ( debounceTimer ) clearTimeout ( debounceTimer ) ;
63+
64+ debounceTimer = setTimeout ( async ( ) => {
65+ console . log ( chalk . gray ( `\n📝 Change detected: ${ filename } ` ) ) ;
66+ try {
67+ console . log ( chalk . blue ( '⚡️ Regenerating types...' ) ) ;
68+ await generateTypes ( sourceDir , outputDir ) ;
69+ } catch ( e ) {
70+ console . error ( chalk . red ( 'Type generation failed:' ) , e ) ;
71+ }
72+ } , 500 ) ; // 500ms debounce
73+ }
74+ } ) ;
75+
76+ watcher . on ( 'error' , ( e ) => console . error ( chalk . red ( 'Watcher error:' ) , e ) ) ;
77+
78+ } catch ( e ) {
79+ console . warn ( chalk . yellow ( 'Native recursive watch not supported or failed. Watching root only.' ) ) ;
80+ // Fallback logic could go here
2981 }
3082}
0 commit comments