Files
indiekit-server/scripts/patch-endpoint-posts-ai-cleanup.mjs
Sven 3ab6c4caa2 fix(patches): silence false-positive warnings for beta.41 native features
Four patch scripts were warning when they couldn't find their target snippets
in @indiekit/endpoint-posts beta.41, because beta.41 already ships those
features natively:

- patch-endpoint-posts-ai-cleanup: beta.41 form.js has native AI field
  cleanup — detect via "ai-text-level" presence and skip silently
- patch-endpoint-posts-search-tags: beta.41 posts.js/posts.njk have native
  filter/sort/search — detect via buildFilterQuery / posts-filter-row
- patch-endpoint-posts-uid-lookup: beta.41 utils.js uses direct MongoDB
  queries (getPostProperties) — skip silently
- patch-preset-eleventy-ai-frontmatter: add v5 block matching the new
  upstream structure (mpUrl + URL pathname normalization)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 23:36:55 +01:00

96 lines
2.7 KiB
JavaScript

import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@rmdes/indiekit-endpoint-posts/lib/controllers/form.js",
"node_modules/@indiekit/endpoint-posts/lib/controllers/form.js",
"node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-posts/lib/controllers/form.js",
"node_modules/@indiekit/indiekit/node_modules/@indiekit/endpoint-posts/lib/controllers/form.js",
];
const marker = "Remove empty AI metadata fields so Micropub payload stays lean.";
const oldSnippet = [
" // Easy MDE appends `image` value to formData for last image uploaded",
" delete values.image;",
"",
" const mf2 = jf2ToMf2({ properties: sanitise(values) });",
].join("\n");
const newSnippet = [
" // Easy MDE appends `image` value to formData for last image uploaded",
" delete values.image;",
"",
" // Remove empty AI metadata fields so Micropub payload stays lean.",
" for (const key of [",
" \"aiTextLevel\",",
" \"aiCodeLevel\",",
" \"aiTools\",",
" \"aiDescription\",",
" \"ai-text-level\",",
" \"ai-code-level\",",
" \"ai-tools\",",
" \"ai-description\",",
" ]) {",
" if (",
" values[key] === undefined ||",
" values[key] === null ||",
" String(values[key]).trim() === \"\"",
" ) {",
" delete values[key];",
" }",
" }",
"",
" const mf2 = jf2ToMf2({ properties: sanitise(values) });",
].join("\n");
async function exists(filePath) {
try {
await access(filePath);
return true;
} catch {
return false;
}
}
let checked = 0;
let patched = 0;
for (const filePath of candidates) {
if (!(await exists(filePath))) {
continue;
}
checked += 1;
const source = await readFile(filePath, "utf8");
if (source.includes(marker)) {
continue;
}
if (!source.includes(oldSnippet)) {
// Beta.41+ has native AI field cleanup — skip silently
if (source.includes('"ai-text-level"') && source.includes('"ai-code-level"')) {
continue;
}
console.warn(
`[postinstall] Skipping endpoint-posts AI cleanup patch for ${filePath}: upstream format changed`,
);
continue;
}
const updated = source.replace(oldSnippet, newSnippet);
await writeFile(filePath, updated, "utf8");
patched += 1;
}
if (checked === 0) {
console.log("[postinstall] No endpoint-posts form controller files found");
} else if (patched === 0) {
console.log("[postinstall] endpoint-posts AI cleanup patch already applied");
} else {
console.log(
`[postinstall] Patched endpoint-posts AI cleanup in ${patched} file(s)`,
);
}