1- import type { CoreConfig } from '@code-pushup/models' ;
1+ import type {
2+ CategoryConfig ,
3+ CoreConfig ,
4+ PluginConfig ,
5+ } from '@code-pushup/models' ;
26import { filterItemRefsBy } from '@code-pushup/utils' ;
37import type { FilterOptions , Filterables } from './filter.model.js' ;
48import {
59 handleConflictingOptions ,
10+ isValidCategoryRef ,
611 validateFilterOption ,
12+ validateFilteredCategories ,
713 validateFinalState ,
814} from './validate-filter-options.utils.js' ;
915
16+ // eslint-disable-next-line max-lines-per-function
1017export function filterMiddleware < T extends FilterOptions > (
1118 originalProcessArgs : T ,
1219) : T {
1320 const {
14- plugins ,
15- categories ,
21+ categories : rcCategories ,
22+ plugins : rcPlugins ,
1623 skipCategories = [ ] ,
1724 onlyCategories = [ ] ,
1825 skipPlugins = [ ] ,
1926 onlyPlugins = [ ] ,
2027 verbose = false ,
2128 } = originalProcessArgs ;
2229
30+ const plugins = processPlugins ( rcPlugins ) ;
31+ const categories = filterSkippedCategories ( rcCategories , plugins ) ;
32+
33+ if ( rcCategories && categories ) {
34+ validateFilteredCategories ( rcCategories , categories , {
35+ onlyCategories,
36+ skipCategories,
37+ verbose,
38+ } ) ;
39+ }
40+
2341 if (
2442 skipCategories . length === 0 &&
2543 onlyCategories . length === 0 &&
2644 skipPlugins . length === 0 &&
2745 onlyPlugins . length === 0
2846 ) {
29- return originalProcessArgs ;
47+ return {
48+ ...originalProcessArgs ,
49+ ...( categories && { categories } ) ,
50+ plugins,
51+ } ;
3052 }
3153
3254 handleConflictingOptions ( 'categories' , onlyCategories , skipCategories ) ;
@@ -52,7 +74,7 @@ export function filterMiddleware<T extends FilterOptions>(
5274
5375 validateFinalState (
5476 { categories : finalCategories , plugins : filteredPlugins } ,
55- { categories, plugins } ,
77+ { categories : rcCategories , plugins : rcPlugins } ,
5678 ) ;
5779
5880 return {
@@ -141,3 +163,40 @@ function filterPluginsFromCategories({
141163 ) ;
142164 return plugins . filter ( plugin => validPluginSlugs . has ( plugin . slug ) ) ;
143165}
166+
167+ function filterSkippedItems < T extends { isSkipped ?: boolean } > (
168+ items : T [ ] | undefined ,
169+ ) : Omit < T , 'isSkipped' > [ ] {
170+ return ( items ?? [ ] )
171+ . filter ( ( { isSkipped } ) => isSkipped !== true )
172+ . map ( ( { isSkipped, ...props } ) => props ) ;
173+ }
174+
175+ export function processPlugins ( plugins : PluginConfig [ ] ) : PluginConfig [ ] {
176+ return plugins . map ( ( plugin : PluginConfig ) => {
177+ const filteredAudits = filterSkippedItems ( plugin . audits ) ;
178+ return {
179+ ...plugin ,
180+ ...( plugin . groups && {
181+ groups : filterItemRefsBy ( filterSkippedItems ( plugin . groups ) , ref =>
182+ filteredAudits . some ( ( { slug } ) => slug === ref . slug ) ,
183+ ) ,
184+ } ) ,
185+ audits : filteredAudits ,
186+ } ;
187+ } ) ;
188+ }
189+
190+ export function filterSkippedCategories (
191+ categories : CoreConfig [ 'categories' ] ,
192+ plugins : CoreConfig [ 'plugins' ] ,
193+ ) : CoreConfig [ 'categories' ] {
194+ return categories
195+ ?. map ( category => {
196+ const validRefs = category . refs . filter ( ref =>
197+ isValidCategoryRef ( ref , plugins ) ,
198+ ) ;
199+ return validRefs . length > 0 ? { ...category , refs : validRefs } : null ;
200+ } )
201+ . filter ( ( category ) : category is CategoryConfig => category != null ) ;
202+ }
0 commit comments