fix(add): merge imports into existing brace + capability idempotency; fmt

- wire::add_import now merges a new symbol into an already-present
  `import { ... } from '<from>';` brace instead of appending a second,
  colliding import line for the same module (a duplicate ESM binding
  is a hard SyntaxError/TS2300, not a harmless dupe as previously
  assumed). Falls back to appending a new line when the module isn't
  imported yet, or if the brace-merge edit fails the parses_clean
  guard.
- cmd_add::run gains a capability-level idempotency guard, keyed on
  each capability's unique route-path literal, so `matcha add <cap>`
  run twice no longer appends a duplicate handler + route.
- cargo fmt --all across the crate; drop the stale #[allow(dead_code)]
  on wire::parses_clean now that it's demonstrably live.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-11 13:52:21 -05:00
1 parent bc013fde9f
commit 757b729986
10 files changed
+329 -79

No files matched your search

+13 -7
View File
@@ -13,24 +13,30 @@ use crate::wire;
fn controller_stub(name: &str) -> String {
format!(
"import {{ Route, Get }} from '@green-tea/core';\n\n\
"import {{ Route, Get }} from '@green-tea/core';\n\n\
@Route('/{lower}')\nexport class {name}Controller {{\n \
@Get('/')\n list() {{\n return [];\n }}\n}}\n",
lower = name.to_lowercase(), name = name)
lower = name.to_lowercase(),
name = name
)
}
pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> {
match kind {
"controller" => create_controller(name, check),
"module" | "step" => Err(Error::new(ErrorKind::Unsupported,
format!("create {kind} not implemented yet"))),
other => Err(Error::new(ErrorKind::InvalidInput, format!("unknown kind {other}"))),
"module" | "step" => Err(Error::new(
ErrorKind::Unsupported,
format!("create {kind} not implemented yet"),
)),
other => Err(Error::new(
ErrorKind::InvalidInput,
format!("unknown kind {other}"),
)),
}
}
fn create_controller(name: &str, check: bool) -> std::io::Result<()> {
let file = Path::new("src/controllers")
.join(format!("{}.controller.ts", name.to_lowercase()));
let file = Path::new("src/controllers").join(format!("{}.controller.ts", name.to_lowercase()));
std::fs::create_dir_all(file.parent().unwrap())?;
std::fs::write(&file, controller_stub(name))?;
println!("✓ {}", file.display());