From be3dcf24161aafa6c094aef8935983b4a41dda81 Mon Sep 17 00:00:00 2001 From: David-Pena Date: Wed, 28 Aug 2024 10:13:02 -0700 Subject: [PATCH] fix: add helper method to check for CLI commands --- src/helpers/wasOptionPassed.ts | 8 ++++++++ src/index.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/helpers/wasOptionPassed.ts diff --git a/src/helpers/wasOptionPassed.ts b/src/helpers/wasOptionPassed.ts new file mode 100644 index 00000000..3d57539c --- /dev/null +++ b/src/helpers/wasOptionPassed.ts @@ -0,0 +1,8 @@ +// Helper function to determine if an option was passed via CLI +export function wasOptionPassed(option: string) { + // Regular expression to match the exact option with any value + const regex = new RegExp(`--${option}(?:=[^\\s]*)?$`) + + // eslint-disable-next-line node/prefer-global/process + return process.argv.some(arg => regex.test(arg)) +} diff --git a/src/index.ts b/src/index.ts index 69e55909..e44a297d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { validateOption } from './helpers/validateOption' import getProjectRoot from './helpers/getProjectRoot' import coerceRules from './helpers/coerceRules' import { FLAT_RULESETS_RULES } from './helpers/constants' +import { wasOptionPassed } from './helpers/wasOptionPassed' const projectRoot = await getProjectRoot() if (!projectRoot) { @@ -44,6 +45,10 @@ catch { output.push({ info: `👉 Using default configuration` }) } +// Check if `apply` or `ignore` options were explicitly passed +const applyFromCLI = wasOptionPassed('apply') +const ignoreFromCLI = wasOptionPassed('ignore') + // eslint-disable-next-line ts/no-unused-expressions, node/prefer-global/process yargs(hideBin(process.argv)) .command( @@ -108,6 +113,16 @@ yargs(hideBin(process.argv)) default: config.output, group: 'Output Format:', }) + .check(() => { + // Only check for conflict if both apply and ignore are provided from CLI + if (applyFromCLI && ignoreFromCLI) { + console.error(`\n${BG_ERR}Cannot use both --ignore and --apply options together.${BG_RESET}\n`) + // eslint-disable-next-line node/prefer-global/process + process.exit(1) + } + + return true + }) }, (argv) => { analyze({