Skip to content

Commit

Permalink
perf: migrate from yargs to cac
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 18, 2025
1 parent c187318 commit 4344a13
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 117 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"@typescript-eslint/parser": "^8.24.1",
"@vitest/eslint-plugin": "^1.1.31",
"ansis": "^3.15.0",
"cac": "^6.7.14",
"eslint-config-flat-gitignore": "^2.1.0",
"eslint-flat-config-utils": "^2.0.1",
"eslint-merge-processors": "^2.0.0",
Expand All @@ -126,8 +127,7 @@
"parse-gitignore": "^2.0.0",
"toml-eslint-parser": "^0.10.0",
"vue-eslint-parser": "^9.4.3",
"yaml-eslint-parser": "^1.2.3",
"yargs": "^17.7.2"
"yaml-eslint-parser": "^1.2.3"
},
"devDependencies": {
"@antfu/eslint-config": "workspace:*",
Expand All @@ -139,7 +139,6 @@
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.13.4",
"@types/prompts": "^2.4.9",
"@types/yargs": "^17.0.33",
"@unocss/eslint-plugin": "^65.5.0",
"astro-eslint-parser": "^1.2.1",
"bumpp": "^10.0.3",
Expand Down
69 changes: 3 additions & 66 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 22 additions & 46 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import process from 'node:process'

import * as p from '@clack/prompts'
import c from 'ansis'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { cac } from 'cac'

import { pkgJson } from './constants'
import { run } from './run'
Expand All @@ -13,48 +12,25 @@ function header(): void {
p.intro(`${c.green`@antfu/eslint-config `}${c.dim`v${pkgJson.version}`}`)
}

const instance = yargs(hideBin(process.argv))
.scriptName('@antfu/eslint-config')
.usage('')
.command(
'*',
'Run the initialization or migration',
args => args
.option('yes', {
alias: 'y',
description: 'Skip prompts and use default values',
type: 'boolean',
})
.option('template', {
alias: 't',
description: 'Use the framework template for optimal customization: vue / react / svelte / astro',
type: 'string',
})
.option('extra', {
alias: 'e',
array: true,
description: 'Use the extra utils: formatter / perfectionist / unocss',
type: 'string',
})
.help(),
async (args) => {
header()
try {
await run(args)
}
catch (error) {
p.log.error(c.inverse.red(' Failed to migrate '))
p.log.error(c.red`✘ ${String(error)}`)
process.exit(1)
}
},
)
.showHelpOnFail(false)
.alias('h', 'help')
.version('version', pkgJson.version)
.alias('v', 'version')
const cli = cac('@antfu/eslint-config')

// eslint-disable-next-line ts/no-unused-expressions
instance
.help()
.argv
cli
.command('', 'Run the initialization or migration')
.option('--yes, -y', 'Skip prompts and use default values', { default: false })
.option('--template, -t <template>', 'Use the framework template for optimal customization: vue / react / svelte / astro', { type: [] })
.option('--extra, -e <extra>', 'Use the extra utils: formatter / perfectionist / unocss', { type: [] })
.action(async (args) => {
header()
try {
await run(args)
}
catch (error) {
p.log.error(c.inverse.red(' Failed to migrate '))
p.log.error(c.red`✘ ${String(error)}`)
process.exit(1)
}
})

cli.help()
cli.version(pkgJson.version)
cli.parse()
4 changes: 2 additions & 2 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export interface CliRunOptions {

export async function run(options: CliRunOptions = {}): Promise<void> {
const argSkipPrompt = !!process.env.SKIP_PROMPT || options.yes
const argTemplate = <FrameworkOption[]>options.frameworks?.map(m => m.trim())
const argExtra = <ExtraLibrariesOption[]>options.extra?.map(m => m.trim())
const argTemplate = <FrameworkOption[]>options.frameworks?.map(m => m?.trim()).filter(Boolean)
const argExtra = <ExtraLibrariesOption[]>options.extra?.map(m => m?.trim()).filter(Boolean)

if (fs.existsSync(path.join(process.cwd(), 'eslint.config.js'))) {
p.log.warn(c.yellow`eslint.config.js already exists, migration wizard exited.`)
Expand Down

0 comments on commit 4344a13

Please # to comment.