feat(create): implement step, provider, and module generators
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+166
-30
No files matched your search
+1
-1
@@ -21,7 +21,7 @@ pub enum Command {
|
||||
Run,
|
||||
/// Generate a piece and auto-wire it into the module
|
||||
Create {
|
||||
#[arg(value_parser = ["module", "controller", "step"])]
|
||||
#[arg(value_parser = ["module", "controller", "step", "provider"])]
|
||||
kind: String,
|
||||
name: String,
|
||||
#[arg(long)]
|
||||
|
||||
+111
-29
@@ -1,33 +1,42 @@
|
||||
//! `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, check),
|
||||
"module" | "step" => Err(Error::new(
|
||||
ErrorKind::Unsupported,
|
||||
format!("create {kind} not implemented yet"),
|
||||
)),
|
||||
"controller" => create_piece(
|
||||
name,
|
||||
check,
|
||||
"controllers",
|
||||
"controller",
|
||||
"Controller",
|
||||
"controllers",
|
||||
controller_stub(name),
|
||||
),
|
||||
"step" => create_piece(
|
||||
name,
|
||||
check,
|
||||
"steps",
|
||||
"step",
|
||||
"Step",
|
||||
"steps",
|
||||
step_stub(name),
|
||||
),
|
||||
"provider" => create_piece(
|
||||
name,
|
||||
check,
|
||||
"providers",
|
||||
"provider",
|
||||
"Provider",
|
||||
"providers",
|
||||
provider_stub(name),
|
||||
),
|
||||
"module" => create_module(name, check),
|
||||
other => Err(Error::new(
|
||||
ErrorKind::InvalidInput,
|
||||
format!("unknown kind {other}"),
|
||||
@@ -35,32 +44,78 @@ pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_controller(name: &str, check: bool) -> std::io::Result<()> {
|
||||
let file = Path::new("src/controllers").join(format!("{}.controller.ts", name.to_lowercase()));
|
||||
/// Writes a piece file under `src/<subdir>/` and wires it into the `@Module`
|
||||
/// in `src/app.module.ts` (`module_key` array). Emit-only + hint on failure.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn create_piece(
|
||||
name: &str,
|
||||
check: bool,
|
||||
subdir: &str,
|
||||
ext: &str,
|
||||
suffix: &str,
|
||||
module_key: &str,
|
||||
stub: String,
|
||||
) -> std::io::Result<()> {
|
||||
let lower = name.to_lowercase();
|
||||
let file = Path::new("src")
|
||||
.join(subdir)
|
||||
.join(format!("{lower}.{ext}.ts"));
|
||||
std::fs::create_dir_all(file.parent().unwrap())?;
|
||||
std::fs::write(&file, controller_stub(name))?;
|
||||
std::fs::write(&file, stub)?;
|
||||
println!("✓ {}", file.display());
|
||||
|
||||
let symbol = format!("{name}Controller");
|
||||
let from = format!("./controllers/{}.controller", name.to_lowercase());
|
||||
let symbol = format!("{name}{suffix}");
|
||||
let from = format!("./{subdir}/{lower}.{ext}");
|
||||
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(());
|
||||
return maybe_check(check);
|
||||
}
|
||||
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) {
|
||||
match wire::add_to_module_array(&imported, module_key, &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");
|
||||
println!("→ could not auto-wire; add {symbol} to {module_key}[] in src/app.module.ts")
|
||||
}
|
||||
}
|
||||
maybe_check(check)
|
||||
}
|
||||
|
||||
/// Writes `src/<name>.module.ts` and registers it in the `createApp({ modules })`
|
||||
/// call in `src/main.ts`. Emit-only + hint on failure.
|
||||
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))?;
|
||||
println!("✓ {}", file.display());
|
||||
|
||||
let symbol = format!("{name}Module");
|
||||
let from = format!("./{lower}.module");
|
||||
let main_path = Path::new("src/main.ts");
|
||||
if !main_path.exists() {
|
||||
println!("→ register {symbol} in createApp({{ modules }}) manually (no src/main.ts found)");
|
||||
return maybe_check(check);
|
||||
}
|
||||
let src = std::fs::read_to_string(main_path)?;
|
||||
let imported = wire::add_import(&src, &symbol, &from);
|
||||
match wire::add_to_createapp_modules(&imported, &symbol) {
|
||||
Some(wired) => {
|
||||
std::fs::write(main_path, wired)?;
|
||||
println!("✓ registered {symbol} in createApp");
|
||||
}
|
||||
None => println!(
|
||||
"→ could not auto-register; add {symbol} to createApp modules[] in src/main.ts"
|
||||
),
|
||||
}
|
||||
maybe_check(check)
|
||||
}
|
||||
|
||||
fn maybe_check(check: bool) -> std::io::Result<()> {
|
||||
if check {
|
||||
match crate::check::run(Path::new(".")) {
|
||||
Ok(true) => println!("✓ type-check passed"),
|
||||
@@ -68,6 +123,33 @@ fn create_controller(name: &str, check: bool) -> std::io::Result<()> {
|
||||
Err(e) => println!("⚠ could not run type-check: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn controller_stub(name: &str) -> String {
|
||||
let lower = name.to_lowercase();
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
fn step_stub(name: &str) -> String {
|
||||
let lower = name.to_lowercase();
|
||||
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();
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
fn module_stub(name: &str) -> String {
|
||||
let lower = name.to_lowercase();
|
||||
format!(
|
||||
"import {{ Module }} from '@green-tea/core';\n\n@Module({{ mountpoint: '/{lower}', controllers: [], providers: [], steps: [] }})\nexport class {name}Module {{}}\n"
|
||||
)
|
||||
}
|
||||
@@ -30,3 +30,57 @@ fn create_controller_writes_and_wires() {
|
||||
assert!(module.contains("UsersController"));
|
||||
assert!(module.contains("import { UsersController }"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_step_writes_and_wires() {
|
||||
let d = tempdir().unwrap();
|
||||
seed_module(d.path()); // writes src/app.module.ts with `@Module({ controllers: [] })`
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["create", "step", "Pasito"])
|
||||
.assert()
|
||||
.success();
|
||||
assert!(d.path().join("src/steps/pasito.step.ts").exists());
|
||||
let m = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap();
|
||||
assert!(m.contains("PasitoStep"));
|
||||
assert!(m.contains("import { PasitoStep }"));
|
||||
assert!(m.contains("steps: [PasitoStep]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_provider_writes_and_wires() {
|
||||
let d = tempdir().unwrap();
|
||||
seed_module(d.path());
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["create", "provider", "Config"])
|
||||
.assert()
|
||||
.success();
|
||||
assert!(d.path().join("src/providers/config.provider.ts").exists());
|
||||
let m = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap();
|
||||
assert!(m.contains("providers: [ConfigProvider]"));
|
||||
assert!(m.contains("import { ConfigProvider }"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_module_writes_and_registers_in_createapp() {
|
||||
let d = tempdir().unwrap();
|
||||
std::fs::create_dir_all(d.path().join("src")).unwrap();
|
||||
std::fs::write(
|
||||
d.path().join("src/main.ts"),
|
||||
"import { createApp } from '@green-tea/core';\nimport { AppModule } from './app.module';\n\nconst app = createApp({ modules: [AppModule] });\n",
|
||||
)
|
||||
.unwrap();
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["create", "module", "Users"])
|
||||
.assert()
|
||||
.success();
|
||||
assert!(d.path().join("src/users.module.ts").exists());
|
||||
let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap();
|
||||
assert!(main.contains("AppModule, UsersModule"));
|
||||
assert!(main.contains("import { UsersModule }"));
|
||||
}
|
||||
Reference in new issue
Block a user