From a1c04ff8987d805fd69a998769eafa1c6ece9254 Mon Sep 17 00:00:00 2001 From: Chili Palmer Date: Wed, 22 Jul 2026 18:54:27 +0200 Subject: [PATCH] Default script entrypoints by kind --- crates/bds-core/src/engine/script_rebuild.rs | 41 ++++++++++++++++++++ crates/bds-core/src/util/frontmatter.rs | 17 +++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/crates/bds-core/src/engine/script_rebuild.rs b/crates/bds-core/src/engine/script_rebuild.rs index 2f4c40e..69840ee 100644 --- a/crates/bds-core/src/engine/script_rebuild.rs +++ b/crates/bds-core/src/engine/script_rebuild.rs @@ -185,6 +185,47 @@ end assert_eq!(script.file_path, "scripts/my-macro.lua"); } + #[test] + fn rebuild_defaults_missing_entrypoint_from_script_kind() { + let (db, dir) = setup(); + let scripts_dir = dir.path().join("scripts"); + fs::create_dir_all(&scripts_dir).unwrap(); + + for (id, kind, expected_entrypoint) in [ + ("legacy-macro", "macro", "render"), + ("legacy-utility", "utility", "main"), + ("legacy-transform", "transform", "main"), + ] { + let content = format!( + "---\nid: \"{id}\"\nslug: \"{id}\"\ntitle: \"{id}\"\nkind: \"{kind}\"\nenabled: true\nversion: 1\ncreatedAt: \"2024-01-01T00:00:00.000Z\"\nupdatedAt: \"2024-01-01T00:00:00.000Z\"\n---\nfunction {expected_entrypoint}() end\n" + ); + fs::write(scripts_dir.join(format!("{id}.lua")), content).unwrap(); + } + + let report = rebuild_scripts_from_filesystem(db.conn(), dir.path(), "p1").unwrap(); + + assert_eq!(report.created, 3); + assert!(report.errors.is_empty(), "errors: {:?}", report.errors); + assert_eq!( + qs::get_script_by_id(db.conn(), "legacy-macro") + .unwrap() + .entrypoint, + "render" + ); + assert_eq!( + qs::get_script_by_id(db.conn(), "legacy-utility") + .unwrap() + .entrypoint, + "main" + ); + assert_eq!( + qs::get_script_by_id(db.conn(), "legacy-transform") + .unwrap() + .entrypoint, + "main" + ); + } + #[test] fn rebuild_updates_existing() { let (db, dir) = setup(); diff --git a/crates/bds-core/src/util/frontmatter.rs b/crates/bds-core/src/util/frontmatter.rs index 16ddc89..59f8921 100644 --- a/crates/bds-core/src/util/frontmatter.rs +++ b/crates/bds-core/src/util/frontmatter.rs @@ -126,6 +126,10 @@ fn default_entrypoint() -> String { "render".to_owned() } +fn default_entrypoint_for_kind(kind: &str) -> String { + if kind == "macro" { "render" } else { "main" }.to_owned() +} + fn deserialize_version<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, @@ -532,7 +536,18 @@ impl ScriptFrontmatter { } pub fn from_yaml(yaml: &str) -> Result { - serde_yaml::from_str(yaml).map_err(|error| format!("YAML parse error: {error}")) + let value: serde_yaml::Value = + serde_yaml::from_str(yaml).map_err(|error| format!("YAML parse error: {error}"))?; + let entrypoint_key = serde_yaml::Value::String("entrypoint".to_owned()); + let entrypoint_is_missing = value + .as_mapping() + .is_none_or(|mapping| !mapping.contains_key(&entrypoint_key)); + let mut frontmatter: Self = + serde_yaml::from_value(value).map_err(|error| format!("YAML parse error: {error}"))?; + if entrypoint_is_missing { + frontmatter.entrypoint = default_entrypoint_for_kind(&frontmatter.kind); + } + Ok(frontmatter) } }