Skip to content

upsertManagedBlock

upsertManagedBlock(content, __namedParameters): string

Defined in: managed-block.ts:59

Inserts or replaces a block of lines delimited by marker lines, leaving everything outside it untouched.

  • Block present (start … end): replaced in place.
  • No block: appended, separated from existing content by a blank line, with a single trailing newline.
  • start present but end missing (hand-edited or truncated): the block is taken to run to the next blank line, or to the end of the file — so the content after it is kept, not lost. An end marker only closes the block when it comes before that blank line; one further down is left alone.

Line endings follow the existing text (\r\n when it mostly uses them), so a CRLF file stays CRLF. That is also why lines can’t contain blank lines: a blank line is how an unterminated block is told apart from the next section.

Idempotent: calling it again with the same options returns the same text. There are no default markers; callers choose them, and must keep them stable so existing blocks are still found.

string

ManagedBlockOptions

string

when an entry in lines is blank or contains a line break.

import { upsertManagedBlock } from "@theholocron/git-utils";
upsertManagedBlock("node_modules\n", {
start: "# managed: skills",
end: "# end managed: skills",
lines: ["/.agents/"],
});
// "node_modules\n\n# managed: skills\n/.agents/\n# end managed: skills\n"