1- import { createRequire } from 'node:module' ;
2- import type { LinterOptions } from 'stylelint' ;
3- import type { PluginConfig } from '@code-pushup/models' ;
4- import { createRunnerFunction } from './runner/index.js' ;
1+ import { createRequire } from 'node:module' ;
2+ import type { LinterOptions } from 'stylelint' ;
3+ import type { Audit , PluginConfig } from '@code-pushup/models' ;
4+ import { createRunnerFunction } from './runner/index.js' ;
5+ import { getNormalizedConfigForFile } from "./runner/normalize-config" ;
6+
7+
8+ export type StylelintPluginConfig = LinterOptions & {
9+ onlyAudits : string [ ] ;
10+ }
511
612/**
713 * Instantiates Code PushUp code stylelint plugin for core config.
@@ -22,14 +28,16 @@ import { createRunnerFunction } from './runner/index.js';
2228 * @returns Plugin configuration.
2329 */
2430export async function stylelintPlugin (
25- options ?: LinterOptions ,
31+ options ?: StylelintPluginConfig ,
2632) : Promise < PluginConfig > {
2733 // const stylelintConfig = stylelintPluginConfigSchema.parse(config ?? {});
2834
2935 const packageJson = createRequire ( import . meta. url ) (
3036 '../../package.json' ,
3137 ) as typeof import ( '../../package.json' ) ;
3238
39+ // console.log('getAudits: ', await getAudits(options ?? {}));
40+
3341 return {
3442 slug : 'stylelint' ,
3543 title : 'Code stylelint' ,
@@ -39,12 +47,22 @@ export async function stylelintPlugin(
3947 packageName : packageJson . name ,
4048 version : packageJson . version ,
4149 audits : Object . keys ( options ?. config ?. rules ?? {
42- 'color-no-invalid-hex' : true ,
43- } ) . map ( slug => ( {
50+ 'color-no-invalid-hex' : true ,
51+ } ) . map ( slug => ( {
4452 slug,
4553 title : slug ,
4654 docsUrl : `https://stylelint.io/user-guide/rules/${ slug } ` ,
4755 } ) ) ,
4856 runner : createRunnerFunction ( options ?? { } ) ,
4957 } ;
5058}
59+
60+ async function getAudits ( options : StylelintPluginConfig ) : Promise < Audit [ ] > {
61+ const { onlyAudits = [ ] , ...rawCfg } = options ;
62+ const config = await getNormalizedConfigForFile ( rawCfg ) ;
63+ return Object . keys ( config . rules ) . filter ( rule => onlyAudits . length > 0 && config . rules [ rule ] !== false ) . map ( rule => ( {
64+ slug : rule ,
65+ title : rule ,
66+ docsUrl : `https://stylelint.io/user-guide/rules/${ rule } ` ,
67+ } ) ) ;
68+ }
0 commit comments