From 281e8ccc1751ff17424eec3da784a2ddc194e0ad Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 11 Jul 2026 12:49:53 -0500 Subject: [PATCH] feat: embed official starter template with token substitution Signed-off-by: Diego Resendez --- src/main.rs | 1 + src/template.rs | 49 +++++++++++++++++++ template/starter/deno.json | 1 + template/starter/matcha.toml | 1 + template/starter/public/index.html | 23 +++++++++ template/starter/src/app.module.ts | 5 ++ .../src/controllers/home.controller.ts | 26 ++++++++++ 7 files changed, 106 insertions(+) create mode 100644 src/template.rs create mode 100644 template/starter/deno.json create mode 100644 template/starter/matcha.toml create mode 100644 template/starter/public/index.html create mode 100644 template/starter/src/app.module.ts create mode 100644 template/starter/src/controllers/home.controller.ts diff --git a/src/main.rs b/src/main.rs index da2d687..bb067cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod cli; mod runtime; +mod template; use clap::Parser; use cli::{Cli, Command}; diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..1bcf073 --- /dev/null +++ b/src/template.rs @@ -0,0 +1,49 @@ +use include_dir::{include_dir, Dir}; +use std::path::Path; + +pub static STARTER: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/starter"); + +#[allow(dead_code)] +pub fn write_starter(dest: &Path, project_name: &str) -> std::io::Result<()> { + write_dir(&STARTER, dest, project_name) +} + +fn write_dir(dir: &Dir, dest: &Path, name: &str) -> std::io::Result<()> { + for entry in dir.entries() { + match entry { + include_dir::DirEntry::Dir(d) => { + let sub = dest.join(d.path().file_name().unwrap()); + std::fs::create_dir_all(&sub)?; + write_dir(d, &sub, name)?; + } + include_dir::DirEntry::File(f) => { + let out = dest.join(f.path().file_name().unwrap()); + if let Some(parent) = out.parent() { + std::fs::create_dir_all(parent)?; + } + let contents = std::str::from_utf8(f.contents()) + .map(|s| s.replace("{{project_name}}", name).into_bytes()) + .unwrap_or_else(|_| f.contents().to_vec()); + std::fs::write(out, contents)?; + } + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::tempdir; + + #[test] + fn writes_starter_and_substitutes_name() { + let d = tempdir().unwrap(); + write_starter(d.path(), "my-api").unwrap(); + let html = std::fs::read_to_string(d.path().join("public/index.html")).unwrap(); + assert!(html.contains("my-api")); + assert!(!html.contains("{{project_name}}")); + assert!(d.path().join("src/controllers/home.controller.ts").exists()); + assert!(d.path().join("matcha.toml").exists()); + } +} diff --git a/template/starter/deno.json b/template/starter/deno.json new file mode 100644 index 0000000..92ad661 --- /dev/null +++ b/template/starter/deno.json @@ -0,0 +1 @@ +{ "tasks": { "dev": "deno run --watch --allow-net --allow-read src/main.ts" } } diff --git a/template/starter/matcha.toml b/template/starter/matcha.toml new file mode 100644 index 0000000..0e63dab --- /dev/null +++ b/template/starter/matcha.toml @@ -0,0 +1 @@ +runtime = "deno" diff --git a/template/starter/public/index.html b/template/starter/public/index.html new file mode 100644 index 0000000..f9571d1 --- /dev/null +++ b/template/starter/public/index.html @@ -0,0 +1,23 @@ + + + + + {{project_name}} + + + +
+ +
connecting…
+
+ + + diff --git a/template/starter/src/app.module.ts b/template/starter/src/app.module.ts new file mode 100644 index 0000000..5a4752b --- /dev/null +++ b/template/starter/src/app.module.ts @@ -0,0 +1,5 @@ +import { Module } from '@green-tea/core'; +import { HomeController } from './controllers/home.controller'; + +@Module({ controllers: [HomeController] }) +export class AppModule {} diff --git a/template/starter/src/controllers/home.controller.ts b/template/starter/src/controllers/home.controller.ts new file mode 100644 index 0000000..a828546 --- /dev/null +++ b/template/starter/src/controllers/home.controller.ts @@ -0,0 +1,26 @@ +import { Route, Get, Html, Sse } from '@green-tea/core'; + +const ZEN = [ + 'Simplicity is the ultimate sophistication.', + 'The obstacle is the path.', + 'When you let go, you have two hands to work.', + 'Make it work, make it right, make it fast.', + 'Less, but better.', +]; + +@Route('/') +export class HomeController { + @Get('/') + @Html('public/index.html') + home() {} + + @Sse('/zen') + zen() { + return (async function* () { + for (let i = 0; ; i++) { + yield { zen: ZEN[i % ZEN.length] }; + await new Promise((r) => setTimeout(r, 30_000)); + } + })(); + } +}