1111 */
1212
1313const assert = require ( "node:assert/strict" ) ;
14- const { execFile } = require ( "node:child_process" ) ;
14+ const { execFile, execFileSync } = require ( "node:child_process" ) ;
1515const {
1616 appendFileSync,
1717 existsSync,
@@ -36,6 +36,13 @@ const {
3636const { Simple } = require ( "release-please/build/src/strategies/simple.js" ) ;
3737const { Changelog } = require ( "release-please/build/src/updaters/changelog.js" ) ;
3838
39+ const LOCAL_PRETTIER_BIN = join (
40+ __dirname ,
41+ "node_modules" ,
42+ ".bin" ,
43+ process . platform === "win32" ? "prettier.cmd" : "prettier" ,
44+ ) ;
45+
3946const execFileAsync = promisify ( execFile ) ;
4047
4148const UNRELEASED_HEADING_PATTERN = / ^ # { 2 , 3 } \[ ? U n r e l e a s e d \] ? [ \t ] * $ / ;
@@ -243,6 +250,29 @@ function findLineOutsideFences(lines, start, predicate) {
243250 return - 1 ;
244251}
245252
253+ function resolvePrettierBin ( env = process . env , fileExists = existsSync ) {
254+ if ( env . PRETTIER_BIN !== undefined && env . PRETTIER_BIN !== "" ) {
255+ return env . PRETTIER_BIN ;
256+ }
257+ if ( fileExists ( LOCAL_PRETTIER_BIN ) ) {
258+ return LOCAL_PRETTIER_BIN ;
259+ }
260+ return "prettier" ;
261+ }
262+
263+ function formatChangelogMarkdown ( content ) {
264+ assert . equal ( typeof content , "string" , "content must be a string" ) ;
265+ return execFileSync (
266+ resolvePrettierBin ( ) ,
267+ [ "--stdin-filepath" , "CHANGELOG.md" ] ,
268+ {
269+ encoding : "utf8" ,
270+ input : content ,
271+ maxBuffer : 16 * 1024 * 1024 ,
272+ } ,
273+ ) ;
274+ }
275+
246276/**
247277 * Replaces release-please's stock CHANGELOG updater. It keeps `## [Unreleased]`
248278 * at the top, clears its draft body after Communique reconciles it into the
@@ -280,7 +310,7 @@ class UnreleasedAwareChangelog {
280310 ( line ) => NEXT_SECTION_PATTERN . test ( line ) ,
281311 ) ;
282312 const rest = nextIndex === - 1 ? "" : lines . slice ( nextIndex ) . join ( "\n" ) ;
283- return joinSections ( head , entry , rest ) ;
313+ return formatChangelogMarkdown ( joinSections ( head , entry , rest ) ) ;
284314 }
285315
286316 // Self-heal a missing Unreleased anchor so Communique has draft space on
@@ -290,9 +320,13 @@ class UnreleasedAwareChangelog {
290320 ) ;
291321 if ( titleIndex !== - 1 ) {
292322 const head = `${ lines . slice ( 0 , titleIndex + 1 ) . join ( "\n" ) } \n\n## [Unreleased]` ;
293- return joinSections ( head , entry , lines . slice ( titleIndex + 1 ) . join ( "\n" ) ) ;
323+ return formatChangelogMarkdown (
324+ joinSections ( head , entry , lines . slice ( titleIndex + 1 ) . join ( "\n" ) ) ,
325+ ) ;
294326 }
295- return joinSections ( "# Changelog\n\n## [Unreleased]" , entry , existing ) ;
327+ return formatChangelogMarkdown (
328+ joinSections ( "# Changelog\n\n## [Unreleased]" , entry , existing ) ,
329+ ) ;
296330 }
297331}
298332
@@ -337,12 +371,16 @@ function formatReleaseOutputs(releases) {
337371
338372function formatPullRequestOutputs ( pullRequests ) {
339373 assert . ok ( Array . isArray ( pullRequests ) , "pullRequests must be an array" ) ;
340- const branches = pullRequests
374+ const prs = pullRequests
341375 . filter ( ( pullRequest ) => pullRequest !== undefined )
342- . map ( ( pullRequest ) => pullRequest . headBranchName ) ;
376+ . map ( ( pullRequest ) => ( {
377+ branch : pullRequest . headBranchName ,
378+ title : pullRequest . title . toString ( ) ,
379+ } ) ) ;
343380 return {
344- prs_created : branches . length > 0 ? "true" : "false" ,
345- pr_branches : branches . join ( " " ) ,
381+ prs_created : prs . length > 0 ? "true" : "false" ,
382+ pr_branches : prs . map ( ( pr ) => pr . branch ) . join ( " " ) ,
383+ pr_metadata : JSON . stringify ( prs ) ,
346384 } ;
347385}
348386
@@ -466,10 +504,12 @@ module.exports = {
466504 buildCommuniqueArgs,
467505 createCommuniqueChangelogNotes,
468506 findLineOutsideFences,
507+ formatChangelogMarkdown,
469508 formatChangelogSection,
470509 formatPullRequestOutputs,
471510 formatReleaseOutputs,
472511 normalizeCommuniqueBody,
512+ resolvePrettierBin,
473513 releasePleaseNotesAreEmpty,
474514 todayIsoDate,
475515} ;
0 commit comments