feat(graph): draw the graph, and stop scaffolding a two-month-old core

The CLI's last feature work was 12 July; core has shipped 26.8 and 26.9 since.
Two consequences, fixed together because they share an entry-file convention.

Every runtime overlay pinned `^26.7.0-beta.0`. Core is prerelease-only, and
npm's semver excludes a prerelease from any range whose comparators carry a
different major.minor.patch — so that caret is not a loose pin, it is a pin to
the oldest matching prerelease that nothing can move off. Every project
`matcha new` has produced since July installed 26.7.0-beta.0 and could not
upgrade: no logger, no request budget, no resumable `@Sse` id, and none of the
crashes 26.9 fixed. The pin is now exact, lives in one `CORE_VERSION` const,
and Deno resolves through JSR — which is core's native path for Deno and the
one 26.9 repaired.

Nothing caught that because CI never ran JavaScript. `cargo test` proves the CLI
writes the files it means to write; it cannot prove those files describe a
project the current core will run. `scripts/starter-smoke.sh` scaffolds,
installs, type-checks, boots, curls `/` and the `/zen` stream, and introspects a
real starter per runtime; a weekly job fails when the pin falls behind the
`beta` dist-tag. The smoke immediately found a second defect: the bun overlay
carried no Bun types, so `tsc --noEmit` — and therefore `matcha create --check`
— failed on every bun project the CLI has ever scaffolded.

`matcha graph` and `matcha explain <pattern>` close the widest gap in the
surface: the framework's claim is that an API is a graph rather than a
middleware chain, `toMermaid`/`toDOT`/`graph`/`explain` have been exported all
along, and the CLI could not draw any of it. Both reach the app through
`app.ready()`, which resolves the graph and is documented as deliberately not
running provider factories — asking for a diagram must not open your database
connections. Formatting lives in the generated script rather than in Rust,
which keeps serde out for output `console.log` already has the values for.

That needed an app the CLI can import without binding a port, so the starter
now composes the graph in `src/app.ts` and leaves `src/main.ts` holding only
the serve call. `create module` follows, preferring `src/app.ts` and falling
back to `src/main.ts` so projects scaffolded before the split keep working.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012x1UPw987zNSxjH2DxHy2C
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-09-05 14:30:19 -05:00
1 parent 8910fe22ee
commit 827e6863ad
21 files changed
+747 -27

No files matched your search

+254
View File
@@ -0,0 +1,254 @@
//! `matcha graph` and `matcha explain <pattern>` — introspection of the
//! dependency graph the framework builds from `needs`/`provides`.
//!
//! Both work the same way: write a short script next to the project, import its
//! entry file, `await app.ready()`, print, delete the script. `ready()` is the
//! reason this is cheap and safe to run — it resolves the graph and is
//! documented as deliberately *not* running provider factories, so drawing a
//! diagram does not open your database connections. It is also required rather
//! than optional: on a mesh app the graph is not knowable until its teapots
//! have been asked, and `graph()`/`explain()` throw until then.
//!
//! The formatting lives in the generated script rather than here on purpose.
//! Rust would have to parse the JSON back to lay out a chain, and nothing about
//! that layout needs Rust — `console.log` already has the values.
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use crate::entry;
use crate::runtime::{self, Runtime};
/// Written into the project root so the runtime resolves `@green-tea/core` from
/// the project's own `node_modules`/import map, then removed. Dotted so it is
/// invisible to a `src/**` glob if a run dies before cleanup.
const SCRIPT_NAME: &str = ".matcha-introspect.ts";
/// `matcha graph` — the whole graph, as Mermaid, Graphviz DOT, or JSON.
pub fn graph(
dir: &Path,
format: &str,
entry_override: Option<&str>,
out: Option<&str>,
) -> std::io::Result<()> {
let print = match format {
"mermaid" => "console.log(app.toMermaid());",
"dot" => "console.log(app.toDOT());",
"json" => "console.log(JSON.stringify(app.graph(), null, 2));",
other => {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("unknown format '{other}' (mermaid, dot, json)"),
))
}
};
emit(dir, entry_override, out, print)
}
/// `matcha explain <pattern>` — one route's chain, in execution order, with
/// each node's needs and provides.
///
/// The argument is the route *pattern* as declared (`/users/:id`), not a
/// concrete URL: `explainRoute` matches the pattern string exactly. A miss
/// therefore lists what is actually registered, which is the only useful thing
/// to say to someone who just guessed wrong.
pub fn explain(
dir: &Path,
route: &str,
entry_override: Option<&str>,
out: Option<&str>,
) -> std::io::Result<()> {
let route = js_string(route);
let body = format!(
r#"let explained;
try {{
explained = app.explain({route});
}} catch {{
const patterns = app.graph().routes.map((r) => ` ${{r.method.padEnd(6)}} ${{r.pattern}}`);
console.error(`no route matching pattern {route}\n\nregistered patterns:\n${{patterns.join('\n')}}`);
process.exit(1);
}}
const lines = [`${{explained.method}} ${{explained.pattern}} [${{explained.transport}}]`];
const width = Math.max(...explained.chain.map((n) => n.kind.length));
for (const node of explained.chain) {{
const needs = node.needs.length ? ` needs: ${{node.needs.join(', ')}}` : '';
const provides = node.provides.length ? ` provides: ${{node.provides.join(', ')}}` : '';
lines.push(` ${{node.kind.padEnd(width)}} ${{node.name}}${{needs}}${{provides}} (${{node.origin}})`);
}}
console.log(lines.join('\n'));"#
);
emit(dir, entry_override, out, &body)
}
/// Resolves the entry, generates the script around `body`, runs it under the
/// detected runtime, and routes its stdout to a file or to ours.
fn emit(
dir: &Path,
entry_override: Option<&str>,
out: Option<&str>,
body: &str,
) -> std::io::Result<()> {
let entry_path = match entry_override {
Some(explicit) => {
let p = dir.join(explicit);
if !p.exists() {
return Err(Error::new(ErrorKind::NotFound, format!("no such entry: {explicit}")));
}
PathBuf::from(explicit)
}
None => entry::find(dir).map(|p| p.strip_prefix(dir).unwrap_or(&p).to_path_buf()).ok_or_else(|| {
Error::new(
ErrorKind::NotFound,
format!(
"no {} found — pass --entry <file>. It must export the app: `export const app = createApp(...)`",
entry::candidates()
),
)
})?,
};
let rt = runtime::detect(dir).ok_or_else(|| {
Error::new(
ErrorKind::NotFound,
"no runtime detected (need deno.json, bun.lock, or package.json)",
)
})?;
let script_path = dir.join(SCRIPT_NAME);
std::fs::write(&script_path, script(&entry_path, body))?;
let result = run(rt, dir, SCRIPT_NAME);
std::fs::remove_file(&script_path).ok();
let output = result?;
// The child has already written its own diagnosis to the inherited stderr —
// a type error, a boot failure, or the script's own "no route matching
// pattern" with the registered ones listed. Wrapping that in a second
// `error: npx exited with 1` adds a line and no information, so the exit
// code is relayed and nothing is said on top of it.
if !output.status.success() {
std::process::exit(output.status.code().unwrap_or(1));
}
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
match out {
Some(file) => {
std::fs::write(dir.join(file), &stdout)?;
println!("✓ {file}");
}
None => print!("{stdout}"),
}
Ok(())
}
/// The generated script: import the app, resolve the graph, run `body`.
fn script(entry_path: &Path, body: &str) -> String {
format!(
"// generated by matcha; safe to delete\nimport {{ app }} from {};\n\nif (!app) {{\n console.error('{} does not export `app` — matcha needs `export const app = createApp(...)`');\n process.exit(1);\n}}\nawait app.ready();\n{body}\n",
js_string(&specifier(entry_path)),
entry_path.display(),
)
}
/// Runs `script` under `rt` in `dir`. The child's stderr is inherited rather
/// than captured: a type error or a boot failure is the reason someone runs
/// this, and it should reach them as it happens.
fn run(rt: Runtime, dir: &Path, script: &str) -> std::io::Result<std::process::Output> {
let (prog, args) = runner(rt, script);
let child = Command::new(prog)
.args(&args)
.current_dir(dir)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.map_err(|e| Error::new(e.kind(), format!("could not run {prog}: {e}")))?;
child.wait_with_output()
}
fn runner(rt: Runtime, script: &str) -> (&'static str, Vec<String>) {
let s = script.to_string();
match rt {
Runtime::Node => ("npx", vec!["tsx".into(), s]),
// `ready()` opens no provider connections, but a mesh app does reach its
// teapots there, so net is part of resolving the graph rather than extra.
Runtime::Deno => (
"deno",
vec![
"run".into(),
"--allow-read".into(),
"--allow-env".into(),
"--allow-net".into(),
s,
],
),
Runtime::Bun => ("bun", vec!["run".into(), s]),
}
}
/// A project-root-relative path as an ES module specifier: forward slashes,
/// always `./`-prefixed, extension kept so Deno resolves it without leaning on
/// `sloppy-imports`.
fn specifier(path: &Path) -> String {
let raw = path.to_string_lossy().replace('\\', "/");
format!("./{}", raw.strip_prefix("./").unwrap_or(&raw))
}
/// Single-quoted JS string literal. Route patterns reach this from argv, so the
/// quote and the backslash have to be escaped rather than assumed absent.
fn js_string(value: &str) -> String {
let mut out = String::with_capacity(value.len() + 2);
out.push('\'');
for ch in value.chars() {
match ch {
'\\' => out.push_str("\\\\"),
'\'' => out.push_str("\\'"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
_ => out.push(ch),
}
}
out.push('\'');
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn specifier_is_dot_slash_prefixed_with_extension() {
assert_eq!(specifier(Path::new("src/app.ts")), "./src/app.ts");
assert_eq!(specifier(Path::new("./src/app.ts")), "./src/app.ts");
}
#[test]
fn js_string_escapes_quote_and_backslash() {
assert_eq!(js_string("a'b"), r"'a\'b'");
assert_eq!(js_string(r"a\b"), r"'a\\b'");
}
#[test]
fn script_imports_the_entry_and_awaits_ready() {
let out = script(Path::new("src/app.ts"), "console.log(1);");
assert!(out.contains("import { app } from './src/app.ts';"));
assert!(out.contains("await app.ready();"));
assert!(out.contains("console.log(1);"));
}
#[test]
fn node_runs_through_tsx() {
assert_eq!(
runner(Runtime::Node, ".matcha-introspect.ts"),
(
"npx",
vec!["tsx".to_string(), ".matcha-introspect.ts".to_string()]
)
);
}
#[test]
fn unknown_format_is_rejected() {
let d = tempfile::tempdir().unwrap();
let err = graph(d.path(), "svg", None, None).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidInput);
}
}