Skip to content

Sort modules by the specified criteria on #306 #315

New issue

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

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

Already on ? Sign in to your account

Merged
merged 10 commits into from
May 9, 2022
62 changes: 61 additions & 1 deletion bin/plugin/commands/common.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@
const path = require( 'path' );
const glob = require( 'fast-glob' );
const fs = require( 'fs' );
const { log, formats } = require( '../lib/logger' );

/**
* @typedef WPModuleData
Expand All@@ -15,6 +16,18 @@ const fs = require( 'fs' );
* @property {boolean} experimental Whether the module is experimental.
*/

/**
* Definition of the groups areas as an object with a number to specify the order priority of each,
* with increments of 10 between each module, in order to allow space for any change down the road.
*/
const FOCUS_AREAS = {
images: 10,
javascript: 20,
'site-health': 30,
measurement: 40,
'object-cache': 50,
};

/**
* Returns a promise resolving to the list of data for all modules.
*
Expand DownExpand Up@@ -68,5 +81,52 @@ exports.getModuleData = async ( modulesDir ) => {

return moduleData;
} )
.filter( ( moduleData ) => moduleData.name && moduleData.description );
.filter( ( moduleData ) => {
const moduleDetails = JSON.stringify( moduleData );
if ( ! moduleData.name ) {
log(
formats.warning(
`This module was not included due is missing a required property 'name', Details: ${ moduleDetails }`
)
);
}

if ( ! moduleData.description ) {
log(
formats.warning(
`This module was not included due is missing a required property 'description', Details: ${ moduleDetails }`
)
);
}

if ( typeof moduleData.experimental === 'undefined' ) {
log(
formats.warning(
`This module was not included due is missing a required property 'experimental', Details: ${ moduleDetails }`
)
);
}

return (
moduleData.name &&
moduleData.description &&
typeof moduleData.experimental !== 'undefined'
);
} )
.sort( ( firstModule, secondModule ) => {
// Not the same focus group.
if ( firstModule.focus !== secondModule.focus ) {
return FOCUS_AREAS[ firstModule.focus ] >
FOCUS_AREAS[ secondModule.focus ]
? 1
: -1;
}

if ( firstModule.experimental !== secondModule.experimental ) {
return firstModule.experimental ? 1 : -1;
}

// Lastly order alphabetically.
return firstModule.slug.localeCompare( secondModule.slug );
} );
};