tcss_cli/commands/
init.rs

1//! Init command implementation
2
3use anyhow::{Context, Result};
4use colored::*;
5use std::fs;
6use std::path::Path;
7
8use crate::output;
9
10/// Initialize a new TCSS project
11pub fn init_project(name: &str, dir: Option<&Path>, verbose: bool) -> Result<()> {
12    let project_dir = dir.unwrap_or_else(|| Path::new(name));
13
14    output::header(&format!("Creating TCSS Project: {}", name));
15    output::debug_value("Project name", &name);
16    output::debug_value("Project directory", &project_dir);
17
18    // Create project directory
19    output::debug("Creating project directory...");
20    fs::create_dir_all(project_dir)
21        .with_context(|| format!("Failed to create directory: {}", project_dir.display()))?;
22
23    output::verbose(&format!("Created directory: {}", project_dir.display()));
24
25    // Create subdirectories
26    let src_dir = project_dir.join("src");
27    let dist_dir = project_dir.join("dist");
28
29    output::debug("Creating subdirectories...");
30    fs::create_dir_all(&src_dir)?;
31    fs::create_dir_all(&dist_dir)?;
32
33    output::verbose(&format!("Created: {}", src_dir.display()));
34    output::verbose(&format!("Created: {}", dist_dir.display()));
35
36    // Create example TCSS file
37    let example_tcss = r#"// TCSS Example - Design System
38// This file demonstrates TCSS features
39
40// Variables
41@var primary-color: #3498db
42@var secondary-color: #2ecc71
43@var danger-color: #e74c3c
44@var spacing-unit: 16px
45@var font-size-base: 14px
46@var border-radius: 4px
47
48// Functions
49@fn spacing(multiplier):
50    return multiplier * spacing-unit
51
52@fn font-size(level):
53    return font-size-base * level
54
55// Button Styles
56.button:
57    padding: spacing(1)
58    font-size: font-size(1)
59    border-radius: border-radius
60    border: 0px
61    cursor: pointer
62    transition: all 0.3s
63
64.button-primary:
65    background: primary-color
66    color: #fff
67
68.button-secondary:
69    background: secondary-color
70    color: #fff
71
72.button-danger:
73    background: danger-color
74    color: #fff
75
76// Container
77.container:
78    max-width: 1200px
79    margin: 0px
80    padding: spacing(2)
81
82// Card Component
83.card:
84    background: #fff
85    border-radius: border-radius
86    padding: spacing(2)
87    margin: spacing(1)
88    box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1)
89
90.card-header:
91    font-size: font-size(1.5)
92    margin-bottom: spacing(1)
93    font-weight: bold
94
95.card-body:
96    padding: spacing(1)
97    color: #333
98"#;
99
100    let main_tcss_path = src_dir.join("main.tcss");
101    fs::write(&main_tcss_path, example_tcss)?;
102
103    if verbose {
104        println!("  {} {}", "Created:".bright_black(), main_tcss_path.display());
105    }
106
107    // Create README
108    let readme = format!(
109        r#"# {}
110
111A TCSS project for modern CSS development.
112
113## Getting Started
114
115### Compile TCSS to CSS
116
117```bash
118tcss compile src/main.tcss -o dist/main.css
119```
120
121### Watch for changes
122
123```bash
124tcss watch src/ -o dist/
125```
126
127### Minify output
128
129```bash
130tcss compile src/main.tcss -o dist/main.min.css --minify
131```
132
133## Project Structure
134
135```
136{}/
137├── src/          # TCSS source files
138│   └── main.tcss
139├── dist/         # Compiled CSS output
140└── README.md
141```
142
143## Learn More
144
145- [TCSS Documentation](https://github.com/tosiiko/TCSS)
146- [TCSS Examples](https://github.com/tosiiko/TCSS/tree/main/examples)
147"#,
148        name, name
149    );
150
151    let readme_path = project_dir.join("README.md");
152    fs::write(&readme_path, readme)?;
153
154    if verbose {
155        println!("  {} {}", "Created:".bright_black(), readme_path.display());
156    }
157
158    // Create .gitignore
159    let gitignore = r#"# Compiled CSS
160dist/
161*.css
162*.css.map
163
164# OS files
165.DS_Store
166Thumbs.db
167
168# Editor files
169.vscode/
170.idea/
171*.swp
172*.swo
173"#;
174
175    let gitignore_path = project_dir.join(".gitignore");
176    fs::write(&gitignore_path, gitignore)?;
177
178    if verbose {
179        println!("  {} {}", "Created:".bright_black(), gitignore_path.display());
180    }
181
182    // Success message
183    println!();
184    println!("{} Project created successfully!", "✓".bright_green().bold());
185    println!();
186    println!("Next steps:");
187    println!("  {} {}", "cd".bright_white(), name);
188    println!("  {} {}", "tcss compile".bright_white(), "src/main.tcss -o dist/main.css");
189    println!("  {} {}", "tcss watch".bright_white(), "src/ -o dist/");
190
191    Ok(())
192}
193