Skip to main content

ferro_cli/commands/
make_theme.rs

1//! make:theme command — scaffold theme files for the Ferro theme system
2//!
3//! Scaffolds plain CSS variable declarations (`:root { ... }`) compatible
4//! with ferro-json-ui's `<style>` injection path.
5
6use console::style;
7use std::fs;
8use std::path::Path;
9
10/// Generate theme scaffold files in `themes/{name}/`.
11///
12/// Creates:
13/// - `themes/{name}/tokens.css` — plain CSS `:root { ... }` block with all 30 semantic token slots
14/// - `themes/{name}/theme.json` — empty JSON object for partial intent template overrides
15///
16/// Returns an error if `themes/{name}/` already exists.
17pub fn make_theme(name: &str) -> Result<(), Box<dyn std::error::Error>> {
18    make_theme_in_dir(name, Path::new("."))
19}
20
21/// Core logic — generate in a given base directory (enables testability).
22pub fn make_theme_in_dir(name: &str, base: &Path) -> Result<(), Box<dyn std::error::Error>> {
23    let theme_dir = base.join("themes").join(name);
24
25    if theme_dir.exists() {
26        return Err(format!("Theme directory '{}' already exists", theme_dir.display()).into());
27    }
28
29    fs::create_dir_all(&theme_dir)?;
30
31    // Write tokens.css — plain CSS variables, injectable into <style> without Tailwind processing
32    let tokens_path = theme_dir.join("tokens.css");
33    fs::write(&tokens_path, tokens_css_template())?;
34
35    // Write theme.json — empty object for partial intent template overrides
36    let theme_json_path = theme_dir.join("theme.json");
37    fs::write(&theme_json_path, "{}\n")?;
38
39    println!(
40        "{} Created theme '{}' at {}/",
41        style("✓").green(),
42        style(name).cyan().bold(),
43        theme_dir.display()
44    );
45    println!();
46    println!("Next steps:");
47    println!(
48        "  {} Edit {}/tokens.css to customize colors, shapes, and typography",
49        style("1.").dim(),
50        theme_dir.display()
51    );
52    println!(
53        "  {} Add intent template overrides to {}/theme.json",
54        style("2.").dim(),
55        theme_dir.display()
56    );
57    println!(
58        "  {} Activate the theme via ThemeMiddleware in your application",
59        style("3.").dim()
60    );
61    println!();
62
63    Ok(())
64}
65
66/// Public entry point called from main.rs.
67pub fn run(name: &str) {
68    if let Err(e) = make_theme(name) {
69        eprintln!("{} {}", style("Error:").red().bold(), e);
70        std::process::exit(1);
71    }
72}
73
74fn tokens_css_template() -> &'static str {
75    r#"/* Theme tokens — plain CSS variables.
76 *
77 * Injected into <style> by ferro-json-ui at render time.
78 * MUST use standard CSS (:root { ... }) — not Tailwind's @theme syntax,
79 * which only works under the Tailwind browser runtime (dev-only).
80 */
81
82:root {
83  /* Surface tokens */
84  --color-background: oklch(99% 0.004 250);
85  --color-surface: oklch(97% 0.006 250);
86  --color-card: oklch(95% 0.008 250);
87  --color-border: oklch(90% 0.012 250);
88  --color-text: oklch(20% 0.02 250);
89  --color-text-muted: oklch(50% 0.016 250);
90
91  /* Role tokens */
92  --color-primary: oklch(55% 0.2 250);
93  --color-primary-foreground: oklch(100% 0 0);
94  --color-secondary: oklch(70% 0.05 250);
95  --color-secondary-foreground: oklch(15% 0 0);
96  --color-accent: oklch(70% 0.13 250);
97  --color-destructive: oklch(55% 0.22 25);
98  --color-success: oklch(55% 0.18 145);
99  --color-warning: oklch(70% 0.18 80);
100
101  /* Shape tokens */
102  --radius-sm: 0.25rem;
103  --radius-md: 0.375rem;
104  --radius-lg: 0.5rem;
105  --radius-full: 9999px;
106
107  /* Shadow tokens */
108  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
109  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
110  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
111
112  /* Typography tokens */
113  --font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
114  --font-mono: ui-monospace, monospace;
115
116  /* Density token */
117  --spacing: 0.25rem;
118
119  /* Motion tokens */
120  --motion-duration-fast: 120ms;
121  --motion-duration-base: 220ms;
122  --motion-duration-slow: 320ms;
123  --motion-ease: cubic-bezier(0.2, 0, 0.38, 0.9);
124
125  /* Focus ring token */
126  --color-ring: oklch(55% 0.2 250);
127
128  /* Display font token */
129  --font-display: var(--font-sans);
130}
131
132@media (prefers-color-scheme: dark) {
133  :root {
134    --color-background: oklch(15% 0.014 250);
135    --color-surface: oklch(19% 0.016 250);
136    --color-card: oklch(22% 0.018 250);
137    --color-border: oklch(31% 0.02 250);
138    --color-text: oklch(95% 0.006 250);
139    --color-text-muted: oklch(63% 0.016 250);
140    --color-primary: oklch(56% 0.2 250);
141    --color-primary-foreground: oklch(100% 0 0);
142    --color-secondary: oklch(53% 0.05 250);
143    --color-secondary-foreground: oklch(95% 0 0);
144    --color-accent: oklch(68% 0.13 250);
145    --color-destructive: oklch(59% 0.22 25);
146    --color-success: oklch(60% 0.18 145);
147    --color-warning: oklch(65% 0.18 80);
148
149    /* Density token */
150    --spacing: 0.25rem;
151
152    /* Motion tokens */
153    --motion-duration-fast: 120ms;
154    --motion-duration-base: 220ms;
155    --motion-duration-slow: 320ms;
156    --motion-ease: cubic-bezier(0.2, 0, 0.38, 0.9);
157
158    /* Focus ring token */
159    --color-ring: oklch(65% 0.18 250);
160
161    /* Display font token */
162    --font-display: var(--font-sans);
163  }
164}
165"#
166}
167
168#[cfg(test)]
169mod tests {
170    use super::*;
171    use tempfile::TempDir;
172
173    fn read_file(path: &Path) -> String {
174        fs::read_to_string(path).unwrap_or_else(|e| panic!("Failed to read {path:?}: {e}"))
175    }
176
177    #[test]
178    fn test_make_theme_creates_directory_structure() {
179        let tmp = TempDir::new().unwrap();
180        make_theme_in_dir("myapp", tmp.path()).unwrap();
181
182        assert!(tmp.path().join("themes/myapp").exists());
183        assert!(tmp.path().join("themes/myapp/tokens.css").exists());
184        assert!(tmp.path().join("themes/myapp/theme.json").exists());
185    }
186
187    #[test]
188    fn test_make_theme_tokens_css_has_all_30_token_slots() {
189        let tmp = TempDir::new().unwrap();
190        make_theme_in_dir("test", tmp.path()).unwrap();
191
192        let css = read_file(&tmp.path().join("themes/test/tokens.css"));
193
194        // Surface tokens (6)
195        assert!(
196            css.contains("--color-background:"),
197            "missing --color-background"
198        );
199        assert!(css.contains("--color-surface:"), "missing --color-surface");
200        assert!(css.contains("--color-card:"), "missing --color-card");
201        assert!(css.contains("--color-border:"), "missing --color-border");
202        assert!(css.contains("--color-text:"), "missing --color-text");
203        assert!(
204            css.contains("--color-text-muted:"),
205            "missing --color-text-muted"
206        );
207
208        // Role tokens (8)
209        assert!(css.contains("--color-primary:"), "missing --color-primary");
210        assert!(
211            css.contains("--color-primary-foreground:"),
212            "missing --color-primary-foreground"
213        );
214        assert!(
215            css.contains("--color-secondary:"),
216            "missing --color-secondary"
217        );
218        assert!(
219            css.contains("--color-secondary-foreground:"),
220            "missing --color-secondary-foreground"
221        );
222        assert!(css.contains("--color-accent:"), "missing --color-accent");
223        assert!(
224            css.contains("--color-destructive:"),
225            "missing --color-destructive"
226        );
227        assert!(css.contains("--color-success:"), "missing --color-success");
228        assert!(css.contains("--color-warning:"), "missing --color-warning");
229
230        // Shape tokens (4)
231        assert!(css.contains("--radius-sm:"), "missing --radius-sm");
232        assert!(css.contains("--radius-md:"), "missing --radius-md");
233        assert!(css.contains("--radius-lg:"), "missing --radius-lg");
234        assert!(css.contains("--radius-full:"), "missing --radius-full");
235
236        // Shadow tokens (3)
237        assert!(css.contains("--shadow-sm:"), "missing --shadow-sm");
238        assert!(css.contains("--shadow-md:"), "missing --shadow-md");
239        assert!(css.contains("--shadow-lg:"), "missing --shadow-lg");
240
241        // Typography tokens (2)
242        assert!(css.contains("--font-sans:"), "missing --font-sans");
243        assert!(css.contains("--font-mono:"), "missing --font-mono");
244
245        // Density token (1)
246        assert!(css.contains("--spacing:"), "missing --spacing");
247
248        // Motion tokens (4)
249        assert!(
250            css.contains("--motion-duration-fast:"),
251            "missing --motion-duration-fast"
252        );
253        assert!(
254            css.contains("--motion-duration-base:"),
255            "missing --motion-duration-base"
256        );
257        assert!(
258            css.contains("--motion-duration-slow:"),
259            "missing --motion-duration-slow"
260        );
261        assert!(css.contains("--motion-ease:"), "missing --motion-ease");
262
263        // Focus ring token (1)
264        assert!(css.contains("--color-ring:"), "missing --color-ring");
265
266        // Display font token (1)
267        assert!(css.contains("--font-display:"), "missing --font-display");
268    }
269
270    #[test]
271    fn test_make_theme_tokens_css_has_root_block_and_no_tailwind_syntax() {
272        let tmp = TempDir::new().unwrap();
273        make_theme_in_dir("test", tmp.path()).unwrap();
274
275        let css = read_file(&tmp.path().join("themes/test/tokens.css"));
276
277        // Must NOT contain Tailwind-CDN-only syntax.
278        assert!(
279            !css.contains("@import \"tailwindcss\""),
280            "scaffolded tokens.css must not contain @import \"tailwindcss\" — it is Tailwind-CDN-specific"
281        );
282        assert!(
283            !css.contains("@theme {"),
284            "scaffolded tokens.css must not contain @theme {{...}} — it is Tailwind-CDN-specific"
285        );
286
287        // Must contain plain CSS :root block with the primary token.
288        assert!(css.contains(":root {"), "missing :root {{...}} block");
289        assert!(
290            css.contains("--color-primary:"),
291            "missing --color-primary declaration"
292        );
293    }
294
295    #[test]
296    fn test_make_theme_tokens_css_has_dark_mode_block() {
297        let tmp = TempDir::new().unwrap();
298        make_theme_in_dir("test", tmp.path()).unwrap();
299
300        let css = read_file(&tmp.path().join("themes/test/tokens.css"));
301
302        assert!(
303            css.contains("@media (prefers-color-scheme: dark)"),
304            "missing dark mode @media"
305        );
306        assert!(
307            css.contains("oklch(15% 0.014 250)"),
308            "missing dark mode background value"
309        );
310        // Dark mode block must use :root, not @theme { ... }.
311        assert!(
312            !css.contains("@theme {"),
313            "dark-mode block must use :root {{...}}, not @theme {{...}}"
314        );
315    }
316
317    #[test]
318    fn test_make_theme_theme_json_is_empty_object() {
319        let tmp = TempDir::new().unwrap();
320        make_theme_in_dir("test", tmp.path()).unwrap();
321
322        let json_content = read_file(&tmp.path().join("themes/test/theme.json"));
323        let trimmed = json_content.trim();
324
325        // Must be valid JSON that deserializes to an empty object
326        let parsed: serde_json::Value =
327            serde_json::from_str(trimmed).expect("theme.json must be valid JSON");
328        assert_eq!(
329            parsed,
330            serde_json::json!({}),
331            "theme.json must be empty object"
332        );
333    }
334
335    #[test]
336    fn test_make_theme_fails_if_directory_exists() {
337        let tmp = TempDir::new().unwrap();
338
339        // Create the directory first
340        fs::create_dir_all(tmp.path().join("themes/duplicate")).unwrap();
341
342        let result = make_theme_in_dir("duplicate", tmp.path());
343        assert!(
344            result.is_err(),
345            "should return error for duplicate theme name"
346        );
347        let err_msg = result.unwrap_err().to_string();
348        assert!(
349            err_msg.contains("already exists"),
350            "error should mention 'already exists'"
351        );
352    }
353
354    #[test]
355    fn test_make_theme_succeeds_once_fails_on_repeat() {
356        let tmp = TempDir::new().unwrap();
357
358        // First call succeeds
359        make_theme_in_dir("myapp", tmp.path()).unwrap();
360
361        // Second call with same name fails
362        let result = make_theme_in_dir("myapp", tmp.path());
363        assert!(result.is_err());
364    }
365}