vika_cli/commands/
templates.rs1use crate::error::{FileSystemError, Result};
2use crate::templates::engine::TemplateEngine;
3use crate::templates::loader::TemplateLoader;
4use colored::*;
5use std::fs;
6
7pub fn list() -> Result<()> {
9 let project_root = std::env::current_dir().ok();
10 let engine = TemplateEngine::new(project_root.as_deref())?;
11
12 let templates = engine.list_templates()?;
13
14 println!("{}", "Built-in templates:".bright_cyan());
15 println!();
16
17 let mut builtin_list = Vec::new();
18 let mut user_list = Vec::new();
19
20 for (name, overridden) in templates {
21 if overridden {
22 user_list.push(name.clone());
23 println!(" {} {} (overridden)", "✓".green(), name.bright_green());
24 } else {
25 builtin_list.push(name.clone());
26 println!(" - {}", name);
27 }
28 }
29
30 if !user_list.is_empty() {
31 println!();
32 println!("{}", "User overrides:".bright_yellow());
33 for name in user_list {
34 println!(" - {}", name.bright_green());
35 }
36 }
37
38 Ok(())
39}
40
41pub fn init() -> Result<()> {
43 let project_root = std::env::current_dir().map_err(|e| FileSystemError::ReadFileFailed {
44 path: ".".to_string(),
45 source: e,
46 })?;
47
48 let templates_dir = project_root.join(".vika").join("templates");
49
50 if let Some(parent) = templates_dir.parent() {
52 fs::create_dir_all(parent).map_err(|e| FileSystemError::CreateDirectoryFailed {
53 path: parent.to_string_lossy().to_string(),
54 source: e,
55 })?;
56 }
57
58 fs::create_dir_all(&templates_dir).map_err(|e| FileSystemError::CreateDirectoryFailed {
60 path: templates_dir.to_string_lossy().to_string(),
61 source: e,
62 })?;
63
64 let builtin_templates = TemplateLoader::list_builtin();
66
67 let mut copied = 0;
68 let mut skipped = 0;
69
70 for template_name in builtin_templates {
71 let template_path = templates_dir.join(format!("{}.tera", template_name));
72
73 if template_path.exists() {
75 println!(" {} {} (already exists)", "⊘".yellow(), template_name);
76 skipped += 1;
77 continue;
78 }
79
80 let content = TemplateLoader::load_builtin(&template_name)?;
82 fs::write(&template_path, content).map_err(|e| FileSystemError::WriteFileFailed {
83 path: template_path.to_string_lossy().to_string(),
84 source: e,
85 })?;
86
87 println!(" {} {}", "✓".green(), template_name.bright_green());
88 copied += 1;
89 }
90
91 println!();
92 println!(
93 "{}",
94 format!(
95 "✅ Initialized templates directory: {}",
96 templates_dir.display()
97 )
98 .green()
99 );
100 println!(
101 "{}",
102 format!(" Copied: {}, Skipped: {}", copied, skipped).bright_black()
103 );
104
105 Ok(())
106}