1- import chalk from 'chalk' ;
21import path from 'path' ;
32import {
43 ALLOWED_CATEGORIES ,
54 type Category ,
65 type Options ,
76 type RawOptions ,
87} from './types.js' ;
8+ import {
9+ printInvalidCategory ,
10+ printInvalidRegex ,
11+ printCiYesWarning ,
12+ } from '../ui/shared/printOptionErrors.js' ;
913
1014/**
1115 * Parses a comma-separated list of strings into an array of strings.
@@ -26,73 +30,74 @@ function parseList(val?: string | string[]): string[] {
2630 * @param flagName - The name of the flag being parsed (for error messages).
2731 * @returns An array of categories.
2832 */
29- function parseCategories ( val ?: string | string [ ] , flagName = '' ) : Category [ ] {
33+ function parseCategories ( val ?: string | string [ ] , flagName = '--only ' ) : Category [ ] {
3034 const raw = parseList ( val ) ;
3135 const bad = raw . filter ( ( c ) => ! ALLOWED_CATEGORIES . includes ( c as Category ) ) ;
3236 if ( bad . length ) {
33- console . error (
34- chalk . red (
35- `❌ Error: invalid ${ flagName } value(s): ${ bad . join ( ', ' ) } . Allowed: ${ ALLOWED_CATEGORIES . join ( ', ' ) } ` ,
36- ) ,
37- ) ;
38- process . exit ( 1 ) ;
37+ printInvalidCategory ( flagName , bad , ALLOWED_CATEGORIES ) ;
3938 }
4039 return raw as Category [ ] ;
4140}
4241
42+ /**
43+ * Parses regex patterns safely, exiting on invalid syntax.
44+ */
45+ function parseRegexList ( val ?: string | string [ ] ) : RegExp [ ] {
46+ const regexList : RegExp [ ] = [ ] ;
47+ for ( const pattern of parseList ( val ) ) {
48+ try {
49+ regexList . push ( new RegExp ( pattern ) ) ;
50+ } catch {
51+ printInvalidRegex ( pattern ) ;
52+ }
53+ }
54+ return regexList ;
55+ }
56+
57+ /**
58+ * Converts flag value to boolean.
59+ */
60+ function toBool ( value : unknown ) : boolean {
61+ return value === true || value === 'true' ;
62+ }
63+
4364/**
4465 * Normalizes the raw options by providing default values and parsing specific fields.
4566 * @param raw - The raw options to normalize.
4667 * @returns The normalized options.
4768 */
4869export function normalizeOptions ( raw : RawOptions ) : Options {
49- const checkValues = raw . checkValues ?? false ;
50- const isCiMode = Boolean ( raw . ci ) ;
51- const isYesMode = Boolean ( raw . yes ) ;
52- const allowDuplicates = Boolean ( raw . allowDuplicates ) ;
53- const fix = Boolean ( raw . fix ) ;
54- const json = Boolean ( raw . json ) ;
55- const onlyParsed = parseCategories ( raw . only , '--only' ) ;
56- const only = onlyParsed . length ? onlyParsed : [ ] ;
57- const noColor = Boolean ( raw . noColor ) ;
58- const compare = Boolean ( raw . compare ) ;
70+ const checkValues = toBool ( raw . checkValues ) ;
71+ const isCiMode = toBool ( raw . ci ) ;
72+ const isYesMode = toBool ( raw . yes ) ;
73+ const allowDuplicates = toBool ( raw . allowDuplicates ) ;
74+ const fix = toBool ( raw . fix ) ;
75+ const json = toBool ( raw . json ) ;
76+ const noColor = toBool ( raw . noColor ) ;
77+ const compare = toBool ( raw . compare ) ;
78+ const strict = toBool ( raw . strict ) ;
5979 const scanUsage = raw . scanUsage ?? ! compare ;
80+
81+ const showUnused = raw . showUnused !== false ;
82+ const showStats = raw . showStats !== false ;
83+ const secrets = raw . secrets !== false ;
84+
85+ const only = parseCategories ( raw . only ) ;
86+ const ignore = parseList ( raw . ignore ) ;
87+ const ignoreRegex = parseRegexList ( raw . ignoreRegex ) ;
6088 const includeFiles = parseList ( raw . includeFiles ) ;
6189 const excludeFiles = parseList ( raw . excludeFiles ) ;
62- const showUnused = raw . showUnused === false ? false : true ;
63- const showStats = raw . showStats === false ? false : true ;
6490 const files = parseList ( raw . files ) ;
65- const secrets = raw . secrets === false ? false : true ;
66- const strict = Boolean ( raw . strict ) ;
6791
68- const ignore = parseList ( raw . ignore ) ;
69- const ignoreRegex : RegExp [ ] = [ ] ;
70- for ( const pattern of parseList ( raw . ignoreRegex ) ) {
71- try {
72- ignoreRegex . push ( new RegExp ( pattern ) ) ;
73- } catch {
74- console . error (
75- chalk . red ( `❌ Error: invalid --ignore-regex pattern: ${ pattern } ` ) ,
76- ) ;
77- process . exit ( 1 ) ;
78- }
79- }
92+ const cwd = process . cwd ( ) ;
93+ const envFlag = typeof raw . env === 'string' ? path . resolve ( cwd , raw . env ) : undefined ;
94+ const exampleFlag =
95+ typeof raw . example === 'string' ? path . resolve ( cwd , raw . example ) : undefined ;
8096
8197 if ( isCiMode && isYesMode ) {
82- console . log (
83- chalk . yellow ( '⚠️ Both --ci and --yes provided; proceeding with --yes.' ) ,
84- ) ;
98+ printCiYesWarning ( ) ;
8599 }
86100
87- const cwd = process . cwd ( ) ;
88-
89- const envFlag =
90- typeof raw . env === 'string' ? path . resolve ( cwd , raw . env ) : undefined ;
91- const exampleFlag =
92- typeof raw . example === 'string'
93- ? path . resolve ( cwd , raw . example )
94- : undefined ;
95-
96101 return {
97102 checkValues,
98103 isCiMode,
0 commit comments