-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.ts
More file actions
81 lines (75 loc) · 2.51 KB
/
Copy pathutils.ts
File metadata and controls
81 lines (75 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { Audit, Group } from '@code-pushup/models';
import { logger, pluralizeToken } from '@code-pushup/utils';
import type { JsDocsPluginTransformedConfig } from './config.js';
import { AUDITS_MAP, GROUPS } from './constants.js';
import { formatMetaLog } from './format.js';
/**
* Get audits based on the configuration.
* If no audits are specified, return all audits.
* If audits are specified, return only the specified audits.
* @param config - The configuration object.
* @returns The audits.
*/
export function filterAuditsByPluginConfig(
config: Pick<JsDocsPluginTransformedConfig, 'onlyAudits' | 'skipAudits'>,
): Audit[] {
const { onlyAudits, skipAudits } = config;
if (onlyAudits && onlyAudits.length > 0) {
return Object.values(AUDITS_MAP).filter(audit =>
onlyAudits.includes(audit.slug),
);
}
if (skipAudits && skipAudits.length > 0) {
return Object.values(AUDITS_MAP).filter(
audit => !skipAudits.includes(audit.slug),
);
}
return Object.values(AUDITS_MAP);
}
/**
* Filter groups by the audits that are specified in the configuration.
* The groups refs are filtered to only include the audits that are specified in the configuration.
* @param groups - The groups to filter.
* @param options - The configuration object containing either onlyAudits or skipAudits.
* @returns The filtered groups.
*/
export function filterGroupsByOnlyAudits(
groups: Group[],
options: Pick<JsDocsPluginTransformedConfig, 'onlyAudits' | 'skipAudits'>,
): Group[] {
const audits = filterAuditsByPluginConfig(options);
return groups
.map(group => ({
...group,
refs: group.refs.filter(ref =>
audits.some(audit => audit.slug === ref.slug),
),
}))
.filter(group => group.refs.length > 0);
}
export function logAuditsAndGroups(audits: Audit[], groups: Group[]) {
logger.info(
formatMetaLog(
`Created ${pluralizeToken('audit', audits.length)} and ${pluralizeToken('group', groups.length)}`,
),
);
const skippedAudits = Object.keys(AUDITS_MAP).filter(
slug => !audits.some(audit => audit.slug === slug),
);
const skippedGroups = GROUPS.filter(
group => !groups.some(({ slug }) => slug === group.slug),
);
if (skippedAudits.length > 0) {
logger.info(
formatMetaLog(
[
`Skipped ${pluralizeToken('audit', skippedAudits.length)}`,
skippedGroups.length > 0 &&
pluralizeToken('group', skippedGroups.length),
]
.filter(Boolean)
.join(' and '),
),
);
}
}