use std::path::PathBuf;
use anyhow::{bail, Result};
use clap::{Args, Subcommand};
use crate::{
capabilities::{
CapabilityOverrides, DatabaseChoice, DeployChoice, StarterChoice, StylingChoice, UiChoice,
},
scaffold::DryRunFormat,
site_scaffold,
};
#[derive(Debug, Args)]
pub(crate) struct NewArgs {
#[command(subcommand)]
command: NewCommand,
}
#[derive(Debug, Subcommand)]
enum NewCommand {
Site(NewSiteArgs),
}
#[derive(Debug, Args)]
struct NewSiteArgs {
path: PathBuf,
#[arg(long, default_value = "app")]
template: String,
#[arg(long, value_enum)]
database: Option<DatabaseChoice>,
#[arg(long, value_enum)]
ui: Option<UiChoice>,
#[arg(long, value_enum)]
styling: Option<StylingChoice>,
#[arg(long, value_enum)]
starter: Option<StarterChoice>,
#[arg(long, value_enum)]
deploy: Option<DeployChoice>,
#[arg(long)]
title: Option<String>,
#[arg(long)]
destination: Option<PathBuf>,
#[arg(long, conflicts_with = "no_vscode")]
vscode: bool,
#[arg(long)]
no_vscode: bool,
#[arg(long, conflicts_with = "no_agents_md")]
agents_md: bool,
#[arg(long)]
no_agents_md: bool,
#[arg(long)]
empty: bool,
#[arg(long, conflicts_with = "no_tailwind")]
tailwind: bool,
#[arg(long)]
no_tailwind: bool,
#[arg(long)]
no_git: bool,
#[arg(long)]
verify: bool,
#[arg(long)]
start: bool,
#[arg(long)]
open: bool,
#[arg(long)]
offline: bool,
#[arg(long)]
dry_run: bool,
#[arg(long)]
json: bool,
#[arg(long)]
yes: bool,
}
pub(crate) fn run(args: NewArgs) -> Result<()> {
match args.command {
NewCommand::Site(args) => run_site(args),
}
}
fn run_site(mut args: NewSiteArgs) -> Result<()> {
if args.tailwind && args.styling.is_some() {
bail!("--tailwind cannot be combined with --styling");
}
if args.no_tailwind && args.styling.is_some() {
bail!("--no-tailwind cannot be combined with --styling");
}
if args.empty && (args.template != "app" || args.starter.is_some()) {
bail!("--empty cannot be combined with an explicit template or starter");
}
if args.empty {
args.template = "minimal".to_string();
args.starter = Some(StarterChoice::Blank);
}
if args.tailwind {
args.styling = Some(StylingChoice::Tailwind);
}
if args.no_tailwind {
args.styling = Some(StylingChoice::Css);
}
let dry_run = args.dry_run.then_some(if args.json {
DryRunFormat::Json
} else {
DryRunFormat::Text
});
let _recommended_defaults = args.yes;
site_scaffold::new_site(site_scaffold::NewSiteOptions {
path: args.path,
destination: args.destination,
title: args.title,
template: args.template,
overrides: CapabilityOverrides {
database: args.database,
ui: args.ui,
styling: args.styling,
starter: args.starter,
deploy: args.deploy,
},
vscode: args.vscode || !args.no_vscode,
agents_md: args.agents_md || !args.no_agents_md,
no_git: args.no_git,
verify: args.verify,
start: args.start || args.open,
open: args.open,
offline: args.offline,
dry_run,
json: args.json,
})
}