2022-06-23 00:09:43 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { execSync } from 'child_process';
|
2022-06-30 19:10:43 +00:00
|
|
|
import { readdirSync, readFileSync } from 'fs';
|
2022-06-23 00:09:43 +00:00
|
|
|
import { join } from 'path';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getFilepathFromPackageName } from './validate';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call changelogger's next version function to get the version for the next release.
|
|
|
|
*
|
|
|
|
* @param {string} name Package name.
|
|
|
|
* @return {string} Next release version.
|
|
|
|
*/
|
|
|
|
export const getNextVersion = ( name: string ) => {
|
|
|
|
try {
|
|
|
|
const cwd = getFilepathFromPackageName( name );
|
|
|
|
return execSync( './vendor/bin/changelogger version next', {
|
|
|
|
cwd,
|
|
|
|
encoding: 'utf-8',
|
|
|
|
} ).trim();
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( e instanceof Error ) {
|
2022-06-29 01:41:43 +00:00
|
|
|
console.log( e );
|
|
|
|
throw e;
|
2022-06-23 00:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call Changelogger's validate function on changelog entries.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @return {Error|void} Output of changelogger exec.
|
|
|
|
*/
|
|
|
|
export const validateChangelogEntries = ( name: string ) => {
|
|
|
|
try {
|
|
|
|
const cwd = getFilepathFromPackageName( name );
|
|
|
|
return execSync( './vendor/bin/changelogger validate', {
|
|
|
|
cwd,
|
|
|
|
encoding: 'utf-8',
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( e instanceof Error ) {
|
2022-06-29 01:41:43 +00:00
|
|
|
console.log( e );
|
|
|
|
throw e;
|
2022-06-23 00:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the changelog.
|
|
|
|
*
|
|
|
|
* @param {string} name Package name.
|
|
|
|
*/
|
|
|
|
export const writeChangelog = ( name: string ) => {
|
|
|
|
try {
|
|
|
|
const cwd = getFilepathFromPackageName( name );
|
2022-07-22 00:08:11 +00:00
|
|
|
execSync( './vendor/bin/changelogger write --add-pr-num', {
|
2022-06-23 00:09:43 +00:00
|
|
|
cwd,
|
|
|
|
encoding: 'utf-8',
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( e instanceof Error ) {
|
2022-06-29 01:41:43 +00:00
|
|
|
console.log( e );
|
|
|
|
throw e;
|
2022-06-23 00:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a package has changelogs to release.
|
|
|
|
*
|
|
|
|
* @param {string} name Package name.
|
|
|
|
* @return {boolean} If there are changelogs.
|
|
|
|
*/
|
2022-06-30 19:10:43 +00:00
|
|
|
export const hasValidChangelogs = ( name: string ): boolean | void => {
|
2022-06-23 00:09:43 +00:00
|
|
|
try {
|
|
|
|
const changelogDir = join(
|
|
|
|
getFilepathFromPackageName( name ),
|
|
|
|
'changelog'
|
|
|
|
);
|
|
|
|
const changelogDirContents = readdirSync( changelogDir, {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
} );
|
|
|
|
|
2022-06-30 19:10:43 +00:00
|
|
|
const changelogs = changelogDirContents.filter(
|
|
|
|
( entry ) => entry !== '.gitkeep'
|
2022-06-23 00:09:43 +00:00
|
|
|
);
|
2022-06-30 19:10:43 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
} );
|
2022-06-23 00:09:43 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
if ( e instanceof Error ) {
|
2022-06-29 01:41:43 +00:00
|
|
|
console.log( e );
|
|
|
|
throw e;
|
2022-06-23 00:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|