Skip to content

Commit a4b47e3

Browse files
committed
fixed oprtions file
1 parent ebd1277 commit a4b47e3

2 files changed

Lines changed: 89 additions & 46 deletions

File tree

src/config/options.ts

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import chalk from 'chalk';
21
import path from 'path';
32
import {
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
*/
4869
export 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,

src/ui/shared/printOptionErrors.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import chalk from 'chalk';
2+
3+
/**
4+
* Prints an error for invalid --only categories and exits.
5+
* @param flagName - The name of the flag.
6+
* @param bad - The invalid values.
7+
* @param allowed - The allowed values.
8+
*/
9+
export function printInvalidCategory(
10+
flagName: string,
11+
bad: string[],
12+
allowed: readonly string[],
13+
): never {
14+
console.error(
15+
chalk.red(
16+
`❌ Error: invalid ${flagName} value(s): ${bad.join(', ')}.\n` +
17+
` Allowed: ${allowed.join(', ')}`,
18+
),
19+
);
20+
process.exit(1);
21+
}
22+
23+
24+
/**
25+
* Prints an error for invalid regex patterns and exits.
26+
* @param pattern - The invalid regex pattern.
27+
*/
28+
export function printInvalidRegex(pattern: string): never {
29+
console.error(chalk.red(`❌ Error: invalid --ignore-regex pattern: ${pattern}`));
30+
process.exit(1);
31+
}
32+
33+
/**
34+
* Prints a warning when both --ci and --yes are provided.
35+
*/
36+
export function printCiYesWarning(): void {
37+
console.log(chalk.yellow('⚠️ Both --ci and --yes provided; proceeding with --yes.'));
38+
}

0 commit comments

Comments
 (0)