fix(create,starters): safe names and no overwrite in create; node starter awaits listen and declares engines
CI / installer (macos-14) (pull_request) Successful in 3s
CI / check (pull_request) Successful in 27s
CI / starter (bun) (pull_request) Successful in 31s
CI / starter (deno) (pull_request) Successful in 33s
CI / starter (edge) (pull_request) Successful in 38s
CI / core-freshness (pull_request) Has been skipped
CI / installer (ubuntu-latest) (pull_request) Successful in 12s
CI / starter (node) (pull_request) Successful in 32s

matcha create derives the class name and its token from what was typed:
current-user becomes CurrentUserStep providing currentUser, where it used
to write a file that did not parse. It refuses names that cannot be a
class, and step or provider names whose token core already provides. It
also refuses to replace an existing file instead of silently discarding
edits.

The node starter awaits app.listen so boot and bind failures stop the
process with their own error, and the node and edge starters declare
engines node >=22 to match core.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-30 21:00:58 -05:00
1 parent 865d87487b
commit a8f1658e4d
6 files changed
+136 -8

No files matched your search

+62 -7
View File
@@ -5,7 +5,7 @@
use std::io::{Error, ErrorKind};
use std::path::Path;
use crate::{entry, wire};
use crate::{entry, naming, wire};
/// A piece that lives inside a module: a file under `src/<subdir>/` plus an
/// entry in one of `@Module`'s arrays.
@@ -60,7 +60,64 @@ pub fn kinds() -> Vec<&'static str> {
.collect()
}
/// Tokens core already owns. A step or provider that provides one fails `createApp` (the framework
/// tokens) or shadows what every route reads from the request (the request tokens).
const TAKEN_TOKENS: &[&str] = &[
"logger", "rooms", "events", "bus", "req", "params", "query", "body", "headers", "cookies",
"inbound", "abort",
];
/// `Users` stays `Users`; `current-user`, `current_user` and `current user` become `CurrentUser`.
/// `None` for anything that cannot become a TypeScript class name.
fn class_name(name: &str) -> Option<String> {
let name = name.trim();
if name.starts_with(|c: char| c.is_ascii_alphabetic())
&& name.chars().all(|c| c.is_ascii_alphanumeric())
{
let (first, rest) = name.split_at(1);
return Some(first.to_ascii_uppercase() + rest);
}
naming::slug(name).map(|s| naming::pascal(&s))
}
/// The token a step or provider named `CurrentUser` provides: `currentUser`.
fn token(class: &str) -> String {
let (first, rest) = class.split_at(1);
first.to_ascii_lowercase() + rest
}
/// Refuses to replace a file the person may already have edited.
fn write_new(file: &Path, contents: String) -> std::io::Result<()> {
if file.exists() {
return Err(Error::new(
ErrorKind::AlreadyExists,
format!(
"{} already exists; matcha create does not overwrite it",
file.display()
),
));
}
std::fs::create_dir_all(file.parent().unwrap())?;
std::fs::write(file, contents)
}
pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> {
let Some(class) = class_name(name) else {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("'{name}' cannot name a class: use letters and digits, starting with a letter (Users, CurrentUser, current-user)"),
));
};
let name = class.as_str();
if (kind == "step" || kind == "provider") && TAKEN_TOKENS.contains(&token(name).as_str()) {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"a {kind} named {name} would provide '{}', which core already provides; pick another name",
token(name)
),
));
}
if kind == "module" {
return create_module(name, check);
}
@@ -87,8 +144,7 @@ fn create_piece(name: &str, check: bool, piece: &Piece) -> std::io::Result<()> {
let file = Path::new("src")
.join(subdir)
.join(format!("{lower}.{ext}.ts"));
std::fs::create_dir_all(file.parent().unwrap())?;
std::fs::write(&file, (piece.stub)(name))?;
write_new(&file, (piece.stub)(name))?;
println!("✓ {}", file.display());
let symbol = format!("{name}{suffix}");
@@ -117,8 +173,7 @@ fn create_piece(name: &str, check: bool, piece: &Piece) -> std::io::Result<()> {
fn create_module(name: &str, check: bool) -> std::io::Result<()> {
let lower = name.to_lowercase();
let file = Path::new("src").join(format!("{lower}.module.ts"));
std::fs::create_dir_all(file.parent().unwrap())?;
std::fs::write(&file, module_stub(name))?;
write_new(&file, module_stub(name))?;
println!("✓ {}", file.display());
let symbol = format!("{name}Module");
@@ -164,14 +219,14 @@ fn controller_stub(name: &str) -> String {
}
fn step_stub(name: &str) -> String {
let lower = name.to_lowercase();
let lower = token(name);
format!(
"import {{ Step }} from '@green-tea/core';\n\n@Step({{ provides: '{lower}', needs: [] }})\nexport class {name}Step {{\n run(ctx: Record<string, unknown>) {{\n return {{ {lower}: null }};\n }}\n}}\n"
)
}
fn provider_stub(name: &str) -> String {
let lower = name.to_lowercase();
let lower = token(name);
format!(
"import {{ Provider }} from '@green-tea/core';\n\n@Provider({{ provides: '{lower}' }})\nexport class {name}Provider {{\n provide() {{\n return {{ {lower}: null }};\n }}\n}}\n"
)