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' ;
4
4
import { LIGHTHOUSE_REPORT_NAME } from './constants' ;
5
5
6
6
type RefinedLighthouseOption = {
7
- url : CliFlags [ '_' ] ;
8
- chromeFlags ?: Record < CliFlags [ 'chromeFlags' ] [ number ] , string > ;
7
+ url : LighthouseFlags [ '_' ] ;
8
+ chromeFlags ?: Record < LighthouseFlags [ 'chromeFlags' ] [ number ] , string > ;
9
9
} ;
10
10
export type LighthouseCliOptions = RefinedLighthouseOption &
11
- Partial < Omit < CliFlags , keyof RefinedLighthouseOption > > ;
11
+ Partial < Omit < LighthouseFlags , keyof RefinedLighthouseOption > > ;
12
12
13
13
export function getLighthouseCliArguments (
14
14
options : LighthouseCliOptions ,
@@ -59,7 +59,7 @@ export class AuditsNotImplementedError extends Error {
59
59
export function validateOnlyAudits (
60
60
audits : Audit [ ] ,
61
61
onlyAudits : string | string [ ] ,
62
- ) : audits is Audit [ ] {
62
+ ) : boolean {
63
63
const missingAudtis = toArray ( onlyAudits ) . filter (
64
64
slug => ! audits . some ( audit => audit . slug === slug ) ,
65
65
) ;
@@ -68,3 +68,64 @@ export function validateOnlyAudits(
68
68
}
69
69
return true ;
70
70
}
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