Skip to content

Add --replacement-file to take the replacement from a file to close #795 - #803

Merged
mathiasrw merged 1 commit into
mainfrom
fix/795
Oct 23, 2026
Merged

mathiasrw merged 1 commit into
mainfrom
fix/795

Conversation

@mathiasrw

Copy link
Copy Markdown
Owner

Closes #795

Stacked on #802.

Change

--replacement-file: the replacement is the path to a file, and the content of the file is used as the replacement.

> rexreplace '\{\{footer\}\}' partials/footer.html 'site/*.html' --replacement-file --trim-file

It is a layer of the replacement, so it combines by order:

Flags The replacement is
--replacement-file the content of the file at the typed path
-R --replacement-file the content of the file at the piped path
--js --replacement-file the content of the file at the path the code gives
> rexreplace '\{\{(\w+)\}\}' '"partials/" + $1 + ".html"' 'site/*.html' --js --replacement-file --trim-file
  • $1 and $& in the content have their meaning, like a typed replacement. --literal-replacement (Add --literal-replacement to use replacement as-is to close #798 #802) uses it as it is. With --js the content is used as it is, like the value of code.
  • The € and § aliases are used in the typed path, not in the content.
  • Each file is read once, also when the code gives the same path for many matches.
  • A file that can not be read stops the run (exit code 3) before any file is changed. With --js the path is only known when the code runs, so then it is an error once for that path, its matches are left as they are, and the run goes on (exit code 2).
  • With --output-match the file is not read, and a 🟡 warning tells so.
  • With --rename the content of the file is the new name.

Trimming

Flag Trims
--trim-pipe (-T) the piped data, as before
--trim-file the content of the replacement file
--trim both

They are apart, as a run can need one and not the other: when the pipe is the content and the replacement comes from a file, the file should lose its line break, but the content should come out as it came in.

Tests

yarn test-ci passes: the demo on Node and Bun (with --replacement-file), and 993 Bun tests. test/engine/replacement-file.test.js, test/engine/trim-file.test.js and test/cli/trim.test.js are new. The README has a "Templates" section.

@mathiasrw
mathiasrw added this pull request to stack #787 October 23, 2026 09:45
@mathiasrw
mathiasrw force-pushed the fix/795 branch 3 times, most recently from 605b4fe to d114998 Compare October 23, 2026 10:09
Base automatically changed from fix/798 to main October 23, 2026 10:10
@mathiasrw
mathiasrw merged commit ce3c5c5 into main Oct 23, 2026
7 checks passed
@mathiasrw
mathiasrw deleted the fix/795 branch October 23, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --replacement-file to take the replacement from a file

1 participant