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:
1 parent
65ecba05d1
commit
2f9b656692
10 files changed
+329
-79
No files matched your search
+34
-12
@@ -8,21 +8,31 @@ use std::path::PathBuf;
|
||||
|
||||
use crate::wire;
|
||||
|
||||
/// Returns `(decorator symbol, method source)` for a known capability, or
|
||||
/// `None` for an unrecognized one.
|
||||
fn handler(cap: &str) -> Option<(&'static str, String)> {
|
||||
/// Returns `(decorator symbol, method source, dedup marker)` for a known
|
||||
/// capability, or `None` for an unrecognized one.
|
||||
///
|
||||
/// The dedup marker is the route-path string literal baked into that
|
||||
/// capability's `handler()` template (e.g. `'/events'`, `'/stream'`,
|
||||
/// `'/data'`). It is unique per capability, unlike the decorator name — both
|
||||
/// `buffer` and any pre-existing handler can legitimately use `@Get` — so
|
||||
/// it's what `run` checks for before inserting, to keep `matcha add <cap>`
|
||||
/// idempotent per capability rather than per decorator.
|
||||
fn handler(cap: &str) -> Option<(&'static str, String, &'static str)> {
|
||||
match cap {
|
||||
"sse" => Some((
|
||||
"Sse",
|
||||
"\n @Sse('/events')\n events() {\n return (async function* () {\n yield { hello: 'world' };\n })();\n }\n".into(),
|
||||
"'/events'",
|
||||
)),
|
||||
"stream" => Some((
|
||||
"Stream",
|
||||
"\n @Stream('/stream')\n stream() {\n return (async function* () {\n yield 'chunk';\n })();\n }\n".into(),
|
||||
"'/stream'",
|
||||
)),
|
||||
"buffer" => Some((
|
||||
"Get",
|
||||
"\n @Get('/data')\n data() {\n return Buffer.from('hello');\n }\n".into(),
|
||||
"'/data'",
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
@@ -38,8 +48,13 @@ fn only_controller() -> std::io::Result<PathBuf> {
|
||||
.collect();
|
||||
match hits.len() {
|
||||
1 => Ok(hits.pop().unwrap()),
|
||||
0 => Err(Error::new(ErrorKind::NotFound, "no *.controller.ts in src/controllers")),
|
||||
_ => Err(Error::other("multiple controllers; specify which (not yet supported)")),
|
||||
0 => Err(Error::new(
|
||||
ErrorKind::NotFound,
|
||||
"no *.controller.ts in src/controllers",
|
||||
)),
|
||||
_ => Err(Error::other(
|
||||
"multiple controllers; specify which (not yet supported)",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,23 +69,30 @@ fn class_name(src: &str) -> Option<String> {
|
||||
}
|
||||
|
||||
pub fn run(cap: &str) -> std::io::Result<()> {
|
||||
let (decorator, method) = handler(cap)
|
||||
let (decorator, method, marker) = handler(cap)
|
||||
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, format!("unknown capability {cap}")))?;
|
||||
let path = only_controller()?;
|
||||
let src = std::fs::read_to_string(&path)?;
|
||||
let class = class_name(&src).ok_or_else(|| Error::other("no exported class found"))?;
|
||||
if src.contains(marker) {
|
||||
println!("→ {cap} already present in {class}");
|
||||
return Ok(());
|
||||
}
|
||||
// wire::add_import merges `decorator` into an existing
|
||||
// `import { ... } from '@green-tea/core';` brace when one is already
|
||||
// present, rather than appending a second import line for the same
|
||||
// module (which would be a duplicate ESM binding — a hard SyntaxError,
|
||||
// not a harmless dupe).
|
||||
let with_import = wire::add_import(&src, decorator, "@green-tea/core");
|
||||
// add_import above assumes a fresh import line; if '@green-tea/core' is
|
||||
// already imported under a shared brace, we still append a second import
|
||||
// line for the new symbol — a duplicate named import is legal TS and tsc
|
||||
// dedups at type level. Merging into the existing brace is deferred past
|
||||
// v1; not worth tree-sitter surgery now.
|
||||
match wire::insert_method(&with_import, &class, &method) {
|
||||
Some(out) => {
|
||||
std::fs::write(&path, out)?;
|
||||
println!("✓ added @{decorator} to {class}");
|
||||
}
|
||||
None => println!("→ could not edit {}; add @{decorator} handler manually", path.display()),
|
||||
None => println!(
|
||||
"→ could not edit {}; add @{decorator} handler manually",
|
||||
path.display()
|
||||
),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user