Skip to main content

kayro_cli/
lib.rs

1mod args;
2mod build;
3mod cargo_command;
4mod cargo_progress;
5mod dev;
6mod error;
7mod output;
8mod packaging;
9mod process;
10mod project;
11mod scaffold;
12mod target;
13mod vite_path;
14
15use std::{env, ffi::OsStr, process::ExitCode};
16
17use clap::{Parser as _, error::ErrorKind};
18
19use crate::{
20    args::{Cli, KayroCommand, ProjectOptions},
21    output::Reporter,
22    process::Cancellation,
23    project::Project,
24};
25
26pub fn entry() -> ExitCode {
27    let arguments = env::args_os().collect::<Vec<_>>();
28    let json_requested = arguments
29        .iter()
30        .any(|argument| argument == OsStr::new("--json"));
31    let cli = match Cli::try_parse_from(arguments) {
32        Ok(cli) => cli,
33        Err(error) if json_requested && error.kind() == ErrorKind::DisplayHelp => {
34            Reporter::clap_display("help", &error.to_string());
35            return ExitCode::SUCCESS;
36        }
37        Err(error) if json_requested && error.kind() == ErrorKind::DisplayVersion => {
38            Reporter::clap_display("version", error.to_string().trim());
39            return ExitCode::SUCCESS;
40        }
41        Err(error)
42            if matches!(
43                error.kind(),
44                ErrorKind::DisplayHelp | ErrorKind::DisplayVersion
45            ) =>
46        {
47            let _ = error.print();
48            return ExitCode::SUCCESS;
49        }
50        Err(error) if json_requested => {
51            Reporter::argument_error(&error.to_string());
52            return ExitCode::from(2);
53        }
54        Err(error) => {
55            let code = u8::try_from(error.exit_code()).unwrap_or(2);
56            let _ = error.print();
57            return ExitCode::from(code);
58        }
59    };
60
61    let reporter = Reporter::new(cli.json, cli.quiet, cli.verbose, cli.color);
62    reporter.begin(command_name(&cli.command), cli.dry_run);
63    let result = run(&cli, &reporter);
64    match result {
65        Ok(command) => {
66            reporter.completed(command, cli.dry_run);
67            ExitCode::SUCCESS
68        }
69        Err(error) => {
70            reporter.error(&error);
71            ExitCode::from(error.exit_code())
72        }
73    }
74}
75
76fn command_name(command: &KayroCommand) -> &'static str {
77    match command {
78        KayroCommand::New(_) => "new",
79        KayroCommand::Init(_) => "init",
80        KayroCommand::Dev(_) => "dev",
81        KayroCommand::Build(_) => "build",
82    }
83}
84
85fn run<'a>(cli: &'a Cli, reporter: &Reporter) -> Result<&'a str, error::CliError> {
86    let cancellation = if cli.dry_run {
87        Cancellation::default()
88    } else {
89        Cancellation::install()?
90    };
91
92    match &cli.command {
93        KayroCommand::New(arguments) => {
94            scaffold::create_new(arguments, reporter, &cancellation, cli.dry_run)?;
95            Ok("new")
96        }
97        KayroCommand::Init(arguments) => {
98            scaffold::initialize(arguments, reporter, &cancellation, cli.dry_run)?;
99            Ok("init")
100        }
101        KayroCommand::Dev(arguments) => {
102            let project = discover_project(&arguments.project, reporter)?;
103            dev::run(&project, arguments, reporter, &cancellation, cli.dry_run)?;
104            Ok("dev")
105        }
106        KayroCommand::Build(arguments) => {
107            let project = discover_project(&arguments.project, reporter)?;
108            build::run(&project, arguments, reporter, &cancellation, cli.dry_run)?;
109            Ok("build")
110        }
111    }
112}
113
114fn discover_project(
115    options: &ProjectOptions,
116    reporter: &Reporter,
117) -> Result<Project, error::CliError> {
118    let project = Project::discover(options.config.as_deref(), options.project_dir.as_deref())?;
119    reporter.project(&project);
120    Ok(project)
121}