From eb223038f61e67059d6ddabf17357efcb5f3e418 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Fri, 1 Jul 2022 07:10:43 +1200 Subject: [PATCH] Prepare Script: Fix failure on only comment changelog entries (#33668) --- tools/package-release/src/changelogger.ts | 38 ++++++++++++++++--- .../src/commands/prepare/index.ts | 4 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/tools/package-release/src/changelogger.ts b/tools/package-release/src/changelogger.ts index ad659ac5dab..747bba6d64a 100644 --- a/tools/package-release/src/changelogger.ts +++ b/tools/package-release/src/changelogger.ts @@ -2,7 +2,7 @@ * External dependencies */ import { execSync } from 'child_process'; -import { readdirSync } from 'fs'; +import { readdirSync, readFileSync } from 'fs'; import { join } from 'path'; /** @@ -78,7 +78,7 @@ export const writeChangelog = ( name: string ) => { * @param {string} name Package name. * @return {boolean} If there are changelogs. */ -export const hasChangelogs = ( name: string ): boolean | void => { +export const hasValidChangelogs = ( name: string ): boolean | void => { try { const changelogDir = join( getFilepathFromPackageName( name ), @@ -88,10 +88,38 @@ export const hasChangelogs = ( name: string ): boolean | void => { encoding: 'utf-8', } ); - return ( - changelogDirContents.filter( ( entry ) => entry !== '.gitkeep' ) - .length > 0 + const changelogs = changelogDirContents.filter( + ( entry ) => entry !== '.gitkeep' ); + + if ( changelogs.length === 0 ) { + return false; + } + + // If there is at least one changelog that is not just a comment, there are valid changelogs. + return changelogs.some( ( changelog ) => { + const contents = readFileSync( + join( changelogDir, changelog ), + 'utf8' + ); + + const commentRegex = /Comment:.*\n([\s\S]*)?/; + const hasComment = commentRegex.test( contents ); + + if ( ! hasComment ) { + // Has no comment, must be a real changelog. + return true; + } + + const textAfterComment = /Comment:.*\n([\s\S]*)?/.exec( contents ); + + if ( textAfterComment ) { + // Return true if there is more than just whitespace. + return textAfterComment[ 1 ].trim().length > 0; + } + + return false; + } ); } catch ( e ) { if ( e instanceof Error ) { console.log( e ); diff --git a/tools/package-release/src/commands/prepare/index.ts b/tools/package-release/src/commands/prepare/index.ts index 5a0058a14bd..30b7f13378f 100644 --- a/tools/package-release/src/commands/prepare/index.ts +++ b/tools/package-release/src/commands/prepare/index.ts @@ -16,7 +16,7 @@ import { getNextVersion, validateChangelogEntries, writeChangelog, - hasChangelogs, + hasValidChangelogs, } from '../../changelogger'; /** @@ -85,7 +85,7 @@ export default class PackageRelease extends Command { CliUx.ux.action.start( `Preparing ${ name }` ); try { - if ( hasChangelogs( name ) ) { + if ( hasValidChangelogs( name ) ) { validateChangelogEntries( name ); const nextVersion = getNextVersion( name ); writeChangelog( name );