import chalk from 'chalk' ; import matcher from 'matcher' ; import { logger , pReduce } from '@reskript/core' ; import { RuleConfig , OptionalRuleConfig , Severity } from '@reskript/settings' ; const SEVERITY_PREFIX = { 'off' : '   ' , 'print' : chalk . bgWhite . black ( ' I ' ) , 'warn' : chalk . bgYellow . white ( ' W ' ) , 'error' : chalk . bgRed . white ( ' E ' ) , } ; export const createPrint = ( severity ) => ( message ) => { logger . log ( `${ SEVERITY_PREFIX [ severity ] } ${ message }` ) ; } ; type UniversalRuleConfig < T > = RuleConfig < T > | OptionalRuleConfig < T > ; export const normalizeRuleConfig = < T > ( config , defaultConfigValue ) : [ Severity , T ] => { if ( typeof config === 'string' ) { return [ config , defaultConfigValue ] ; } return config ; } ; export interface CheckHelper { notice : ( message : string ) => void ; report : ( message : string ) => void ; } export interface InspectOptions { cwd : string ; exitOnError : boolean ; } export type Check < T > = ( configValue : T , helpers : CheckHelper , options : InspectOptions ) => Promise < boolean > ; export interface RuleProcessor < T > { config : UniversalRuleConfig < T > ; defaultConfigValue : T ; check : Check < T > ; } export const run = async ( processors , options ) : Promise < void > => { const results = await pReduce ( processors , async ( results , processor ) => { const [ severity , configValue ] = normalizeRuleConfig ( processor . config , processor . defaultConfigValue ) ; if ( severity === 'off' ) { return results ; } const helpers = { report : createPrint ( severity ) , notice : createPrint ( 'print' ) , } ; const result = await processor . check ( configValue , helpers , options ) ; if ( ! result ) { results . add ( severity ) ; } return results ; } , new Set < Severity > ( ) ) ; if ( results . has ( 'error' ) && options . exitOnError ) { process . exit ( 23 ) ; } } ; const hasMatchInArray = ( value , array ) => { return array . some ( pattern => matcher . isMatch ( value , pattern ) ) ; } ; export const isIncluded = ( name , includes ? , excludes ? ) : boolean => { if ( includes ) { return hasMatchInArray ( name , includes ) ; } if ( excludes && hasMatchInArray ( name , excludes ) ) { return false ; } return true ; } ;