Default script entrypoints by kind

This commit is contained in:
2026-07-22 18:54:27 +02:00
parent e9f2cdb25d
commit a1c04ff898
2 changed files with 57 additions and 1 deletions

View File

@@ -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();

View File

@@ -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<i32, D::Error>
where
D: Deserializer<'de>,
@@ -532,7 +536,18 @@ impl ScriptFrontmatter {
}
pub fn from_yaml(yaml: &str) -> Result<Self, String> {
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)
}
}