use crate::prelude::*;
use crate::theme;
#[derive(Debug, clap::Args)]
pub struct Theme {
name: String,
#[arg(short = 's', long)]
source: Option<PathBuf>,
#[arg(short = 'o', long)]
output: Option<PathBuf>,
#[arg(short = 'f', long)]
force: bool,
}
impl super::Command for Theme {
async fn execute(self) -> Result<()> {
let source = normalize(self.source, || "tokens.css".into())?;
let output = normalize(self.output, || self.name.to_case(Case::Kebab))?;
if output.try_exists()? {
if self.force {
fs::remove_file(&output)?;
} else {
bail!("file already exists: {}", output.display());
}
}
theme::create(source, output)?;
Ok(())
}
}
fn normalize(path: Option<PathBuf>, default_name: impl FnOnce() -> String) -> Result<PathBuf> {
let mut path = path.unwrap_or_else(|| PathBuf::from(default_name()));
if path.is_relative() {
let current_dir = env::current_dir()?;
path = current_dir.join(path);
}
Ok(path)
}