diff --git a/bin/plugin/commands/readme.js b/bin/plugin/commands/readme.js index 12f44b0df9..8b4cd0d3fb 100644 --- a/bin/plugin/commands/readme.js +++ b/bin/plugin/commands/readme.js @@ -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. */ @@ -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. */ @@ -32,6 +34,11 @@ exports.options = [ argname: '-m, --milestone ', description: 'Milestone title, to update the changelog', }, + { + argname: '-p, --path ', + description: + 'Path to the readme.txt file to update; if omitted, it will be detected via --milestone', + }, { argname: '-t, --token ', description: 'GitHub token', @@ -48,10 +55,37 @@ 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'; + } + + if ( ! fs.existsSync( path.join( '.', `plugins/${ slug[ 1 ] }` ) ) ) { + throw new Error( `Unknown plugin with slug '${ slug[ 1 ] }'` ); + } + + return `plugins/${ slug[ 1 ] }/readme.txt`; +} + /** * Updates the `readme.txt` file with the given changelog. * @@ -59,9 +93,19 @@ exports.handler = async ( opt ) => { * @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; @@ -95,6 +139,10 @@ async function updateReadme( settings ) { ); try { + if ( ! settings.path ) { + settings.path = detectReadmePath( settings.milestone ); + } + const changelog = await getChangelog( { owner: settings.owner, repo: settings.repo, @@ -108,6 +156,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` ) + ); } } diff --git a/includes/admin/js/perflab-plugin-management.js b/includes/admin/js/perflab-plugin-management.js index 45200150e8..677d5584fb 100644 --- a/includes/admin/js/perflab-plugin-management.js +++ b/includes/admin/js/perflab-plugin-management.js @@ -9,15 +9,15 @@ typeof settings.data === 'string' && settings.data.includes( 'action=install-plugin' ) ) { - var params = new URLSearchParams( settings.data ); - var slug = params.get( 'slug' ); + const params = new URLSearchParams( settings.data ); + const slug = params.get( 'slug' ); // Check if 'slug' was found and output the value. if ( ! slug ) { return; } - var target_element = $( + const target_element = $( '.wpp-standalone-plugins a[data-slug="' + slug + '"]' ); if ( ! target_element ) { @@ -30,15 +30,15 @@ * the core changes have taken place. */ setTimeout( function () { - var plugin_url = target_element.attr( 'href' ); + const plugin_url = target_element.attr( 'href' ); if ( ! plugin_url ) { return; } - var nonce = target_element.attr( + const nonce = target_element.attr( 'data-plugin-activation-nonce' ); - var plugin_slug = target_element.attr( 'data-slug' ); - var url = new URL( plugin_url ); + const plugin_slug = target_element.attr( 'data-slug' ); + const url = new URL( plugin_url ); url.searchParams.set( 'action', 'perflab_activate_plugin' ); url.searchParams.set( '_wpnonce', nonce ); url.searchParams.set( 'plugin', plugin_slug ); diff --git a/includes/admin/load.php b/includes/admin/load.php index 3b89a6db8d..0684adea70 100644 --- a/includes/admin/load.php +++ b/includes/admin/load.php @@ -131,8 +131,8 @@ function perflab_render_pointer( $pointer_id = 'perflab-admin-pointer', $args =