mod new;
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)]
#[command(name = "rxferrix", version, about = "Creates RxFerrix apps")]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command {
New(NewArgs),
}
#[derive(clap::Args, Debug)]
struct NewArgs {
dir: PathBuf,
#[arg(long)]
name: Option<String>,
#[arg(long, value_enum, default_value_t = Web::React)]
web: Web,
#[arg(long, value_name = "DIR", conflicts_with = "published")]
rxferrix_path: Option<PathBuf>,
#[arg(long)]
published: bool,
}
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum Web {
React,
Svelte,
None,
}
fn main() -> ExitCode {
let cli = Cli::parse();
let result = match cli.command {
Command::New(args) => {
let source = if args.published {
new::Source::Published
} else if let Some(path) = args.rxferrix_path {
new::Source::Checkout(path)
} else {
new::Source::default_for_this_build()
};
new::run(&new::Options { dir: args.dir, name: args.name, web: args.web, source })
}
};
match result {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cli_definition_is_valid() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
#[test]
fn new_defaults_to_react() {
let cli = Cli::try_parse_from(["rxferrix", "new", "app"]).unwrap();
let Command::New(args) = cli.command;
assert_eq!(args.web, Web::React);
assert!(Cli::try_parse_from(["rxferrix", "new", "app", "--published", "--rxferrix-path", "x"]).is_err());
}
}