Skip to content

Commit 4344a13

Browse files
committedFeb 18, 2025
perf: migrate from yargs to cac
1 parent c187318 commit 4344a13

File tree

4 files changed

+29
-117
lines changed

4 files changed

+29
-117
lines changed
 

‎package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"@typescript-eslint/parser": "^8.24.1",
103103
"@vitest/eslint-plugin": "^1.1.31",
104104
"ansis": "^3.15.0",
105+
"cac": "^6.7.14",
105106
"eslint-config-flat-gitignore": "^2.1.0",
106107
"eslint-flat-config-utils": "^2.0.1",
107108
"eslint-merge-processors": "^2.0.0",
@@ -126,8 +127,7 @@
126127
"parse-gitignore": "^2.0.0",
127128
"toml-eslint-parser": "^0.10.0",
128129
"vue-eslint-parser": "^9.4.3",
129-
"yaml-eslint-parser": "^1.2.3",
130-
"yargs": "^17.7.2"
130+
"yaml-eslint-parser": "^1.2.3"
131131
},
132132
"devDependencies": {
133133
"@antfu/eslint-config": "workspace:*",
@@ -139,7 +139,6 @@
139139
"@types/fs-extra": "^11.0.4",
140140
"@types/node": "^22.13.4",
141141
"@types/prompts": "^2.4.9",
142-
"@types/yargs": "^17.0.33",
143142
"@unocss/eslint-plugin": "^65.5.0",
144143
"astro-eslint-parser": "^1.2.1",
145144
"bumpp": "^10.0.3",

‎pnpm-lock.yaml

+3-66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cli/index.ts

+22-46
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import process from 'node:process'
22

33
import * as p from '@clack/prompts'
44
import c from 'ansis'
5-
import yargs from 'yargs'
6-
import { hideBin } from 'yargs/helpers'
5+
import { cac } from 'cac'
76

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

16-
const instance = yargs(hideBin(process.argv))
17-
.scriptName('@antfu/eslint-config')
18-
.usage('')
19-
.command(
20-
'*',
21-
'Run the initialization or migration',
22-
args => args
23-
.option('yes', {
24-
alias: 'y',
25-
description: 'Skip prompts and use default values',
26-
type: 'boolean',
27-
})
28-
.option('template', {
29-
alias: 't',
30-
description: 'Use the framework template for optimal customization: vue / react / svelte / astro',
31-
type: 'string',
32-
})
33-
.option('extra', {
34-
alias: 'e',
35-
array: true,
36-
description: 'Use the extra utils: formatter / perfectionist / unocss',
37-
type: 'string',
38-
})
39-
.help(),
40-
async (args) => {
41-
header()
42-
try {
43-
await run(args)
44-
}
45-
catch (error) {
46-
p.log.error(c.inverse.red(' Failed to migrate '))
47-
p.log.error(c.red`✘ ${String(error)}`)
48-
process.exit(1)
49-
}
50-
},
51-
)
52-
.showHelpOnFail(false)
53-
.alias('h', 'help')
54-
.version('version', pkgJson.version)
55-
.alias('v', 'version')
15+
const cli = cac('@antfu/eslint-config')
5616

57-
// eslint-disable-next-line ts/no-unused-expressions
58-
instance
59-
.help()
60-
.argv
17+
cli
18+
.command('', 'Run the initialization or migration')
19+
.option('--yes, -y', 'Skip prompts and use default values', { default: false })
20+
.option('--template, -t <template>', 'Use the framework template for optimal customization: vue / react / svelte / astro', { type: [] })
21+
.option('--extra, -e <extra>', 'Use the extra utils: formatter / perfectionist / unocss', { type: [] })
22+
.action(async (args) => {
23+
header()
24+
try {
25+
await run(args)
26+
}
27+
catch (error) {
28+
p.log.error(c.inverse.red(' Failed to migrate '))
29+
p.log.error(c.red`✘ ${String(error)}`)
30+
process.exit(1)
31+
}
32+
})
33+
34+
cli.help()
35+
cli.version(pkgJson.version)
36+
cli.parse()

‎src/cli/run.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export interface CliRunOptions {
3131

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.