Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance readme CLI command to support standalone plugins #1075

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 49 additions & 3 deletions bin/plugin/commands/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { getChangelog } = require( './changelog' );
* @typedef WPReadmeCommandOptions
*
* @property {string=} milestone Optional milestone title, to update the changelog in the readme.
* @property {string=} path Optional path to the readme.txt file to update. If omitted, it will be detected via --milestone.
* @property {string=} token Optional personal GitHub access token, only relevant for changelog updates.
*/

Expand All @@ -24,6 +25,7 @@ const { getChangelog } = require( './changelog' );
* @property {string} owner GitHub repository owner.
* @property {string} repo GitHub repository name.
* @property {string=} milestone Optional milestone title, to update the changelog in the readme.
* @property {string=} path Optional path to the readme.txt file to update.
* @property {string=} token Optional personal GitHub access token, only relevant for changelog updates.
*/

Expand All @@ -32,6 +34,11 @@ exports.options = [
argname: '-m, --milestone <milestone>',
description: 'Milestone title, to update the changelog',
},
{
argname: '-p, --path <path>',
description:
'Path to the readme.txt file to update; if omitted, it will be detected via --milestone',
},
{
argname: '-t, --token <token>',
description: 'GitHub token',
Expand All @@ -48,20 +55,53 @@ exports.handler = async ( opt ) => {
owner: config.githubRepositoryOwner,
repo: config.githubRepositoryName,
milestone: opt.milestone,
path: opt.path,
token: opt.token,
} );
};

/**
* Detects the path to the readme.txt file to update based on the milestone title.
*
* @param {string} milestone Milestone title.
*
* @return {string} Detected readme.txt path.
*/
function detectReadmePath( milestone ) {
const slug = milestone.match( /^([a-z0-9-]+) / );
if ( ! slug ) {
throw new Error(
`The ${ milestone } milestone does not start with a valid plugin slug.`
);
}

if ( 'performance-lab' === slug[ 1 ] ) {
return 'readme.txt';
}

felixarntz marked this conversation as resolved.
Show resolved Hide resolved
return `plugins/${ slug[ 1 ] }/readme.txt`;
}

/**
* Updates the `readme.txt` file with the given changelog.
*
* @param {string} changelog Changelog in markdown, with trailing newline.
* @param {WPReadmeSettings} settings Readme settings.
*/
function updateReadmeChangelog( changelog, settings ) {
const regex = new RegExp( `= ${ settings.milestone } =[^=]+` );
// Detect the version number to replace it in readme changelog, if already present.
const version = settings.milestone.match(
/\d+\.\d+(\.\d+)?(-[A-Za-z0-9.]+)?$/
);
if ( ! version ) {
throw new Error(
`The ${ settings.milestone } milestone does not end with a version number.`
);
}

const readmeFile = path.join( '.', 'readme.txt' );
const regex = new RegExp( `= ${ version[ 0 ] } =[^=]+` );

const readmeFile = path.join( '.', settings.path );
const fileContent = fs.readFileSync( readmeFile, 'utf8' );

let newContent;
Expand Down Expand Up @@ -95,6 +135,10 @@ async function updateReadme( settings ) {
);

try {
if ( ! settings.path ) {
settings.path = detectReadmePath( settings.milestone );
}

const changelog = await getChangelog( {
owner: settings.owner,
repo: settings.repo,
Expand All @@ -108,6 +152,8 @@ async function updateReadme( settings ) {
return;
}
}
log( formats.success( `\n💃readme.txt successfully updated\n\n` ) );
log(
formats.success( `\n💃${ settings.path } successfully updated\n\n` )
);
}
}