Prepare Script: Fix failure on only comment changelog entries (#33668)

This commit is contained in:
Paul Sealock 2022-07-01 07:10:43 +12:00 committed by GitHub
parent 6cb6fd02df
commit eb223038f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 7 deletions

View File

@ -2,7 +2,7 @@
* External dependencies * External dependencies
*/ */
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { readdirSync } from 'fs'; import { readdirSync, readFileSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
/** /**
@ -78,7 +78,7 @@ export const writeChangelog = ( name: string ) => {
* @param {string} name Package name. * @param {string} name Package name.
* @return {boolean} If there are changelogs. * @return {boolean} If there are changelogs.
*/ */
export const hasChangelogs = ( name: string ): boolean | void => { export const hasValidChangelogs = ( name: string ): boolean | void => {
try { try {
const changelogDir = join( const changelogDir = join(
getFilepathFromPackageName( name ), getFilepathFromPackageName( name ),
@ -88,10 +88,38 @@ export const hasChangelogs = ( name: string ): boolean | void => {
encoding: 'utf-8', encoding: 'utf-8',
} ); } );
return ( const changelogs = changelogDirContents.filter(
changelogDirContents.filter( ( entry ) => entry !== '.gitkeep' ) ( entry ) => entry !== '.gitkeep'
.length > 0
); );
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 ) { } catch ( e ) {
if ( e instanceof Error ) { if ( e instanceof Error ) {
console.log( e ); console.log( e );

View File

@ -16,7 +16,7 @@ import {
getNextVersion, getNextVersion,
validateChangelogEntries, validateChangelogEntries,
writeChangelog, writeChangelog,
hasChangelogs, hasValidChangelogs,
} from '../../changelogger'; } from '../../changelogger';
/** /**
@ -85,7 +85,7 @@ export default class PackageRelease extends Command {
CliUx.ux.action.start( `Preparing ${ name }` ); CliUx.ux.action.start( `Preparing ${ name }` );
try { try {
if ( hasChangelogs( name ) ) { if ( hasValidChangelogs( name ) ) {
validateChangelogEntries( name ); validateChangelogEntries( name );
const nextVersion = getNextVersion( name ); const nextVersion = getNextVersion( name );
writeChangelog( name ); writeChangelog( name );