feat: matcha create controller with tree-sitter auto-wire + emit-only fallback
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 parent
49909ed821
commit
81af7c1c2a
4 files changed
+88
-3
No files matched your search
@@ -0,0 +1,58 @@
|
||||
//! `matcha create <kind> <Name>` — generate a stub file for `<kind>` and
|
||||
//! auto-wire it into `src/app.module.ts` via the pure text-editing helpers
|
||||
//! in `wire`.
|
||||
//!
|
||||
//! `module` and `step` are intentionally stubbed as "not implemented yet"
|
||||
//! for v1 — they will reuse the same `wire` primitives as `controller` in a
|
||||
//! fast follow.
|
||||
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::wire;
|
||||
|
||||
fn controller_stub(name: &str) -> String {
|
||||
format!(
|
||||
"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)
|
||||
}
|
||||
|
||||
pub fn run(kind: &str, name: &str, _check: bool) -> std::io::Result<()> {
|
||||
match kind {
|
||||
"controller" => create_controller(name),
|
||||
"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) -> std::io::Result<()> {
|
||||
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());
|
||||
|
||||
let symbol = format!("{name}Controller");
|
||||
let from = format!("./controllers/{}.controller", name.to_lowercase());
|
||||
let module_path = Path::new("src/app.module.ts");
|
||||
if !module_path.exists() {
|
||||
println!("→ add {symbol} to your @Module manually (no src/app.module.ts found)");
|
||||
return Ok(());
|
||||
}
|
||||
let src = std::fs::read_to_string(module_path)?;
|
||||
let imported = wire::add_import(&src, &symbol, &from);
|
||||
match wire::add_to_module_array(&imported, "controllers", &symbol) {
|
||||
Some(wired) => {
|
||||
std::fs::write(module_path, wired)?;
|
||||
println!("✓ wired {symbol} into AppModule");
|
||||
}
|
||||
None => {
|
||||
// agreed fallback: emit-only, do NOT corrupt the module
|
||||
println!("→ could not auto-wire; add {symbol} to controllers[] in src/app.module.ts");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+7
-1
@@ -1,4 +1,5 @@
|
||||
mod cli;
|
||||
mod cmd_create;
|
||||
mod cmd_new;
|
||||
mod cmd_run;
|
||||
mod runtime;
|
||||
@@ -23,7 +24,12 @@ fn main() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Create { .. } => eprintln!("create: not yet implemented"),
|
||||
Command::Create { kind, name, check } => {
|
||||
if let Err(e) = cmd_create::run(&kind, &name, check) {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Add { .. } => eprintln!("add: not yet implemented"),
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,6 @@ fn has_error(node: Node) -> bool {
|
||||
/// Inserts `import { <symbol> } from '<from>';` after the last top-level
|
||||
/// import statement. No-op (returns `src` unchanged) if the exact import
|
||||
/// line is already present, making repeated calls idempotent.
|
||||
#[allow(dead_code)]
|
||||
pub fn add_import(src: &str, symbol: &str, from: &str) -> String {
|
||||
let line = format!("import {{ {symbol} }} from '{from}';");
|
||||
if src.contains(&line) {
|
||||
@@ -84,7 +83,6 @@ pub fn add_import(src: &str, symbol: &str, from: &str) -> String {
|
||||
/// original text untouched — when the edited text fails to re-parse clean
|
||||
/// (an `ERROR`/missing node appears), which is also what happens when `key`
|
||||
/// doesn't exist in the module object at all (the splice can't be located).
|
||||
#[allow(dead_code)]
|
||||
pub fn add_to_module_array(src: &str, key: &str, symbol: &str) -> Option<String> {
|
||||
let tree = parser().parse(src, None).unwrap();
|
||||
let (open, close, existing) = find_array(&tree.root_node(), src, key)?;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
use assert_cmd::Command;
|
||||
use tempfile::tempdir;
|
||||
|
||||
fn seed_module(dir: &std::path::Path) {
|
||||
std::fs::create_dir_all(dir.join("src/controllers")).unwrap();
|
||||
std::fs::write(dir.join("src/app.module.ts"),
|
||||
"import { Module } from '@green-tea/core';\n\n\
|
||||
@Module({ controllers: [] })\nexport class AppModule {}\n").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_controller_writes_and_wires() {
|
||||
let d = tempdir().unwrap();
|
||||
seed_module(d.path());
|
||||
Command::cargo_bin("matcha").unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["create", "controller", "Users"]).assert().success();
|
||||
|
||||
assert!(d.path().join("src/controllers/users.controller.ts").exists());
|
||||
let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap();
|
||||
assert!(module.contains("UsersController"));
|
||||
assert!(module.contains("import { UsersController }"));
|
||||
}
|
||||
Reference in new issue
Block a user