crepuscularity-cli 0.12.0

crepus CLI — scaffolding and builds for Crepuscularity (UNSTABLE; in active development).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
//! `crepus moonshine` — scaffold and dependency helper for Moonshine + Crepus apps.
//!
//! Moonshine is an external product: <https://github.com/tschk/moonshine>
//! (local checkout often at `~/projects/moonshine`). Prefer the `moonshine` CLI
//! from that repo when available; `crepus moonshine` remains a working fallback.
//!
//! Crepuscularity compiles `.crepus` → View IR and emits apps that import
//! `@tschk/crepus-moonshine` (`crepus-emit.moonshine.tsx`).
//!
//! - `crepus moonshine new <name>` (alias `crepus moon new`) scaffolds a
//!   Rust-only app: crepus templates inlined in `src/main.rs`, built into a
//!   real Moonshine site via `crepus moon build`. Pass `--js` for the legacy
//!   React + Vite scaffold with a hardcoded View IR blob in `src/main.tsx`.
//! - `crepus moonshine build` (alias `crepus moon build`) builds a Rust-only
//!   scaffold end to end: `cargo run` emits the app, then package.json /
//!   index.html / vite.config.ts / tsconfig.json are refreshed and `bun
//!   install && bun run build` runs if `bun` is on PATH.
//! - `crepus moonshine dep` prints package.json dependency snippets.

use std::env;
use std::path::{Path, PathBuf};
use std::time::Instant;

use crate::cli::MoonshineCommands;
use crate::error::CrepusCliError;
use crate::scaffold;
use crate::ui;

/// Resolve a local tschk/moonshine checkout, if present.
///
/// Order: `MOONSHINE_PATH` → `../moonshine` relative to cwd → `~/projects/moonshine`.
fn resolve_moonshine_root() -> Option<PathBuf> {
    if let Ok(raw) = env::var("MOONSHINE_PATH") {
        let p = PathBuf::from(raw);
        if looks_like_moonshine_checkout(&p) {
            return Some(canonicalize_or_self(&p));
        }
    }

    let cwd_sibling = env::current_dir()
        .ok()
        .map(|cwd| cwd.join("..").join("moonshine"));
    if let Some(p) = cwd_sibling {
        if looks_like_moonshine_checkout(&p) {
            return Some(canonicalize_or_self(&p));
        }
    }

    if let Some(home) = env::var_os("HOME").or_else(|| env::var_os("USERPROFILE")) {
        let p = PathBuf::from(home).join("projects").join("moonshine");
        if looks_like_moonshine_checkout(&p) {
            return Some(canonicalize_or_self(&p));
        }
    }

    None
}

fn looks_like_moonshine_checkout(root: &Path) -> bool {
    root.join("packages/core/package.json").is_file()
        && root
            .join("packages/crepus-moonshine/package.json")
            .is_file()
        && root.join("components/package.json").is_file()
}

fn canonicalize_or_self(path: &Path) -> PathBuf {
    path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
}

/// `file:` dependency paths for the three @tschk packages.
///
/// Always uses absolute `file:` URLs so scaffolds outside the moonshine tree
/// (e.g. `/tmp/app`) don't get broken `../../Users/...` relatives.
fn file_dep_paths(moonshine_root: &Path, _from: Option<&Path>) -> (String, String, String) {
    let core = moonshine_root.join("packages/core");
    let crepus = moonshine_root.join("packages/crepus-moonshine");
    let components = moonshine_root.join("components");

    let fmt =
        |target: &Path| -> String { format!("file:{}", canonicalize_or_self(target).display()) };

    (fmt(&core), fmt(&crepus), fmt(&components))
}

fn package_json_template(core: &str, crepus: &str, components: &str) -> String {
    format!(
        r#"{{
  "name": "{{{{slug}}}}",
  "private": true,
  "version": "0.1.0",
  "type": "module",
  "scripts": {{
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  }},
  "dependencies": {{
    "@tschk/moonshine": "{core}",
    "@tschk/crepus-moonshine": "{crepus}",
    "@tschk/moonshine-components": "{components}",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  }},
  "devDependencies": {{
    "@types/react": "^19.0.0",
    "@types/react-dom": "^19.0.0",
    "@vitejs/plugin-react": "^4.5.0",
    "typescript": "^5.8.0",
    "vite": "^6.0.0"
  }}
}}
"#
    )
}

// Used when no local moonshine checkout is found, which is the normal case
// outside this developer's machine. A `file:` path here would resolve against a
// sibling directory the user does not have, so `bun install` would fail on a
// freshly scaffolded project; the published packages are the working default.
const PLACEHOLDER_CORE: &str = "^0.3.4";
const PLACEHOLDER_CREPUS: &str = "^0.3.4";
const PLACEHOLDER_COMPONENTS: &str = "^0.3.4";

const INDEX_HTML: &str = r#"<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{{name}}</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>
"#;

const MAIN_TSX: &str = r##"import { createApp } from "@tschk/moonshine/react";
import { renderCrepusIr } from "@tschk/crepus-moonshine";
import type { ViewIr } from "@tschk/crepus-moonshine";
import { Sparkline } from "@tschk/moonshine-components";
import "./app.css";

const sampleIr = {
  version: 1,
  root: [
    {
      kind: "stack",
      axis: "column",
      gap: 16,
      children: [
        {
          kind: "text",
          content: "{{name}}",
          style: { fontSize: 28, fontWeight: 700 },
        },
        {
          kind: "text",
          content: "Moonshine + Crepuscularity View IR",
          style: { opacity: 0.7 },
        },
        {
          kind: "badge",
          label: "renderCrepusIr",
          tone: "accent",
        },
        {
          kind: "button",
          label: "Ping",
          onClick: "ping",
        },
      ],
    },
  ],
} as const satisfies ViewIr;

function App() {
  return (
    <main className="shell">
      {renderCrepusIr(sampleIr, {
        onAction: (handler) => console.log("action:", handler),
      })}
      <section className="spark">
        <h2>Sparkline</h2>
        <Sparkline values={[2, 4, 3, 7, 5, 9, 6, 8, 10, 7]} color="blue" height={56} />
      </section>
    </main>
  );
}

createApp({ root: App }).mount("#app");
"##;

const APP_CSS: &str = r#":root {
  color-scheme: light dark;
  font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
  background:
    radial-gradient(1200px 600px at 10% -10%, #1e3a5f55, transparent),
    radial-gradient(900px 500px at 100% 0%, #0f766e33, transparent),
    #0c1117;
  color: #e8eef6;
}

body {
  margin: 0;
  min-height: 100vh;
}

.shell {
  max-width: 36rem;
  margin: 3.5rem auto;
  padding: 0 1.25rem;
  display: grid;
  gap: 1.5rem;
}

.spark h2 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  opacity: 0.65;
  font-weight: 600;
}
"#;

const INDEX_CREPUS: &str = r#"div w-full min-h-screen p-8 flex flex-col gap-3
 div text-3xl font-bold
  "Hello from Moonshine + .crepus"
 div text-zinc-400
  "Scaffolded by crepus moonshine new."
"#;

const VITE_CONFIG: &str = r#"import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  publicDir: "public",
  server: { port: 5173 },
});
"#;

const TSCONFIG: &str = r#"{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ES2022", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "jsx": "react-jsx",
    "strict": true,
    "skipLibCheck": true,
    "noEmit": true,
    "isolatedModules": true,
    "resolveJsonModule": true
  },
  "include": ["src"]
}
"#;

fn readme_template(local_hint: &str) -> String {
    format!(
        r#"# {{{{name}}}}

React + Vite app scaffolded by `crepus moonshine new`.

Moonshine is a separate product: https://github.com/tschk/moonshine
(local checkout often at `~/projects/moonshine`).

When available, prefer the `moonshine` CLI from tschk/moonshine for new apps.
`crepus moonshine` remains a supported Crepuscularity fallback.

## Setup

{local_hint}

```bash
bun install
bun run dev
```

Emit a View IR app entry from `.crepus` (`dist/crepus-emit.moonshine.tsx`):

```bash
crepus web build --emit moonshine --site .
```

Dependency snippets:

```bash
crepus moonshine dep
```
"#
    )
}

pub fn execute(cmd: MoonshineCommands) -> Result<(), CrepusCliError> {
    match cmd {
        MoonshineCommands::New { name, js } => {
            if js {
                scaffold_app(&name);
            } else {
                rust_scaffold::scaffold_rust_app(&name);
            }
            Ok(())
        }
        MoonshineCommands::Dep => {
            print!("{}", dependency_snippets());
            Ok(())
        }
        MoonshineCommands::Build { dir } => rust_build::build(dir),
    }
}

fn dependency_snippets() -> String {
    let mut out = String::from(
        r#"# package.json dependencies for Moonshine + Crepuscularity
#
# Moonshine: https://github.com/tschk/moonshine
# Do NOT use bun's github-repo + nested path form — it 404s. Use file: paths instead.
"#,
    );

    if let Some(root) = resolve_moonshine_root() {
        let (core, crepus, components) = file_dep_paths(&root, env::current_dir().ok().as_deref());
        out.push_str(&format!(
            r#"
# A) Local checkout detected at {}
#    Prefer file: paths (relative to cwd when possible):

{{
  "dependencies": {{
    "@tschk/moonshine": "{core}",
    "@tschk/crepus-moonshine": "{crepus}",
    "@tschk/moonshine-components": "{components}",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  }}
}}
"#,
            root.display()
        ));
    } else {
        out.push_str(
            r#"
# A) No local moonshine checkout found.
#    Set MOONSHINE_PATH, place a checkout at ../moonshine, or ~/projects/moonshine.
#    Placeholder file: paths (clone first — see B):

{
  "dependencies": {
    "@tschk/moonshine": "file:../moonshine/packages/core",
    "@tschk/crepus-moonshine": "file:../moonshine/packages/crepus-moonshine",
    "@tschk/moonshine-components": "file:../moonshine/components",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  }
}
"#,
        );
    }

    out.push_str(
        r#"
# B) Clone tschk/moonshine, then point file: deps at that tree:

git clone https://github.com/tschk/moonshine.git ../moonshine
# or: git clone https://github.com/tschk/moonshine.git ~/projects/moonshine

# Then either re-run `crepus moonshine dep` / `crepus moonshine new`, or set:
#   export MOONSHINE_PATH=/absolute/path/to/moonshine
"#,
    );

    out
}

fn scaffold_app(name: &str) {
    let t0 = Instant::now();
    let slug = name
        .to_lowercase()
        .chars()
        .map(|c| if c.is_alphanumeric() { c } else { '-' })
        .collect::<String>();
    let base = PathBuf::from(&slug);
    if base.exists() {
        ui::error(&format!("directory already exists: {slug}"));
    }

    let app_abs = env::current_dir()
        .map(|cwd| cwd.join(&base))
        .unwrap_or_else(|_| base.clone());

    let (core, crepus, components, local_hint) = match resolve_moonshine_root() {
        Some(root) => {
            let (c, cr, co) = file_dep_paths(&root, Some(&app_abs));
            let hint = format!(
                "Local moonshine detected at `{}` — `package.json` uses `file:` deps.",
                root.display()
            );
            eprintln!("{} {hint}", ui::ok());
            (c, cr, co, hint)
        }
        None => {
            eprintln!(
                "{} no local moonshine checkout found (MOONSHINE_PATH, ../moonshine, or ~/projects/moonshine)",
                ui::warn()
            );
            eprintln!(
                "  {} scaffolding with placeholder file:../moonshine/… — clone tschk/moonshine first (see README)",
                ui::dim("")
            );
            (
                PLACEHOLDER_CORE.to_string(),
                PLACEHOLDER_CREPUS.to_string(),
                PLACEHOLDER_COMPONENTS.to_string(),
                r#"No local moonshine checkout was found. Clone it next to this app (or set `MOONSHINE_PATH`):

```bash
git clone https://github.com/tschk/moonshine.git ../moonshine
# packages: packages/core, packages/crepus-moonshine, components
```

`package.json` already uses placeholder `file:../moonshine/…` paths."#
                    .to_string(),
            )
        }
    };

    scaffold::ensure_dir(&base.join("src"))
        .unwrap_or_else(|e| ui::error(&format!("create src: {e}")));
    scaffold::ensure_dir(&base.join("public"))
        .unwrap_or_else(|e| ui::error(&format!("create public: {e}")));

    let pkg = package_json_template(&core, &crepus, &components);
    scaffold::write_template(&base.join("package.json"), &pkg, &[("{{slug}}", &slug)])
        .unwrap_or_else(|e| ui::error(&format!("write package.json: {e}")));
    scaffold::write_template(&base.join("index.html"), INDEX_HTML, &[("{{name}}", name)])
        .unwrap_or_else(|e| ui::error(&format!("write index.html: {e}")));
    scaffold::write_template(&base.join("src/main.tsx"), MAIN_TSX, &[("{{name}}", name)])
        .unwrap_or_else(|e| ui::error(&format!("write src/main.tsx: {e}")));
    scaffold::write_file(&base.join("src/app.css"), APP_CSS)
        .unwrap_or_else(|e| ui::error(&format!("write src/app.css: {e}")));
    scaffold::write_file(&base.join("public/index.crepus"), INDEX_CREPUS)
        .unwrap_or_else(|e| ui::error(&format!("write public/index.crepus: {e}")));
    // Root index.crepus for `crepus web build --emit moonshine`.
    scaffold::write_file(&base.join("index.crepus"), INDEX_CREPUS)
        .unwrap_or_else(|e| ui::error(&format!("write index.crepus: {e}")));
    scaffold::write_file(&base.join("vite.config.ts"), VITE_CONFIG)
        .unwrap_or_else(|e| ui::error(&format!("write vite.config.ts: {e}")));
    scaffold::write_file(&base.join("tsconfig.json"), TSCONFIG)
        .unwrap_or_else(|e| ui::error(&format!("write tsconfig.json: {e}")));
    let readme = readme_template(&local_hint);
    scaffold::write_template(&base.join("README.md"), &readme, &[("{{name}}", name)])
        .unwrap_or_else(|e| ui::error(&format!("write README.md: {e}")));

    eprintln!(
        "  {} prefer `moonshine` CLI (tschk/moonshine) when available; `crepus moonshine` stays supported",
        ui::dim("")
    );
    scaffold::scaffold_success(
        &slug,
        &base,
        &[
            &format!("cd {slug}"),
            "bun install",
            "bun run dev",
            "crepus moonshine dep   # dependency snippets",
            "crepus web build --emit moonshine --site .",
        ],
    );
    ui::done_in(t0.elapsed());
}

/// `crepus moon new` — the Rust-only scaffold: crepus templates inlined as
/// raw string literals in `src/main.rs`, parsed with
/// `crepuscularity_native::render_template_to_ir` and emitted with
/// `emit_moonshine_app` / `emit_moonshine_component` at `cargo run` time.
mod rust_scaffold {
    use std::time::Instant;

    use crate::scaffold;
    use crate::ui;

    const SCAFFOLD_NATIVE_VERSION: &str = "0.6.1";

    const CARGO_TOML: &str = r#"[package]
name = "{{slug}}"
version = "0.1.0"
edition = "2021"

[dependencies]
crepuscularity-native = "{{native_version}}"
"#;

    const MAIN_RS: &str = r####"//! Generated by `crepus moon new`. crepus templates are inlined here as raw
//! string literals — edit them, then run `cargo run` (or `crepus moon build`)
//! to regenerate the Moonshine app under `generated/`.
//!
//! Drop TypeScript modules in `ts/` — they are copied into the built app by
//! `crepus moon build` and stay importable from the generated TSX.

use crepuscularity_native::{emit_moonshine_app, emit_moonshine_component, render_template_to_ir};

/// The page — becomes `generated/app.tsx` (the Moonshine app entry).
const PAGE: &str = r###"div w-full min-h-screen p-8 flex flex-col gap-4
 div text-3xl font-bold
  "{{name}}"
 div text-zinc-400
  "Crepus templates inlined in src/main.rs, built with crepus moon build."
 button
  "Ping"
"###;

/// A reusable component — becomes `generated/components/Card.tsx`.
const CARD: &str = r###"div rounded-lg border p-4 flex flex-col gap-2
 div text-lg font-semibold
  "Card"
 div text-sm text-zinc-400
  "Emitted via emit_moonshine_component."
"###;

fn main() {
    let page_ir = render_template_to_ir(PAGE, &Default::default()).expect("render PAGE template");
    let app_tsx = emit_moonshine_app(&page_ir);

    let card_ir = render_template_to_ir(CARD, &Default::default()).expect("render CARD template");
    let card_tsx = emit_moonshine_component(&card_ir, "Card");

    std::fs::create_dir_all("generated/components").expect("create generated/components");
    std::fs::write("generated/app.tsx", app_tsx).expect("write generated/app.tsx");
    std::fs::write("generated/components/Card.tsx", card_tsx)
        .expect("write generated/components/Card.tsx");

    println!("wrote generated/app.tsx and generated/components/Card.tsx");
    println!("next: crepus moon build");
}
"####;

    const EXAMPLE_TS: &str = r#"// Example TypeScript module — imported by the generated app, or import it
// yourself from src/ once `crepus moon build` copies this directory in.
export function greet(name: string): string {
  return `Hello, ${name}! (from ts/example.ts)`;
}
"#;

    fn readme_template() -> &'static str {
        r#"# {{name}}

Rust-only Moonshine app scaffolded by `crepus moon new` (alias for
`crepus moonshine new`).

crepus templates are inlined as raw string literals in `src/main.rs`. Running
the project (`cargo run`) parses them with
`crepuscularity_native::render_template_to_ir` and emits a real Moonshine
React/TSX app under `generated/` via `emit_moonshine_app` /
`emit_moonshine_component`.

## Build

```bash
crepus moon build
```

This runs `cargo run` to (re)generate `generated/`, refreshes
`package.json`, `index.html`, `vite.config.ts`, and `tsconfig.json`, copies
`ts/` into the app so your TypeScript modules stay importable, and then runs
`bun install && bun run build` if `bun` is on PATH. If `bun` is missing, the
project is still fully generated — install bun and re-run `crepus moon
build`, or run the printed commands yourself.

## Add TypeScript modules or npm libraries

- Drop `.ts`/`.tsx` files in `ts/` — they're copied in and importable from
  the generated app.
- This is a real bun project after `crepus moon build`: run `bun add
  <package>` in the project directory like any other bun/Vite app.

## Add more templates

Add more `const` raw strings to `src/main.rs`, render them with
`render_template_to_ir`, and emit them with `emit_moonshine_app` (a page) or
`emit_moonshine_component(&ir, "Name")` (a reusable component) — write the
result under `generated/` and `crepus moon build` will pick it up.
"#
    }

    pub fn scaffold_rust_app(name: &str) {
        let t0 = Instant::now();
        let slug = name
            .to_lowercase()
            .chars()
            .map(|c| if c.is_alphanumeric() { c } else { '-' })
            .collect::<String>();
        let base = std::path::PathBuf::from(&slug);
        if base.exists() {
            ui::error(&format!("directory already exists: {slug}"));
        }

        scaffold::ensure_dir(&base.join("src"))
            .unwrap_or_else(|e| ui::error(&format!("create src: {e}")));
        scaffold::ensure_dir(&base.join("ts"))
            .unwrap_or_else(|e| ui::error(&format!("create ts: {e}")));

        let cargo_toml = CARGO_TOML
            .replace("{{slug}}", &slug)
            .replace("{{native_version}}", SCAFFOLD_NATIVE_VERSION);
        scaffold::write_file(&base.join("Cargo.toml"), &cargo_toml)
            .unwrap_or_else(|e| ui::error(&format!("write Cargo.toml: {e}")));

        scaffold::write_template(&base.join("src/main.rs"), MAIN_RS, &[("{{name}}", name)])
            .unwrap_or_else(|e| ui::error(&format!("write src/main.rs: {e}")));

        scaffold::write_file(&base.join("ts/example.ts"), EXAMPLE_TS)
            .unwrap_or_else(|e| ui::error(&format!("write ts/example.ts: {e}")));

        let readme = readme_template().replace("{{name}}", name);
        scaffold::write_file(&base.join("README.md"), &readme)
            .unwrap_or_else(|e| ui::error(&format!("write README.md: {e}")));

        scaffold::scaffold_success(
            &slug,
            &base,
            &[
                &format!("cd {slug}"),
                "cargo run           # emit generated/ from the inlined crepus templates",
                "crepus moon build   # build the real Moonshine + Vite site",
            ],
        );
        ui::done_in(t0.elapsed());
    }
}

/// `crepus moon build` — builds a `crepus moon new` Rust-only project into a
/// real Moonshine + React + Vite app.
mod rust_build {
    use std::env;
    use std::path::{Path, PathBuf};
    use std::process::Command;
    use std::time::Instant;

    use super::{
        file_dep_paths, package_json_template, resolve_moonshine_root, INDEX_HTML,
        PLACEHOLDER_COMPONENTS, PLACEHOLDER_CORE, PLACEHOLDER_CREPUS, TSCONFIG, VITE_CONFIG,
    };
    use crate::error::CrepusCliError;
    use crate::scaffold;
    use crate::ui;

    const MAIN_TSX_BOOTSTRAP: &str = r#"// Generated by `crepus moon build` — bootstraps the app emitted at
// generated/app.tsx (kept in sync with your inlined crepus templates in
// src/main.rs). Safe to edit; regenerated on every `crepus moon build`.
import { mount } from "./App";

mount();
"#;

    pub fn build(dir: Option<PathBuf>) -> Result<(), CrepusCliError> {
        let t0 = Instant::now();
        let project = dir.unwrap_or_else(|| PathBuf::from("."));

        if !project.join("Cargo.toml").is_file() {
            return Err(CrepusCliError::context(format!(
                "no Cargo.toml in {} — run `crepus moon build` from a project scaffolded by \
                 `crepus moon new`, or pass --dir",
                project.display()
            )));
        }

        eprintln!(
            "{} cargo run (emit generated/ from inlined crepus templates)",
            ui::arrow()
        );
        let status = Command::new("cargo")
            .arg("run")
            .current_dir(&project)
            .status()
            .map_err(|e| {
                CrepusCliError::context(format!(
                    "failed to invoke `cargo run`: {e}. Is the Rust toolchain installed and on PATH?"
                ))
            })?;
        if !status.success() {
            return Err(CrepusCliError::command(
                status.code(),
                "`cargo run` failed — fix the errors above and re-run `crepus moon build`",
            ));
        }

        let generated_app = project.join("generated/app.tsx");
        if !generated_app.is_file() {
            return Err(CrepusCliError::context(
                "cargo run did not write generated/app.tsx — check src/main.rs calls \
                 emit_moonshine_app and writes to generated/app.tsx"
                    .to_string(),
            ));
        }

        let name = project
            .canonicalize()
            .ok()
            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
            .unwrap_or_else(|| "moon-app".to_string());
        let slug = name
            .to_lowercase()
            .chars()
            .map(|c| if c.is_alphanumeric() { c } else { '-' })
            .collect::<String>();

        scaffold::ensure_dir(&project.join("src/components")).map_err(CrepusCliError::context)?;

        let app_tsx = std::fs::read_to_string(&generated_app).map_err(|e| e.to_string())?;
        scaffold::write_file(&project.join("src/App.tsx"), &app_tsx)
            .map_err(CrepusCliError::context)?;

        let generated_components = project.join("generated/components");
        if generated_components.is_dir() {
            copy_dir_flat(&generated_components, &project.join("src/components"))
                .map_err(CrepusCliError::context)?;
        }

        let ts_dir = project.join("ts");
        if ts_dir.is_dir() {
            let dest = project.join("src/ts");
            scaffold::ensure_dir(&dest).map_err(CrepusCliError::context)?;
            copy_dir_flat(&ts_dir, &dest).map_err(CrepusCliError::context)?;
        }

        scaffold::write_file(&project.join("src/main.tsx"), MAIN_TSX_BOOTSTRAP)
            .map_err(CrepusCliError::context)?;

        let (core, crepus, components, local_hint) = match resolve_moonshine_root() {
            Some(root) => {
                let (c, cr, co) = file_dep_paths(&root, Some(project.as_path()));
                let hint = format!("local moonshine checkout at `{}`", root.display());
                (c, cr, co, hint)
            }
            None => (
                PLACEHOLDER_CORE.to_string(),
                PLACEHOLDER_CREPUS.to_string(),
                PLACEHOLDER_COMPONENTS.to_string(),
                "no local moonshine checkout found (MOONSHINE_PATH, ../moonshine, or \
                 ~/projects/moonshine) — using placeholder file: paths"
                    .to_string(),
            ),
        };
        eprintln!("{} {local_hint}", ui::arrow());

        let pkg = package_json_template(&core, &crepus, &components);
        scaffold::write_template(&project.join("package.json"), &pkg, &[("{{slug}}", &slug)])
            .map_err(CrepusCliError::context)?;
        scaffold::write_template(
            &project.join("index.html"),
            INDEX_HTML,
            &[("{{name}}", &name)],
        )
        .map_err(CrepusCliError::context)?;
        scaffold::write_file(&project.join("vite.config.ts"), VITE_CONFIG)
            .map_err(CrepusCliError::context)?;
        scaffold::write_file(&project.join("tsconfig.json"), TSCONFIG)
            .map_err(CrepusCliError::context)?;

        match which_bun() {
            Some(_) => {
                eprintln!("{} bun install", ui::arrow());
                run_bun(&project, &["install"])?;
                eprintln!("{} bun run build", ui::arrow());
                run_bun(&project, &["run", "build"])?;
                ui::success(&format!(
                    "built {} in {:?}",
                    project.display(),
                    t0.elapsed()
                ));
            }
            None => {
                eprintln!(
                    "{} bun not found on PATH — project generated but not built",
                    ui::warn()
                );
                eprintln!("  run next:");
                eprintln!("    cd {}", project.display());
                eprintln!("    bun install");
                eprintln!("    bun run build");
            }
        }

        Ok(())
    }

    fn which_bun() -> Option<PathBuf> {
        let path = env::var_os("PATH")?;
        env::split_paths(&path).find_map(|dir| {
            let candidate = dir.join("bun");
            candidate.is_file().then_some(candidate)
        })
    }

    fn run_bun(project: &Path, args: &[&str]) -> Result<(), CrepusCliError> {
        let status = Command::new("bun")
            .args(args)
            .current_dir(project)
            .status()
            .map_err(|e| {
                CrepusCliError::context(format!("failed to invoke `bun {args:?}`: {e}"))
            })?;
        if !status.success() {
            return Err(CrepusCliError::command(
                status.code(),
                format!("`bun {}` failed", args.join(" ")),
            ));
        }
        Ok(())
    }

    /// Copy every regular file directly under `src` into `dest` (non-recursive
    /// into further subdirectories beyond one level, which is all the
    /// generated `components/` and user `ts/` layouts need).
    fn copy_dir_flat(src: &Path, dest: &Path) -> Result<(), String> {
        std::fs::create_dir_all(dest).map_err(|e| e.to_string())?;
        for entry in std::fs::read_dir(src).map_err(|e| e.to_string())? {
            let entry = entry.map_err(|e| e.to_string())?;
            let path = entry.path();
            if path.is_file() {
                let file_name = path.file_name().ok_or("missing file name")?;
                std::fs::copy(&path, dest.join(file_name)).map_err(|e| e.to_string())?;
            } else if path.is_dir() {
                let dir_name = path.file_name().ok_or("missing dir name")?;
                copy_dir_flat(&path, &dest.join(dir_name))?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dep_mentions_moonshine_packages() {
        let snip = dependency_snippets();
        assert!(snip.contains("\"@tschk/moonshine\""));
        assert!(snip.contains("\"@tschk/crepus-moonshine\""));
        assert!(snip.contains("\"@tschk/moonshine-components\""));
        assert!(snip.contains("file:"));
        assert!(snip.contains("git clone https://github.com/tschk/moonshine"));
        assert!(!snip.contains("#path:packages/"));
    }

    #[test]
    fn package_json_includes_react() {
        let pkg =
            package_json_template(PLACEHOLDER_CORE, PLACEHOLDER_CREPUS, PLACEHOLDER_COMPONENTS);
        assert!(pkg.contains("\"react\""));
        assert!(pkg.contains("\"react-dom\""));
        assert!(pkg.contains("@vitejs/plugin-react"));
        assert!(pkg.contains("\"@tschk/moonshine\": \"^0.3.4\""));
        // Without a local checkout the fallback must be installable anywhere, so
        // no `file:` path may reach a scaffolded package.json.
        assert!(!pkg.contains("file:"), "{pkg}");
    }

    #[test]
    fn looks_like_moonshine_rejects_empty() {
        assert!(!looks_like_moonshine_checkout(Path::new(
            "/tmp/nope-moonshine-xyz"
        )));
    }
}