Skip to content

Commit

Permalink
fix: add helper method to check for CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Pena committed Aug 28, 2024
1 parent 98b69b7 commit be3dcf2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/helpers/wasOptionPassed.ts
Original file line number Diff line number Diff line change
@@ -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))
}
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit be3dcf2

Please sign in to comment.