Skip to main content

chub_cli/commands/
init.rs

1use clap::Args;
2use owo_colors::OwoColorize;
3
4use crate::output;
5
6#[derive(Args)]
7pub struct InitArgs {
8    /// Auto-detect dependencies from package.json/Cargo.toml/etc.
9    #[arg(long)]
10    from_deps: bool,
11
12    /// Scaffold for monorepo (root + per-package .chub/ dirs)
13    #[arg(long)]
14    monorepo: bool,
15}
16
17pub fn run(args: InitArgs, json: bool) {
18    match chub_core::team::project::init_project(args.from_deps, args.monorepo) {
19        Ok(chub_dir) => {
20            if json {
21                println!(
22                    "{}",
23                    serde_json::json!({
24                        "status": "created",
25                        "path": chub_dir.display().to_string(),
26                    })
27                );
28            } else {
29                output::success(&format!("Created .chub/ at {}", chub_dir.display()));
30                eprintln!("  {}", "config.yaml".dimmed());
31                eprintln!("  {}", "pins.yaml".dimmed());
32                eprintln!("  {}", "profiles/base.yaml".dimmed());
33                eprintln!("  {}", "context/architecture.md".dimmed());
34                eprintln!("  {}", "annotations/".dimmed());
35                eprintln!();
36                eprintln!("Next steps:");
37                eprintln!(
38                    "  {} — pin docs for your project",
39                    "chub pin openai/chat --lang python".bold()
40                );
41                eprintln!(
42                    "  {} — auto-detect from dependencies",
43                    "chub detect --pin".bold()
44                );
45                eprintln!("  {} — create context profiles", "chub profile list".bold());
46            }
47
48            // If --from-deps, run detect
49            if args.from_deps {
50                eprintln!();
51                run_detect_after_init(json);
52            }
53        }
54        Err(e) => {
55            output::error(&e.to_string(), json);
56            std::process::exit(1);
57        }
58    }
59}
60
61fn run_detect_after_init(json: bool) {
62    let cwd = std::env::current_dir().unwrap_or_default();
63    let deps = chub_core::team::detect::detect_dependencies(&cwd);
64
65    if deps.is_empty() {
66        if !json {
67            eprintln!("{}", "No dependency files found.".dimmed());
68        }
69        return;
70    }
71
72    if !json {
73        eprintln!("Detected {} dependencies from project files:", deps.len());
74        for dep in &deps {
75            let ver = dep
76                .version
77                .as_deref()
78                .map(|v| format!(" ({})", v))
79                .unwrap_or_default();
80            eprintln!(
81                "  {} {}{}",
82                dep.name.bold(),
83                dep.language.dimmed(),
84                ver.dimmed()
85            );
86        }
87        eprintln!();
88        eprintln!(
89            "Run {} to auto-pin detected docs.",
90            "chub detect --pin".bold()
91        );
92    }
93
94    // Clean up the marker file
95    if let Some(chub_dir) = chub_core::team::project::project_chub_dir() {
96        let _ = std::fs::remove_file(chub_dir.join(".init_from_deps"));
97    }
98}