@@ -11,56 +11,65 @@ No user facing changes.
1111
1212` ;
1313
14- /** Returns today's date formatted as `DD Mon YYYY`. */
15- export function getTodayString ( ) : string {
16- const today = new Date ( ) ;
14+ /** Returns `date` formatted as `DD Mon YYYY`. */
15+ export function getReleaseDateString ( today : Date = new Date ( ) ) : string {
1716 return today . toLocaleDateString ( "en-GB" , {
1817 day : "2-digit" ,
1918 month : "short" ,
2019 year : "numeric" ,
2120 } ) ;
2221}
2322
24- /**
25- * Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
26- * and today's date.
27- */
28- export function setVersionAndDate (
29- options : DryRunOption ,
30- version : string ,
23+ export interface OpenChangelogOptions {
24+ initChangelog ?: boolean ;
25+ }
26+
27+ export function withChangelog (
28+ transformer : ( contents : string ) => string ,
29+ options : DryRunOption & OpenChangelogOptions ,
3130) : void {
3231 let content : string ;
3332
34- if ( fs . existsSync ( CHANGELOG_FILE ) ) {
35- content = fs . readFileSync ( CHANGELOG_FILE , "utf8" ) ;
36- } else {
33+ if ( options . initChangelog && ! fs . existsSync ( CHANGELOG_FILE ) ) {
3734 content = EMPTY_CHANGELOG ;
35+ } else {
36+ content = fs . readFileSync ( CHANGELOG_FILE , "utf8" ) ;
3837 }
3938
40- const versionAndDate = `${ version } - ${ getTodayString ( ) } ` ;
41- const newContent = content . replace ( "[UNRELEASED]" , versionAndDate ) ;
42-
4339 if ( ! options . dryRun ) {
44- fs . writeFileSync ( CHANGELOG_FILE , newContent , "utf8" ) ;
40+ fs . writeFileSync ( CHANGELOG_FILE , transformer ( content ) , "utf8" ) ;
4541 } else {
46- console . info (
47- `[DRY RUN] Would have replaced '[UNRELEASED]' in '${ CHANGELOG_FILE } ' with '${ versionAndDate } '.` ,
48- ) ;
42+ console . info ( `[DRY RUN] Would have written updated changelog.` ) ;
4943 }
5044}
5145
46+ /**
47+ * Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
48+ * and today's date.
49+ */
50+ export function setVersionAndDate (
51+ version : string ,
52+ content : string ,
53+ date : Date = new Date ( ) ,
54+ ) : string {
55+ const versionAndDate = `${ version } - ${ getReleaseDateString ( date ) } ` ;
56+ return content . replace ( "[UNRELEASED]" , versionAndDate ) ;
57+ }
58+
5259/**
5360 * Processes changelog entries for a backport, converting version references
5461 * from the source major version to the target major version and filtering
5562 * entries that only apply to newer versions.
5663 */
5764export function processChangelogForBackports (
58- options : DryRunOption ,
5965 sourceBranchMajorVersion : string ,
6066 targetBranchMajorVersion : string ,
61- ) : void {
62- const content = fs . readFileSync ( CHANGELOG_FILE , "utf8" ) ;
67+ content : string ,
68+ ) : string {
6369 const lines = content . split ( "\n" ) ;
70+
71+ // Changelog entries can use the following format to indicate
72+ // that they only apply to newer versions
6473 const someVersionsOnlyRegex = / \[ v ( \d + ) \+ o n l y \] / ;
6574
6675 let output = "" ;
@@ -86,6 +95,7 @@ export function processChangelogForBackports(
8695 }
8796
8897 // Process remaining lines.
98+ // `foundContent` tracks whether we hit two headings in a row
8999 let foundContent = false ;
90100 output += "\n" ;
91101
@@ -121,11 +131,5 @@ export function processChangelogForBackports(
121131 }
122132 }
123133
124- if ( ! options . dryRun ) {
125- fs . writeFileSync ( CHANGELOG_FILE , output , "utf8" ) ;
126- } else {
127- console . info (
128- `[DRY RUN] Would have written updated changelog to replace 'v${ sourceBranchMajorVersion } ' with 'v${ targetBranchMajorVersion } '.` ,
129- ) ;
130- }
134+ return output ;
131135}
0 commit comments