diff --git a/.specs/2026-07-12-matcha-multi-runtime.md b/.specs/2026-07-12-matcha-multi-runtime.md new file mode 100644 index 0000000..b2f1beb --- /dev/null +++ b/.specs/2026-07-12-matcha-multi-runtime.md @@ -0,0 +1,152 @@ +# matcha — multi-runtime starter (node/deno/bun) · Spec + +**Date:** 2026-07-12 +**Branch:** `feature/matcha-multi-runtime` (off `develop`) +**Goal:** `matcha new --runtime ` scaffolds the starter for the chosen runtime. Shared app files are written once; only `main.ts` + runtime config + `matcha.toml` differ per runtime. Default runtime: **node** (green-tea is developed node-first; Node is the reference implementation. deno/bun via `--runtime`). + +## Verified framework facts (from Green-Tea/core) +- `@green-tea/core` imports `reflect-metadata` internally (`src/index.ts:6`) — consumers need NO explicit import; it installs transitively with the package. +- Serve per runtime: Node `app.listen(3000)`; Deno `Deno.serve({ port }, app.fetch)`; Bun `Bun.serve({ port, fetch: app.fetch })`. Starter has no WebSocket, so `app.fetch` is sufficient (no `serveDeno`/`serveBun`). +- `@Module` requires `mountpoint`. Package version pin: `^26.7.0-beta.0`. + +## New template layout (replaces `template/starter/`) +``` +template/ + shared/ + public/index.html + src/app.module.ts + src/controllers/home.controller.ts + runtimes/ + deno/ { matcha.toml, deno.json, src/main.ts } + node/ { matcha.toml, package.json, tsconfig.json, src/main.ts } + bun/ { matcha.toml, package.json, tsconfig.json, src/main.ts } +``` +`write_starter` writes `shared` then overlays the chosen runtime dir on top (both go into the same project dir; `src/main.ts` from the overlay lands beside `src/app.module.ts` from shared). Token substitution `{{project_name}}` applies to every copied file (now also `package.json` "name"). + +## Exact file contents + +### shared/ (moved verbatim from current `template/starter/`) +- `public/index.html` — unchanged (keep `{{project_name}}` title + `EventSource('/zen')`). +- `src/app.module.ts` — unchanged (`@Module({ mountpoint: '/', controllers: [HomeController] })`). +- `src/controllers/home.controller.ts` — unchanged (`@Route('/')`, `@Get('/') @Html('public/index.html')`, `@Sse('/zen')` rotating zen every 30s). + +### runtimes/deno/ (moved from current starter) +- `matcha.toml`: `runtime = "deno"` +- `deno.json` (unchanged from current starter): +```json +{ + "compilerOptions": { "experimentalDecorators": true }, + "unstable": ["sloppy-imports"], + "imports": { "@green-tea/core": "npm:@green-tea/core@^26.7.0-beta.0" }, + "tasks": { "dev": "deno run --watch --allow-net --allow-read --allow-env src/main.ts" } +} +``` +- `src/main.ts`: +```ts +import { createApp } from '@green-tea/core'; +import { AppModule } from './app.module'; + +const app = createApp({ modules: [AppModule] }); + +Deno.serve({ port: 8000 }, app.fetch); +console.log('🍵 green-tea running on http://localhost:8000'); +``` + +### runtimes/node/ +- `matcha.toml`: `runtime = "node"` +- `package.json`: +```json +{ + "name": "{{project_name}}", + "type": "module", + "private": true, + "scripts": { "dev": "tsx watch src/main.ts" }, + "dependencies": { "@green-tea/core": "^26.7.0-beta.0" }, + "devDependencies": { "tsx": "^4", "typescript": "^5" } +} +``` +- `tsconfig.json`: +```json +{ + "compilerOptions": { + "target": "es2020", + "module": "esnext", + "moduleResolution": "bundler", + "experimentalDecorators": true, + "strict": true, + "skipLibCheck": true + } +} +``` +- `src/main.ts`: +```ts +import { createApp } from '@green-tea/core'; +import { AppModule } from './app.module'; + +const app = createApp({ modules: [AppModule] }); + +app.listen(3000); +console.log('🍵 green-tea running on http://localhost:3000'); +``` + +### runtimes/bun/ +- `matcha.toml`: `runtime = "bun"` +- `package.json`: +```json +{ + "name": "{{project_name}}", + "type": "module", + "private": true, + "scripts": { "dev": "bun --watch run src/main.ts" }, + "dependencies": { "@green-tea/core": "^26.7.0-beta.0" } +} +``` +- `tsconfig.json`: +```json +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "moduleResolution": "bundler", + "experimentalDecorators": true, + "strict": true, + "skipLibCheck": true + } +} +``` +- `src/main.ts`: +```ts +import { createApp } from '@green-tea/core'; +import { AppModule } from './app.module'; + +const app = createApp({ modules: [AppModule] }); + +Bun.serve({ port: 8000, fetch: app.fetch }); +console.log('🍵 green-tea running on http://localhost:8000'); +``` + +## Code changes + +### `src/template.rs` +- Replace the single `STARTER` embed with `SHARED` + three runtime overlays (`include_dir!` each). +- `pub fn write_starter(dest: &Path, project_name: &str, runtime: Runtime) -> io::Result<()>` — write `SHARED`, then the overlay for `runtime`. Keep `write_dir` substitution logic. + +### `src/cli.rs` +- `Command::New` gains `#[arg(long, default_value = "deno", value_parser = ["node", "deno", "bun"])] runtime: String`. + +### `src/cmd_new.rs` +- `run(name, template_url, runtime: &str)`: parse `runtime` via `Runtime::from_str` (already in `runtime.rs`); for the starter (no `--template-url`) path, call `write_starter(dest, name, rt)`. `--template-url` path ignores `--runtime`. + +### `src/cmd_run.rs` +- Change the Bun mapping from `("bun", vec!["--watch", "run", "dev"])` to `("bun", vec!["run", "dev"])` so all three uniformly run their `dev` script (deno.json/package.json define the watch). Update the `bun_watch` unit test accordingly. + +### `src/main.rs` +- Pass the new `runtime` arg through the `Command::New` dispatch. + +## Tests +- `template.rs`: parametrize the write test — assert deno writes `deno.json` + `src/main.ts` containing `Deno.serve`; node writes `package.json` (with substituted name) + `tsconfig.json` + `main.ts` containing `app.listen`; bun writes `package.json` + `main.ts` containing `Bun.serve`. All three write the shared `src/app.module.ts` and `public/index.html`. +- `tests/new.rs`: `matcha new demo --runtime node` produces `package.json` with `"name": "demo"` and no `deno.json`; default (no flag) produces `deno.json`. +- `cmd_run.rs`: bun mapping test expects `("bun", vec!["run", "dev"])`. + +## Out of scope +Edge starter (deploy target, not `run`). Per-runtime lockfiles. `serveDeno`/`serveBun` (no WS in starter). diff --git a/src/cli.rs b/src/cli.rs index f26f88b..553806f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,8 @@ pub enum Command { name: String, #[arg(long)] template_url: Option, + #[arg(long, default_value = "node", value_parser = ["node", "deno", "bun"])] + runtime: String, }, /// Detect the runtime and run the project in watch mode Run, diff --git a/src/cmd_new.rs b/src/cmd_new.rs index 4d0072d..0b111ab 100644 --- a/src/cmd_new.rs +++ b/src/cmd_new.rs @@ -1,9 +1,10 @@ use std::io::{Error, ErrorKind}; use std::path::Path; +use crate::runtime::Runtime; use crate::template; -pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> { +pub fn run(name: &str, template_url: Option<&str>, runtime: &str) -> std::io::Result<()> { let dest = Path::new(name); if dest.exists() { return Err(Error::new( @@ -14,8 +15,14 @@ pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> { match template_url { None => { + let rt = Runtime::from_str(runtime).ok_or_else(|| { + Error::new( + ErrorKind::InvalidInput, + format!("unknown runtime '{runtime}'"), + ) + })?; std::fs::create_dir_all(dest)?; - template::write_starter(dest, name)?; + template::write_starter(dest, name, rt)?; } Some(url) => clone_template(url, dest)?, } diff --git a/src/cmd_run.rs b/src/cmd_run.rs index 28cb2df..8b6d7f5 100644 --- a/src/cmd_run.rs +++ b/src/cmd_run.rs @@ -6,7 +6,7 @@ use crate::runtime::{self, Runtime}; fn command_for(rt: Runtime) -> (&'static str, Vec<&'static str>) { match rt { Runtime::Deno => ("deno", vec!["task", "dev"]), - Runtime::Bun => ("bun", vec!["--watch", "run", "dev"]), + Runtime::Bun => ("bun", vec!["run", "dev"]), Runtime::Node => ("npm", vec!["run", "dev"]), } } @@ -38,10 +38,7 @@ mod tests { } #[test] fn bun_watch() { - assert_eq!( - command_for(Runtime::Bun), - ("bun", vec!["--watch", "run", "dev"]) - ); + assert_eq!(command_for(Runtime::Bun), ("bun", vec!["run", "dev"])); } #[test] fn node_watch() { diff --git a/src/main.rs b/src/main.rs index ea02138..ce41459 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,12 @@ use cli::{Cli, Command}; fn main() { let cli = Cli::parse(); match cli.command { - Command::New { name, template_url } => { - if let Err(e) = cmd_new::run(&name, template_url.as_deref()) { + Command::New { + name, + template_url, + runtime, + } => { + if let Err(e) = cmd_new::run(&name, template_url.as_deref(), &runtime) { eprintln!("error: {e}"); std::process::exit(1); } diff --git a/src/runtime.rs b/src/runtime.rs index 773e1c0..99070b7 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,6 +1,5 @@ use std::path::Path; -#[allow(dead_code)] #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Runtime { Node, @@ -9,7 +8,6 @@ pub enum Runtime { } impl Runtime { - #[allow(dead_code)] pub fn from_str(s: &str) -> Option { match s.trim() { "node" => Some(Runtime::Node), @@ -20,13 +18,11 @@ impl Runtime { } } -#[allow(dead_code)] fn exists(dir: &Path, name: &str) -> bool { dir.join(name).exists() } /// Reads `runtime = "..."` from matcha.toml without a TOML dep (one field only). -#[allow(dead_code)] fn override_from_toml(dir: &Path) -> Option { let text = std::fs::read_to_string(dir.join("matcha.toml")).ok()?; for line in text.lines() { @@ -39,7 +35,6 @@ fn override_from_toml(dir: &Path) -> Option { None } -#[allow(dead_code)] pub fn detect(dir: &Path) -> Option { if let Some(r) = override_from_toml(dir) { return Some(r); diff --git a/src/template.rs b/src/template.rs index 1f180c2..267ff60 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,10 +1,24 @@ use include_dir::{include_dir, Dir}; use std::path::Path; -pub static STARTER: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/starter"); +use crate::runtime::Runtime; -pub fn write_starter(dest: &Path, project_name: &str) -> std::io::Result<()> { - write_dir(&STARTER, dest, project_name) +pub static SHARED: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/shared"); +pub static RUNTIME_DENO: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/deno"); +pub static RUNTIME_NODE: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/node"); +pub static RUNTIME_BUN: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/bun"); + +fn overlay_for(runtime: Runtime) -> &'static Dir<'static> { + match runtime { + Runtime::Deno => &RUNTIME_DENO, + Runtime::Node => &RUNTIME_NODE, + Runtime::Bun => &RUNTIME_BUN, + } +} + +pub fn write_starter(dest: &Path, project_name: &str, runtime: Runtime) -> std::io::Result<()> { + write_dir(&SHARED, dest, project_name)?; + write_dir(overlay_for(runtime), dest, project_name) } fn write_dir(dir: &Dir, dest: &Path, name: &str) -> std::io::Result<()> { @@ -36,16 +50,54 @@ mod tests { use tempfile::tempdir; #[test] - fn writes_starter_and_substitutes_name() { + fn deno_writes_shared_and_deno_overlay() { let d = tempdir().unwrap(); - write_starter(d.path(), "my-api").unwrap(); + write_starter(d.path(), "my-api", Runtime::Deno).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()); - assert!(d.path().join("src/main.ts").exists()); let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap(); assert!(module.contains("mountpoint")); + + assert!(d.path().join("deno.json").exists()); + assert!(d.path().join("matcha.toml").exists()); + let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap(); + assert!(main.contains("Deno.serve")); + assert!(!d.path().join("package.json").exists()); + } + + #[test] + fn node_writes_shared_and_node_overlay() { + let d = tempdir().unwrap(); + write_starter(d.path(), "my-api", Runtime::Node).unwrap(); + + assert!(d.path().join("public/index.html").exists()); + assert!(d.path().join("src/app.module.ts").exists()); + + let pkg = std::fs::read_to_string(d.path().join("package.json")).unwrap(); + assert!(pkg.contains("\"name\": \"my-api\"")); + assert!(!pkg.contains("{{project_name}}")); + assert!(d.path().join("tsconfig.json").exists()); + let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap(); + assert!(main.contains("app.listen")); + assert!(!d.path().join("deno.json").exists()); + } + + #[test] + fn bun_writes_shared_and_bun_overlay() { + let d = tempdir().unwrap(); + write_starter(d.path(), "my-api", Runtime::Bun).unwrap(); + + assert!(d.path().join("public/index.html").exists()); + assert!(d.path().join("src/app.module.ts").exists()); + + let pkg = std::fs::read_to_string(d.path().join("package.json")).unwrap(); + assert!(pkg.contains("\"name\": \"my-api\"")); + assert!(!pkg.contains("{{project_name}}")); + let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap(); + assert!(main.contains("Bun.serve")); + assert!(!d.path().join("deno.json").exists()); } } diff --git a/template/runtimes/bun/matcha.toml b/template/runtimes/bun/matcha.toml new file mode 100644 index 0000000..a426864 --- /dev/null +++ b/template/runtimes/bun/matcha.toml @@ -0,0 +1 @@ +runtime = "bun" diff --git a/template/runtimes/bun/package.json b/template/runtimes/bun/package.json new file mode 100644 index 0000000..b8b3863 --- /dev/null +++ b/template/runtimes/bun/package.json @@ -0,0 +1,7 @@ +{ + "name": "{{project_name}}", + "type": "module", + "private": true, + "scripts": { "dev": "bun --watch run src/main.ts" }, + "dependencies": { "@green-tea/core": "^26.7.0-beta.0" } +} diff --git a/template/runtimes/bun/src/main.ts b/template/runtimes/bun/src/main.ts new file mode 100644 index 0000000..34586a8 --- /dev/null +++ b/template/runtimes/bun/src/main.ts @@ -0,0 +1,7 @@ +import { createApp } from '@green-tea/core'; +import { AppModule } from './app.module'; + +const app = createApp({ modules: [AppModule] }); + +Bun.serve({ port: 8000, fetch: app.fetch }); +console.log('🍵 green-tea running on http://localhost:8000'); diff --git a/template/runtimes/bun/tsconfig.json b/template/runtimes/bun/tsconfig.json new file mode 100644 index 0000000..4974940 --- /dev/null +++ b/template/runtimes/bun/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "moduleResolution": "bundler", + "experimentalDecorators": true, + "strict": true, + "skipLibCheck": true + } +} diff --git a/template/starter/deno.json b/template/runtimes/deno/deno.json similarity index 100% rename from template/starter/deno.json rename to template/runtimes/deno/deno.json diff --git a/template/starter/matcha.toml b/template/runtimes/deno/matcha.toml similarity index 100% rename from template/starter/matcha.toml rename to template/runtimes/deno/matcha.toml diff --git a/template/starter/src/main.ts b/template/runtimes/deno/src/main.ts similarity index 100% rename from template/starter/src/main.ts rename to template/runtimes/deno/src/main.ts diff --git a/template/runtimes/node/matcha.toml b/template/runtimes/node/matcha.toml new file mode 100644 index 0000000..0d7f6fa --- /dev/null +++ b/template/runtimes/node/matcha.toml @@ -0,0 +1 @@ +runtime = "node" diff --git a/template/runtimes/node/package.json b/template/runtimes/node/package.json new file mode 100644 index 0000000..69f153d --- /dev/null +++ b/template/runtimes/node/package.json @@ -0,0 +1,8 @@ +{ + "name": "{{project_name}}", + "type": "module", + "private": true, + "scripts": { "dev": "tsx watch src/main.ts" }, + "dependencies": { "@green-tea/core": "^26.7.0-beta.0" }, + "devDependencies": { "tsx": "^4", "typescript": "^5" } +} diff --git a/template/runtimes/node/src/main.ts b/template/runtimes/node/src/main.ts new file mode 100644 index 0000000..c7521ae --- /dev/null +++ b/template/runtimes/node/src/main.ts @@ -0,0 +1,7 @@ +import { createApp } from '@green-tea/core'; +import { AppModule } from './app.module'; + +const app = createApp({ modules: [AppModule] }); + +app.listen(3000); +console.log('🍵 green-tea running on http://localhost:3000'); diff --git a/template/runtimes/node/tsconfig.json b/template/runtimes/node/tsconfig.json new file mode 100644 index 0000000..4a15361 --- /dev/null +++ b/template/runtimes/node/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "esnext", + "moduleResolution": "bundler", + "experimentalDecorators": true, + "strict": true, + "skipLibCheck": true + } +} diff --git a/template/starter/public/index.html b/template/shared/public/index.html similarity index 100% rename from template/starter/public/index.html rename to template/shared/public/index.html diff --git a/template/starter/src/app.module.ts b/template/shared/src/app.module.ts similarity index 100% rename from template/starter/src/app.module.ts rename to template/shared/src/app.module.ts diff --git a/template/starter/src/controllers/home.controller.ts b/template/shared/src/controllers/home.controller.ts similarity index 100% rename from template/starter/src/controllers/home.controller.ts rename to template/shared/src/controllers/home.controller.ts diff --git a/tests/new.rs b/tests/new.rs index 4f34b40..f5d1cb5 100644 --- a/tests/new.rs +++ b/tests/new.rs @@ -16,6 +16,66 @@ fn new_scaffolds_into_named_dir() { assert!(root.join("public/index.html").exists()); } +#[test] +fn new_default_runtime_is_node() { + let d = tempdir().unwrap(); + Command::cargo_bin("matcha") + .unwrap() + .current_dir(d.path()) + .args(["new", "demo"]) + .assert() + .success(); + let root = d.path().join("demo"); + let pkg = std::fs::read_to_string(root.join("package.json")).unwrap(); + assert!(pkg.contains("\"name\": \"demo\"")); + assert!(root.join("tsconfig.json").exists()); + assert!(!root.join("deno.json").exists()); +} + +#[test] +fn new_with_deno_runtime_produces_deno_json() { + let d = tempdir().unwrap(); + Command::cargo_bin("matcha") + .unwrap() + .current_dir(d.path()) + .args(["new", "demo-deno", "--runtime", "deno"]) + .assert() + .success(); + let root = d.path().join("demo-deno"); + assert!(root.join("deno.json").exists()); + assert!(!root.join("package.json").exists()); +} + +#[test] +fn new_with_node_runtime_produces_package_json() { + let d = tempdir().unwrap(); + Command::cargo_bin("matcha") + .unwrap() + .current_dir(d.path()) + .args(["new", "demo", "--runtime", "node"]) + .assert() + .success(); + let root = d.path().join("demo"); + let pkg = std::fs::read_to_string(root.join("package.json")).unwrap(); + assert!(pkg.contains("\"name\": \"demo\"")); + assert!(!root.join("deno.json").exists()); +} + +#[test] +fn new_with_bun_runtime_produces_package_json() { + let d = tempdir().unwrap(); + Command::cargo_bin("matcha") + .unwrap() + .current_dir(d.path()) + .args(["new", "demo-bun", "--runtime", "bun"]) + .assert() + .success(); + let root = d.path().join("demo-bun"); + let pkg = std::fs::read_to_string(root.join("package.json")).unwrap(); + assert!(pkg.contains("\"name\": \"demo-bun\"")); + assert!(!root.join("deno.json").exists()); +} + #[test] fn new_refuses_existing_dir() { let d = tempdir().unwrap();