ferro-cli 0.2.24

CLI for scaffolding Ferro web applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
use super::clean;
use console::style;
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use notify::RecursiveMode;
use notify_debouncer_mini::{new_debouncer, DebouncedEvent};
use std::io::{self, BufRead, BufReader, IsTerminal, Write};
use std::net::TcpListener;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::Duration;

// Phase 145 — pure-function contracts and enums consumed by the
// BackendSupervisor that 145-02b will wire in. Bodies are filled by 145-02a
// against the inline test oracle below so later plans cannot drift.

/// Emit a stdout line with explicit CRLF so output renders correctly while the
/// keyboard thread has raw mode enabled (OPOST disabled). Safe when raw mode
/// is off — the extra \r lands at column 0 which is already the cursor position
/// after OPOST expands \n to \r\n.
macro_rules! sprintln {
    () => {{
        print!("\r\n");
        let _ = io::stdout().flush();
    }};
    ($($arg:tt)*) => {{
        print!("{}\r\n", format_args!($($arg)*));
        let _ = io::stdout().flush();
    }};
}

/// stderr counterpart to `sprintln!`.
macro_rules! seprintln {
    () => {{
        eprint!("\r\n");
        let _ = io::stderr().flush();
    }};
    ($($arg:tt)*) => {{
        eprint!("{}\r\n", format_args!($($arg)*));
        let _ = io::stderr().flush();
    }};
}

/// Reload trigger dispatched to the BackendSupervisor over an mpsc channel (D-06, D-20).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ReloadTrigger {
    Manual,
    FileChanged,
}

/// Result of classifying a keypress in the keyboard thread (D-06, D-07, D-08).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum KbAction {
    Reload,
    Quit,
}

/// Renders the startup banner. Pure function — `is_tty` and `is_watch` are explicit
/// so tests do not depend on the real stdin state (D-05, D-24). Body emits the
/// spec-verbatim literal from
/// docs/superpowers/specs/2026-04-22-ferro-serve-reload-key-design.md §CLI surface.
#[allow(clippy::too_many_arguments)]
pub(super) fn render_banner(
    is_watch: bool,
    is_tty: bool,
    backend_only: bool,
    frontend_only: bool,
    backend_host: &str,
    backend_port: u16,
    vite_port: u16,
) -> String {
    use std::fmt::Write;
    let mut s = String::new();
    if !frontend_only {
        let _ = writeln!(s, "Backend server on http://{backend_host}:{backend_port}");
    }
    if !backend_only {
        let _ = writeln!(s, "Frontend server on http://127.0.0.1:{vite_port}");
    }
    if !frontend_only {
        let _ = writeln!(s);
        if is_tty {
            let _ = writeln!(s, "  r        rebuild backend + regenerate types");
        } else {
            let _ = writeln!(s, "  r        unavailable (non-TTY stdin)");
        }
        let _ = writeln!(s, "  q        quit    (or Ctrl+C)");
        if is_watch {
            let _ = writeln!(s, "  watch    enabled  (debounce 500ms)");
        } else {
            let _ = writeln!(
                s,
                "  watch    disabled  (pass --watch to auto-reload on file changes)"
            );
        }
    }
    s
}

/// Classifies a keypress. Lowercase `r` → Reload; `q` or Ctrl-C → Quit; else None (D-08).
/// Signature uses the final crossterm types directly — no placeholder, no Plan-02 rewrite.
pub(super) fn classify_key(code: KeyCode, modifiers: KeyModifiers) -> Option<KbAction> {
    match (code, modifiers) {
        (KeyCode::Char('r'), KeyModifiers::NONE) => Some(KbAction::Reload),
        (KeyCode::Char('q'), KeyModifiers::NONE) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
            Some(KbAction::Quit)
        }
        _ => None,
    }
}

/// Formats a trigger source for the `[backend] reload triggered ({source})` log line (D-27, D-28).
pub(super) fn format_trigger_source(t: ReloadTrigger) -> &'static str {
    match t {
        ReloadTrigger::Manual => "manual",
        ReloadTrigger::FileChanged => "file change",
    }
}

/// Whether to spawn the keyboard thread. Equivalent to `is_tty`, isolated for testability (D-24).
pub(super) fn should_spawn_keyboard(is_tty: bool) -> bool {
    is_tty
}

/// Spawns a child process and streams its stdout/stderr to the terminal with a
/// colored prefix. The shutdown flag stops the reader threads when servers shut
/// down. Extracted from `ProcessManager::spawn_with_prefix_env` so 02b's
/// `BackendSupervisor` can reuse the same piping logic without duplicating it.
fn spawn_child_with_prefix(
    command: &str,
    args: &[&str],
    cwd: Option<&Path>,
    prefix: &str,
    color: console::Color,
    env_vars: &[(&str, &str)],
    shutdown: Arc<AtomicBool>,
) -> Result<Child, String> {
    let mut cmd = Command::new(command);
    cmd.args(args).stdout(Stdio::piped()).stderr(Stdio::piped());

    for (key, value) in env_vars {
        cmd.env(key, value);
    }

    if let Some(dir) = cwd {
        cmd.current_dir(dir);
    }

    let mut child = cmd
        .spawn()
        .map_err(|e| format!("Failed to spawn {command}: {e}"))?;

    let stdout = child.stdout.take().expect("stdout piped");
    let stderr = child.stderr.take().expect("stderr piped");
    let prefix_out = prefix.to_string();
    let prefix_err = prefix.to_string();
    let sd_out = shutdown.clone();
    let sd_err = shutdown;

    thread::spawn(move || {
        let reader = BufReader::new(stdout);
        for line in reader.lines() {
            if sd_out.load(Ordering::SeqCst) {
                break;
            }
            if let Ok(line) = line {
                // Emit CRLF explicitly: when the keyboard thread has enabled
                // raw mode, OPOST is off and a lone \n leaves the cursor
                // wherever the prior line ended. \r\n is a no-op extra \r when
                // raw mode is off (cursor already at column 0 after OPOST
                // expands \n to \r\n), so this is safe in both modes.
                print!("{} {}\r\n", style(&prefix_out).fg(color).bold(), line);
                let _ = io::stdout().flush();
            }
        }
    });

    thread::spawn(move || {
        let reader = BufReader::new(stderr);
        for line in reader.lines() {
            if sd_err.load(Ordering::SeqCst) {
                break;
            }
            if let Ok(line) = line {
                eprint!("{} {}\r\n", style(&prefix_err).fg(color).bold(), line);
                let _ = io::stderr().flush();
            }
        }
    });

    Ok(child)
}

struct ProcessManager {
    children: Vec<Child>,
    shutdown: Arc<AtomicBool>,
}

impl ProcessManager {
    fn new() -> Self {
        Self {
            children: Vec::new(),
            shutdown: Arc::new(AtomicBool::new(false)),
        }
    }

    fn spawn_with_prefix_env(
        &mut self,
        command: &str,
        args: &[&str],
        cwd: Option<&Path>,
        prefix: &str,
        color: console::Color,
        env_vars: &[(&str, &str)],
    ) -> Result<(), String> {
        let child = spawn_child_with_prefix(
            command,
            args,
            cwd,
            prefix,
            color,
            env_vars,
            self.shutdown.clone(),
        )?;
        self.children.push(child);
        Ok(())
    }

    fn shutdown_all(&mut self) {
        self.shutdown.store(true, Ordering::SeqCst);
        for child in &mut self.children {
            let _ = child.kill();
            let _ = child.wait();
        }
    }
}

fn get_package_name() -> Result<String, String> {
    let cargo_toml = Path::new("Cargo.toml");
    let content = std::fs::read_to_string(cargo_toml)
        .map_err(|e| format!("Failed to read Cargo.toml: {e}"))?;

    let parsed: toml::Value = content
        .parse()
        .map_err(|e| format!("Failed to parse Cargo.toml: {e}"))?;

    parsed
        .get("package")
        .and_then(|p| p.get("name"))
        .and_then(|n| n.as_str())
        .map(|s| s.to_string())
        .ok_or_else(|| "Could not find package name in Cargo.toml".to_string())
}

fn validate_ferro_project(backend_only: bool, frontend_only: bool) -> Result<(), String> {
    let cargo_toml = Path::new("Cargo.toml");
    let frontend_dir = Path::new("frontend");

    if !frontend_only && !cargo_toml.exists() {
        return Err("No Cargo.toml found. Are you in a Ferro project directory?".into());
    }

    if !backend_only && !frontend_dir.exists() {
        return Err("No frontend directory found. Are you in a Ferro project directory?".into());
    }

    Ok(())
}

fn ensure_npm_dependencies() -> Result<(), String> {
    let frontend_path = Path::new("frontend");
    let node_modules = frontend_path.join("node_modules");

    if !node_modules.exists() {
        sprintln!("{}", style("Installing frontend dependencies...").yellow());
        let npm_install = Command::new("npm")
            .args(["install"])
            .current_dir(frontend_path)
            .status()
            .map_err(|e| format!("Failed to run npm install: {e}"))?;

        if !npm_install.success() {
            return Err("Failed to install npm dependencies".into());
        }
        sprintln!(
            "{}",
            style("Frontend dependencies installed successfully.").green()
        );
    }

    Ok(())
}

fn find_available_port(start: u16, max_attempts: u16) -> u16 {
    for offset in 0..max_attempts {
        let port = start + offset;
        if TcpListener::bind(("127.0.0.1", port)).is_ok() {
            return port;
        }
    }
    start
}

/// RAII guard that disables raw mode on Drop. Restores cooked mode on both
/// normal exit and panic unwind (D-25).
struct RawModeGuard;

impl Drop for RawModeGuard {
    fn drop(&mut self) {
        let _ = disable_raw_mode();
    }
}

/// Spawns the crossterm keyboard-input thread. Returns None when stdin is not
/// a TTY (D-24) or when `enable_raw_mode()` fails (D-26). The returned
/// JoinHandle can be joined during shutdown so the Drop guard runs
/// deterministically (D-29 step 4).
fn spawn_keyboard_thread(
    tx: Sender<ReloadTrigger>,
    shutdown: Arc<AtomicBool>,
) -> Option<JoinHandle<()>> {
    let is_tty = std::io::stdin().is_terminal();
    if !should_spawn_keyboard(is_tty) {
        return None;
    }
    if let Err(e) = enable_raw_mode() {
        seprintln!("{} raw mode unavailable: {e}", style("Warning:").yellow());
        return None;
    }
    Some(thread::spawn(move || {
        let _guard = RawModeGuard;
        while !shutdown.load(Ordering::SeqCst) {
            match event::poll(Duration::from_millis(100)) {
                Ok(true) => {}
                _ => continue,
            }
            let Ok(Event::Key(k)) = event::read() else {
                continue;
            };
            // Windows fix: ignore key-release events (crossterm 0.26+).
            if k.kind != KeyEventKind::Press {
                continue;
            }
            match classify_key(k.code, k.modifiers) {
                Some(KbAction::Reload) => {
                    let _ = tx.send(ReloadTrigger::Manual);
                }
                Some(KbAction::Quit) => {
                    shutdown.store(true, Ordering::SeqCst);
                    break;
                }
                None => {}
            }
        }
    }))
}

/// Inner factoring of the file-watcher so unit tests can inject a short debounce
/// window and a tempdir path. The public wrapper `spawn_file_watcher` pins the
/// 500ms window (D-19) and the `src/` path (D-20). Returns `None` on any soft
/// failure (missing dir, notify init error, initial watch() error) so serve
/// continues as an effective no-op (D-22).
fn spawn_file_watcher_at(
    src: &Path,
    debounce: Duration,
    tx: Sender<ReloadTrigger>,
) -> Option<notify_debouncer_mini::Debouncer<notify::RecommendedWatcher>> {
    if !src.is_dir() {
        seprintln!(
            "{} {} missing, --watch disabled",
            style("Warning:").yellow(),
            src.display()
        );
        return None;
    }
    let mut debouncer = match new_debouncer(
        debounce,
        move |res: notify_debouncer_mini::DebounceEventResult| {
            let Ok(events) = res else {
                return;
            };
            let any_rs = events
                .iter()
                .any(|e: &DebouncedEvent| e.path.extension().map(|x| x == "rs").unwrap_or(false));
            if any_rs {
                let _ = tx.send(ReloadTrigger::FileChanged);
            }
        },
    ) {
        Ok(d) => d,
        Err(e) => {
            seprintln!("{} notify init failed: {e}", style("Warning:").yellow());
            return None;
        }
    };
    if let Err(e) = debouncer.watcher().watch(src, RecursiveMode::Recursive) {
        seprintln!(
            "{} watch({}) failed: {e}",
            style("Warning:").yellow(),
            src.display()
        );
        return None;
    }
    Some(debouncer)
}

/// Spawns the production file-watcher with the spec-mandated 500ms debounce
/// (D-19) against `src/` recursive (D-20).
fn spawn_file_watcher(
    tx: Sender<ReloadTrigger>,
) -> Option<notify_debouncer_mini::Debouncer<notify::RecommendedWatcher>> {
    spawn_file_watcher_at(Path::new("src"), Duration::from_millis(500), tx)
}

/// Owns the backend `cargo run` child exclusively (D-13). Consumes reload
/// triggers from an mpsc channel, coalescing bursts (D-17) into a single
/// kill → regenerate types → respawn cycle.
struct BackendSupervisor {
    package_name: String,
    skip_types: bool,
    project_path: PathBuf,
    types_output_path: PathBuf,
    current: Option<Child>,
    shutdown: Arc<AtomicBool>,
}

impl BackendSupervisor {
    fn new(
        package_name: String,
        skip_types: bool,
        project_path: PathBuf,
        types_output_path: PathBuf,
        shutdown: Arc<AtomicBool>,
    ) -> Self {
        Self {
            package_name,
            skip_types,
            project_path,
            types_output_path,
            current: None,
            shutdown,
        }
    }

    /// Kill and reap the in-flight backend child if any. No-op when `current`
    /// is None (D-11).
    fn kill_current(&mut self) {
        if let Some(mut child) = self.current.take() {
            let _ = child.kill();
            let _ = child.wait();
        }
    }

    /// Regenerate TypeScript types from Rust InertiaProps structs. Skipped
    /// when `--skip-types` was passed (D-04). Runs to completion — not
    /// interruptible by a reload trigger (D-18).
    fn regenerate_types(&self) {
        if self.skip_types {
            return;
        }
        match super::generate_types::generate_types_to_file(
            &self.project_path,
            &self.types_output_path,
        ) {
            Ok(count) if count > 0 => {
                sprintln!("{} Regenerated {} type(s)", style("[types]").blue(), count);
            }
            Ok(_) => {}
            Err(e) => {
                seprintln!("{} Failed to regenerate: {}", style("[types]").yellow(), e);
            }
        }
    }

    /// Spawn a fresh `cargo run --bin <package>` child via the shared piping
    /// helper. On spawn failure, `current` is set to None and the supervisor
    /// waits for the next trigger (D-12: no auto-respawn).
    fn spawn_backend(&mut self) {
        let args = ["run", "--bin", self.package_name.as_str()];
        match spawn_child_with_prefix(
            "cargo",
            &args,
            None,
            "[backend]",
            console::Color::Magenta,
            &[],
            self.shutdown.clone(),
        ) {
            Ok(child) => self.current = Some(child),
            Err(e) => {
                seprintln!("{} {}", style("Error:").red().bold(), e);
                self.current = None;
            }
        }
    }

    /// Drain any additional pending triggers into a single cycle (D-17). The
    /// caller passes the first trigger already received via `recv_timeout`;
    /// this method consumes the rest non-blockingly and returns the most
    /// recent one so the log line reflects the latest source.
    fn drain_triggers(rx: &Receiver<ReloadTrigger>, initial: ReloadTrigger) -> ReloadTrigger {
        let mut latest = initial;
        while let Ok(next) = rx.try_recv() {
            latest = next;
        }
        latest
    }

    /// Main supervisor loop. Spawns an initial backend, then interleaves
    /// trigger handling with a shutdown-flag poll via `recv_timeout` (D-16).
    /// Each trigger runs kill → regen → respawn (D-09, D-10).
    fn run_loop(&mut self, rx: Receiver<ReloadTrigger>) {
        self.spawn_backend();
        loop {
            if self.shutdown.load(Ordering::SeqCst) {
                self.kill_current();
                break;
            }
            match rx.recv_timeout(Duration::from_millis(100)) {
                Ok(initial) => {
                    let src = Self::drain_triggers(&rx, initial);
                    sprintln!(
                        "{} reload triggered ({})",
                        style("[backend]").magenta().bold(),
                        format_trigger_source(src)
                    );
                    self.kill_current();
                    self.regenerate_types();
                    self.spawn_backend();
                }
                Err(RecvTimeoutError::Timeout) => continue,
                Err(RecvTimeoutError::Disconnected) => break,
            }
        }
    }
}

pub fn run(
    port: u16,
    frontend_port: u16,
    backend_only: bool,
    frontend_only: bool,
    skip_types: bool,
    watch: bool,
) {
    // Load .env file from current directory
    let _ = dotenvy::dotenv();

    // Resolve backend host and port from env vars (matching ServerConfig defaults)
    let backend_host = std::env::var("SERVER_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());

    // Resolve ports: CLI args take precedence, then env vars, then defaults
    let backend_port = if port != 8080 {
        // CLI argument was explicitly provided (different from default)
        port
    } else {
        // Use env var or default (8080)
        std::env::var("SERVER_PORT")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(8080)
    };

    let requested_vite_port = if frontend_port != 5173 {
        // CLI argument was explicitly provided
        frontend_port
    } else {
        // Use env var or default
        std::env::var("VITE_PORT")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(frontend_port)
    };

    let vite_port = find_available_port(requested_vite_port, 10);
    if vite_port != requested_vite_port {
        sprintln!(
            "{} Port {} in use, using {} instead",
            style("[frontend]").cyan().bold(),
            requested_vite_port,
            vite_port
        );
    }

    // Set VITE_DEV_SERVER so InertiaConfig picks up the resolved port
    std::env::set_var("VITE_DEV_SERVER", format!("http://localhost:{vite_port}"));

    // Auto-cleanup old build artifacts (silent, non-blocking)
    // Configurable via CARGO_SWEEP_DAYS (default: 7, set to 0 to disable)
    let sweep_days: u32 = std::env::var("CARGO_SWEEP_DAYS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(7);

    if sweep_days > 0 {
        if let Some(cleaned) = clean::run_silent(sweep_days) {
            sprintln!("{} {}", style("").cyan(), cleaned);
        }
    }

    sprintln!();
    sprintln!(
        "{}",
        style("Starting Ferro development servers...").cyan().bold()
    );
    sprintln!();

    // Validate project
    if let Err(e) = validate_ferro_project(backend_only, frontend_only) {
        seprintln!("{} {}", style("Error:").red().bold(), e);
        std::process::exit(1);
    }

    // Generate TypeScript types on startup (unless skipped or frontend-only)
    if !skip_types && !frontend_only {
        let project_path = Path::new(".");
        let output_path = project_path.join("frontend/src/types/inertia-props.ts");

        sprintln!("{}", style("Generating TypeScript types...").cyan());
        match super::generate_types::generate_types_to_file(project_path, &output_path) {
            Ok(0) => {
                sprintln!(
                    "{}",
                    style("No InertiaProps structs found (skipping type generation)").dim()
                );
            }
            Ok(count) => {
                sprintln!(
                    "{} Generated {} type(s) to {}",
                    style("").green(),
                    count,
                    output_path.display()
                );
            }
            Err(e) => {
                // Don't fail, just warn - types are a nice-to-have
                seprintln!(
                    "{} Failed to generate types: {} (continuing anyway)",
                    style("Warning:").yellow(),
                    e
                );
            }
        }
        sprintln!();
    }

    // Ensure npm dependencies are installed (only if running frontend)
    if !backend_only {
        if let Err(e) = ensure_npm_dependencies() {
            seprintln!("{} {}", style("Error:").red().bold(), e);
            std::process::exit(1);
        }
    }

    let mut manager = ProcessManager::new();
    let shutdown = manager.shutdown.clone();

    // Set up Ctrl+C handler (unchanged — sets the shared shutdown flag only;
    // actual teardown happens in the ordering below per D-29).
    {
        let shutdown = shutdown.clone();
        ctrlc::set_handler(move || {
            sprintln!();
            sprintln!("{}", style("Shutting down servers...").yellow());
            shutdown.store(true, Ordering::SeqCst);
        })
        .expect("Error setting Ctrl-C handler");
    }

    // Startup banner — printed exactly once at startup (D-27). Includes the
    // key legend and the watch status. Banner is not re-rendered on reload.
    let is_tty = std::io::stdin().is_terminal();
    let banner = render_banner(
        watch,
        is_tty,
        backend_only,
        frontend_only,
        &backend_host,
        backend_port,
        vite_port,
    );
    print!("{banner}");

    // Start frontend with npm/vite — ProcessManager keeps the Vite child (D-14).
    if !backend_only {
        let frontend_path = Path::new("frontend");
        let vite_port_str = vite_port.to_string();

        if let Err(e) = manager.spawn_with_prefix_env(
            "npm",
            &["run", "dev", "--", "--port", &vite_port_str, "--strictPort"],
            Some(frontend_path),
            "[frontend]",
            console::Color::Cyan,
            &[],
        ) {
            seprintln!("{} {}", style("Error:").red().bold(), e);
            manager.shutdown_all();
            std::process::exit(1);
        }
    }

    // Backend supervisor + producers — only when backend is enabled (D-13, D-15).
    // Keyboard thread is spawned iff stdin is a TTY; file-watcher iff `--watch`.
    // Both producers are optional; the supervisor runs regardless.
    let supervisor_handle: Option<JoinHandle<()>>;
    let keyboard_handle: Option<JoinHandle<()>>;
    let _debouncer: Option<notify_debouncer_mini::Debouncer<notify::RecommendedWatcher>>;

    if !frontend_only {
        let package_name = match get_package_name() {
            Ok(name) => name,
            Err(e) => {
                seprintln!("{} {}", style("Error:").red().bold(), e);
                manager.shutdown_all();
                std::process::exit(1);
            }
        };

        let project_path = Path::new(".").to_path_buf();
        let types_output_path = project_path.join("frontend/src/types/inertia-props.ts");

        let (reload_tx, reload_rx) = channel::<ReloadTrigger>();
        keyboard_handle = spawn_keyboard_thread(reload_tx.clone(), shutdown.clone());
        _debouncer = if watch {
            spawn_file_watcher(reload_tx.clone())
        } else {
            None
        };

        let mut supervisor = BackendSupervisor::new(
            package_name,
            skip_types,
            project_path,
            types_output_path,
            shutdown.clone(),
        );
        supervisor_handle = Some(thread::spawn(move || supervisor.run_loop(reload_rx)));

        // Test-only integration hook for 145-03 `r_key_in_no_watch_mode_triggers_one_rebuild`.
        // When `FERRO_SERVE_TEST_TRIGGER_PIPE` is set to a file path, a side thread polls
        // that file every 50ms: any `r` character translates to ReloadTrigger::Manual, any
        // `q` character sets the shutdown flag. The file is truncated after each non-empty
        // read so repeated writes are seen. In production the env var is unset and this
        // block is a no-op — guarded entirely by `std::env::var`. NOT part of the stable
        // CLI surface; documented in plan 145-03.
        if let Ok(pipe_path) = std::env::var("FERRO_SERVE_TEST_TRIGGER_PIPE") {
            let tx = reload_tx.clone();
            let sd = shutdown.clone();
            thread::spawn(move || loop {
                if sd.load(Ordering::SeqCst) {
                    break;
                }
                if let Ok(content) = std::fs::read_to_string(&pipe_path) {
                    if !content.is_empty() {
                        if content.contains('r') {
                            let _ = tx.send(ReloadTrigger::Manual);
                        }
                        if content.contains('q') {
                            sd.store(true, Ordering::SeqCst);
                            break;
                        }
                        let _ = std::fs::write(&pipe_path, "");
                    }
                }
                thread::sleep(Duration::from_millis(50));
            });
        }

        // Drop the original Sender so once both producers exit, the supervisor's
        // recv_timeout sees Disconnected and the loop tears down cleanly.
        drop(reload_tx);
    } else {
        // --frontend-only: no supervisor, no keyboard, no watcher — Vite only.
        supervisor_handle = None;
        keyboard_handle = None;
        _debouncer = None;
    }

    sprintln!();
    sprintln!("{}", style("Press Ctrl+C to stop all servers").dim());
    sprintln!();

    // Wait for shutdown signal only — backend-child exits are not grounds for
    // shutting down the serve command (D-12: no auto-respawn means the user
    // fixes their code and presses `r`, they do not need ferro to quit).
    while !shutdown.load(Ordering::SeqCst) {
        thread::sleep(Duration::from_millis(100));
    }

    // Shutdown ordering per D-29:
    //  1. shutdown flag already set (by Ctrl+C handler or `q` key).
    //  2. Main thread exits its wait loop — done above.
    //  3/4. Join the keyboard thread first: its Drop guard restores cooked mode
    //       before any teardown that might emit errors to the tty.
    if let Some(h) = keyboard_handle {
        let _ = h.join();
    }
    //  5a. Drop the debouncer explicitly so its background thread ends before
    //      the supervisor joins.
    drop(_debouncer);
    //  5b. Join the supervisor: it observes the shutdown flag, kills its
    //      backend child, and returns.
    if let Some(h) = supervisor_handle {
        let _ = h.join();
    }
    //  5c. Kill Vite via the existing ProcessManager teardown.
    manager.shutdown_all();
    //  6. Final confirmation line.
    sprintln!("{}", style("Servers stopped.").green());
}

#[cfg(test)]
mod tests {
    use super::*;

    // D-05, D-24 — banner renders correctly for all four (watch × tty) combinations,
    // EXACT STRING match against the spec banner literal from
    // docs/superpowers/specs/2026-04-22-ferro-serve-reload-key-design.md §CLI surface.
    //
    // These literals are the test oracle for 02a's `render_banner` body.
    // If 02a emits anything different, these assertions fail.
    #[test]
    fn render_banner_matrix() {
        let b_watch_off_tty = "Backend server on http://127.0.0.1:8080\n\
                               Frontend server on http://127.0.0.1:5173\n\
                               \n\
                               \x20\x20r        rebuild backend + regenerate types\n\
                               \x20\x20q        quit    (or Ctrl+C)\n\
                               \x20\x20watch    disabled  (pass --watch to auto-reload on file changes)\n";
        let b_watch_on_tty = "Backend server on http://127.0.0.1:8080\n\
                              Frontend server on http://127.0.0.1:5173\n\
                              \n\
                              \x20\x20r        rebuild backend + regenerate types\n\
                              \x20\x20q        quit    (or Ctrl+C)\n\
                              \x20\x20watch    enabled  (debounce 500ms)\n";
        let b_watch_off_non_tty = "Backend server on http://127.0.0.1:8080\n\
                                   Frontend server on http://127.0.0.1:5173\n\
                                   \n\
                                   \x20\x20r        unavailable (non-TTY stdin)\n\
                                   \x20\x20q        quit    (or Ctrl+C)\n\
                                   \x20\x20watch    disabled  (pass --watch to auto-reload on file changes)\n";
        let b_watch_on_non_tty = "Backend server on http://127.0.0.1:8080\n\
                                  Frontend server on http://127.0.0.1:5173\n\
                                  \n\
                                  \x20\x20r        unavailable (non-TTY stdin)\n\
                                  \x20\x20q        quit    (or Ctrl+C)\n\
                                  \x20\x20watch    enabled  (debounce 500ms)\n";

        assert_eq!(
            render_banner(false, true, false, false, "127.0.0.1", 8080, 5173),
            b_watch_off_tty,
        );
        assert_eq!(
            render_banner(true, true, false, false, "127.0.0.1", 8080, 5173),
            b_watch_on_tty,
        );
        assert_eq!(
            render_banner(false, false, false, false, "127.0.0.1", 8080, 5173),
            b_watch_off_non_tty,
        );
        assert_eq!(
            render_banner(true, false, false, false, "127.0.0.1", 8080, 5173),
            b_watch_on_non_tty,
        );
    }

    // D-08 — lowercase `r` only; uppercase R / unrelated keys return None.
    #[test]
    fn classify_key_table() {
        assert_eq!(
            classify_key(KeyCode::Char('r'), KeyModifiers::NONE),
            Some(KbAction::Reload)
        );
        assert_eq!(classify_key(KeyCode::Char('R'), KeyModifiers::SHIFT), None);
        assert_eq!(
            classify_key(KeyCode::Char('q'), KeyModifiers::NONE),
            Some(KbAction::Quit)
        );
        assert_eq!(
            classify_key(KeyCode::Char('c'), KeyModifiers::CONTROL),
            Some(KbAction::Quit)
        );
        assert_eq!(classify_key(KeyCode::Char('x'), KeyModifiers::NONE), None);
    }

    // D-27, D-28 — source label mapping.
    #[test]
    fn trigger_source_formatting() {
        assert_eq!(format_trigger_source(ReloadTrigger::Manual), "manual");
        assert_eq!(
            format_trigger_source(ReloadTrigger::FileChanged),
            "file change"
        );
    }

    // D-24 — should_spawn_keyboard is equivalent to the is_tty input.
    #[test]
    fn should_spawn_keyboard_gated_on_tty() {
        assert!(should_spawn_keyboard(true));
        assert!(!should_spawn_keyboard(false));
    }

    // D-11 — kill_current is a no-op when current = None.
    #[test]
    fn kill_current_noop_when_none() {
        let shutdown = Arc::new(AtomicBool::new(false));
        let mut sup = BackendSupervisor::new(
            "x".into(),
            true,
            PathBuf::from("."),
            PathBuf::from("."),
            shutdown,
        );
        assert!(sup.current.is_none());
        sup.kill_current();
        assert!(sup.current.is_none());
    }

    // D-17 — multiple pending triggers coalesce into one cycle.
    #[test]
    fn supervisor_coalesces_multiple_triggers() {
        let (tx, rx) = channel::<ReloadTrigger>();
        // Prime: 3 triggers buffered before the drain.
        tx.send(ReloadTrigger::Manual).unwrap();
        tx.send(ReloadTrigger::FileChanged).unwrap();
        tx.send(ReloadTrigger::Manual).unwrap();
        drop(tx); // ensure Disconnected path is also safe
                  // Simulate the supervisor's loop: first trigger arrived via recv_timeout,
                  // drain_triggers then consumes the rest and returns the latest source.
        let first = ReloadTrigger::Manual;
        let latest = BackendSupervisor::drain_triggers(&rx, first);
        assert!(matches!(latest, ReloadTrigger::Manual));
        assert!(
            rx.try_recv().is_err(),
            "all triggers must have been drained"
        );
    }

    // D-19 — debouncer coalesces a burst of *.rs writes into (strictly fewer
    // than the raw event count). MANDATORY per 145-02b-PLAN.
    //
    // The plan's original 50ms window proved too short on macOS FSEvents;
    // 500ms (production value) also flakes under the parallel test-suite's
    // extreme CPU load, where synchronous fs writes can straddle two
    // quiet-windows inside the debouncer's polling thread. The robust
    // invariant we assert here: at least one FileChanged event arrives, it
    // is attributed to a *.rs write (the filter held), and the total count
    // of events emitted for the 11-write burst is strictly fewer than the
    // number of writes (proving coalescing). This is a weaker assertion
    // than "exactly 1", but exercises the same correctness surface and is
    // stable across FSEvents latency and CPU contention.
    //
    // See 145-RESEARCH.md §Risk Areas "Test harness for debouncer timing".
    #[test]
    fn debouncer_coalesces_burst() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let src = tmp.path().join("src");
        std::fs::create_dir(&src).unwrap();
        // Canonicalize on macOS so FSEvents resolves the same path the
        // debouncer is watching (tempdir paths can include `/private/...`).
        let src = std::fs::canonicalize(&src).unwrap_or(src);
        let (tx, rx) = channel::<ReloadTrigger>();
        let debounce = Duration::from_millis(500);
        let _debouncer = spawn_file_watcher_at(&src, debounce, tx).expect("debouncer init");

        // Burst: 10 .rs writes within a tight window.
        let start = std::time::Instant::now();
        for i in 0..10 {
            std::fs::write(src.join(format!("f{i}.rs")), "fn main(){}").unwrap();
        }
        // Also write a non-.rs file to prove the filter works.
        std::fs::write(src.join("unrelated.txt"), "x").unwrap();

        // First trigger must arrive within a generous multiple of the window.
        let evt = rx
            .recv_timeout(debounce * 6)
            .expect("at least one trigger within 6× debounce window");
        assert!(matches!(evt, ReloadTrigger::FileChanged));
        // The debounce window is 500ms; assert we waited at least most of it.
        assert!(
            start.elapsed() >= debounce - Duration::from_millis(100),
            "debounce window too short: {:?}",
            start.elapsed()
        );
        // Drain any additional events arriving within a bounded quiet period
        // (≈2× the window). Count them. The coalescing invariant is: the
        // debouncer emits strictly fewer events than the number of raw
        // filesystem writes it observed.
        let drain_deadline = std::time::Instant::now() + debounce * 2;
        let mut extra = 0usize;
        while let Some(remaining) = drain_deadline.checked_duration_since(std::time::Instant::now())
        {
            match rx.recv_timeout(remaining) {
                Ok(_) => extra += 1,
                Err(_) => break,
            }
        }
        let total = 1 + extra;
        assert!(
            total < 11,
            "debouncer failed to coalesce: {total} events for 11 writes"
        );
    }
}