1- import type { CliFlags } from 'lighthouse' ;
2- import { Audit } from '@code-pushup/models' ;
3- import { objectToCliArgs , toArray } from '@code-pushup/utils' ;
1+ import type { CliFlags as LighthouseFlags } from 'lighthouse' ;
2+ import { Audit , Group } from '@code-pushup/models' ;
3+ import { filterItemRefsBy , objectToCliArgs , toArray } from '@code-pushup/utils' ;
44import { LIGHTHOUSE_REPORT_NAME } from './constants' ;
55
66type RefinedLighthouseOption = {
7- url : CliFlags [ '_' ] ;
8- chromeFlags ?: Record < CliFlags [ 'chromeFlags' ] [ number ] , string > ;
7+ url : LighthouseFlags [ '_' ] ;
8+ chromeFlags ?: Record < LighthouseFlags [ 'chromeFlags' ] [ number ] , string > ;
99} ;
1010export type LighthouseCliOptions = RefinedLighthouseOption &
11- Partial < Omit < CliFlags , keyof RefinedLighthouseOption > > ;
11+ Partial < Omit < LighthouseFlags , keyof RefinedLighthouseOption > > ;
1212
1313export function getLighthouseCliArguments (
1414 options : LighthouseCliOptions ,
@@ -59,7 +59,7 @@ export class AuditsNotImplementedError extends Error {
5959export function validateOnlyAudits (
6060 audits : Audit [ ] ,
6161 onlyAudits : string | string [ ] ,
62- ) : audits is Audit [ ] {
62+ ) : boolean {
6363 const missingAudtis = toArray ( onlyAudits ) . filter (
6464 slug => ! audits . some ( audit => audit . slug === slug ) ,
6565 ) ;
@@ -68,3 +68,64 @@ export function validateOnlyAudits(
6868 }
6969 return true ;
7070}
71+
72+ export class CategoriesNotImplementedError extends Error {
73+ constructor ( categorySlugs : string [ ] ) {
74+ super ( `categories: "${ categorySlugs . join ( ', ' ) } " not implemented` ) ;
75+ }
76+ }
77+
78+ export function validateOnlyCategories (
79+ groups : Group [ ] ,
80+ onlyCategories : string | string [ ] ,
81+ ) : boolean {
82+ const missingCategories = toArray ( onlyCategories ) . filter ( slug =>
83+ groups . every ( group => group . slug !== slug ) ,
84+ ) ;
85+ if ( missingCategories . length > 0 ) {
86+ throw new CategoriesNotImplementedError ( missingCategories ) ;
87+ }
88+ return true ;
89+ }
90+
91+ export function filterAuditsAndGroupsByOnlyOptions (
92+ audits : Audit [ ] ,
93+ groups : Group [ ] ,
94+ options ?: Pick < LighthouseFlags , 'onlyAudits' | 'onlyCategories' > ,
95+ ) : {
96+ audits : Audit [ ] ;
97+ groups : Group [ ] ;
98+ } {
99+ const { onlyAudits, onlyCategories } = options ?? { } ;
100+
101+ // category wins over audits
102+ if ( onlyCategories && onlyCategories . length > 0 ) {
103+ validateOnlyCategories ( groups , onlyCategories ) ;
104+
105+ const categorieSlugs = new Set ( onlyCategories ) ;
106+ const filteredGroups : Group [ ] = groups . filter ( ( { slug } ) =>
107+ categorieSlugs . has ( slug ) ,
108+ ) ;
109+ const auditSlugsFromRemainingGroups = new Set (
110+ filteredGroups . flatMap ( ( { refs } ) => refs . map ( ( { slug } ) => slug ) ) ,
111+ ) ;
112+ return {
113+ audits : audits . filter ( ( { slug } ) =>
114+ auditSlugsFromRemainingGroups . has ( slug ) ,
115+ ) ,
116+ groups : filteredGroups ,
117+ } ;
118+ } else if ( onlyAudits && onlyAudits . length > 0 ) {
119+ validateOnlyAudits ( audits , onlyAudits ) ;
120+ const auditSlugs = new Set ( onlyAudits ) ;
121+ return {
122+ audits : audits . filter ( ( { slug } ) => auditSlugs . has ( slug ) ) ,
123+ groups : filterItemRefsBy ( groups , ( { slug } ) => auditSlugs . has ( slug ) ) ,
124+ } ;
125+ }
126+ // return unchanged
127+ return {
128+ audits,
129+ groups,
130+ } ;
131+ }
0 commit comments