Skip to content

Commit

Permalink
Merge pull request #1119 from WordPress/update/since-command
Browse files Browse the repository at this point in the history
Update `since` command for standalone plugin usage

Co-authored-by: thelovekesh <thelovekesh@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
  • Loading branch information
4 people authored Apr 10, 2024
2 parents 9d0a869 + b65b8a8 commit 308f61a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
7 changes: 6 additions & 1 deletion bin/plugin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
const program = require( 'commander' );

/**
* Internal dependencies
*/
const { formats } = require( './lib/logger' );

const withOptions = ( command, options ) => {
options.forEach( ( { description, argname, defaults } ) => {
command = command.option( argname, description, defaults );
Expand All @@ -17,7 +22,7 @@ const catchException = ( handler ) => {
try {
await handler( ...args );
} catch ( error ) {
console.error( error ); // eslint-disable-line no-console
console.error( formats.error( error.message ) ); // eslint-disable-line no-console
process.exitCode = 1;
}
};
Expand Down
52 changes: 39 additions & 13 deletions bin/plugin/commands/since.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const glob = require( 'fast-glob' );
const fs = require( 'fs' );

/**
* Internal dependencies
*/
const { log, formats } = require( '../lib/logger' );
const { plugins } = require( '../../../plugins.json' );

/**
* @typedef WPSinceCommandOptions
*
* @property {string} version Version number.
* @property {string} plugin Plugin slug.
* @property {string} release Release version number.
*/

exports.options = [
{
argname: '-p, --plugin <plugin>',
description: 'Plugin slug',
defaults: 'performance-lab',
},
{
argname: '-r, --release <release>',
description: 'Release version number',
Expand All @@ -30,21 +36,41 @@ exports.options = [
*/
exports.handler = async ( opt ) => {
if ( ! opt.release ) {
log(
formats.error(
'The release version must be provided via the --release (-r) argument.'
)
throw new Error(
'The release version must be provided via the --release (-r) argument.'
);
}

if (
opt.plugin !== 'performance-lab' &&
! plugins.includes( opt.plugin )
) {
throw new Error(
`The plugin "${ opt.plugin }" is not a valid plugin managed as part of this project.`
);
return;
}

const patterns = [
path.resolve( __dirname, '../../../**/*.php' ),
path.resolve( __dirname, '../../../**/*.js' ),
];
const patterns = [];
const pluginRoot = path.resolve( __dirname, '../../../' );
const ignore = [ '**/node_modules', '**/vendor', '**/bin', '**/build' ];

/*
* For a standalone plugin, use the specific plugin directory.
* For Performance Lab, use the root directory and ignore the standalone plugin directories.
*/
if ( opt.plugin !== 'performance-lab' ) {
const pluginPath = path.resolve( pluginRoot, 'plugins', opt.plugin );

patterns.push( `${ pluginPath }/**/*.php` );
patterns.push( `${ pluginPath }/**/*.js` );
} else {
ignore.push( '**/plugins' );
patterns.push( `${ pluginRoot }/**/*.php` );
patterns.push( `${ pluginRoot }/**/*.js` );
}

const files = await glob( patterns, {
ignore: [ __filename, '**/node_modules', '**/vendor' ],
ignore,
} );

const regexp = new RegExp( '@since(\\s+)n.e.x.t', 'g' );
Expand Down

0 comments on commit 308f61a

Please sign in to comment.