Skip to content

Commit

Permalink
feat(cli): support also initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 19, 2023
1 parent 0a27b74 commit da2129f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -87,13 +87,13 @@ For example:

### Migration

We provided an experimental cli tool to help you migrate from the legacy config to the new flat config.
We provided an experimental CLI tool to help you migrate from the legacy config to the new flat config.

```bash
npx @antfu/eslint-config migrate
npx @antfu/eslint-config@latest
```

Before running the migration, make sure to commit your changes first.
Before running the migration, make sure to commit your unsaved changes first.

## VS Code support (auto fix)

Expand Down
13 changes: 5 additions & 8 deletions src/cli/index.ts
Expand Up @@ -14,15 +14,16 @@ const instance = yargs(hideBin(process.argv))
.scriptName('@antfu/eslint-config')
.usage('')
.command(
'migrate',
'Migrate from legacy config to new flat config',
'*',
'Run the initialization or migration',
args => args
.option('yes', { alias: 'y', description: 'Skip prompts and use default values', type: 'boolean' })
.help(),
async (_args) => {
async (args) => {
header()
console.log()
try {
await run()
await run(args)
}
catch (error) {
console.error(c.inverse(c.red(' Failed to migrate ')))
Expand All @@ -31,10 +32,6 @@ const instance = yargs(hideBin(process.argv))
}
},
)
.command('*', false, args => args, () => {
header()
instance.showHelp()
})
.showHelpOnFail(false)
.alias('h', 'help')
.version('version', version)
Expand Down
16 changes: 13 additions & 3 deletions src/cli/run.ts
Expand Up @@ -11,10 +11,17 @@ import parse from 'parse-gitignore'
import { ARROW, CHECK, WARN, version, vscodeSettingsString } from './constants'
import { isGitClean } from './utils'

const SKIP_PROMPT = !!process.env.SKIP_PROMPT
const SKIP_GIT_CHECK = !!process.env.SKIP_GIT_CHECK
export interface RuleOptions {
/**
* Skip prompts and use default values
*/
yes?: boolean
}

export async function run(options: RuleOptions = {}) {
const SKIP_PROMPT = !!process.env.SKIP_PROMPT || options.yes
const SKIP_GIT_CHECK = !!process.env.SKIP_GIT_CHECK

export async function run() {
const cwd = process.cwd()

const pathFlatConfig = path.join(cwd, 'eslint.config.js')
Expand All @@ -37,6 +44,9 @@ export async function run() {
pkg.devDependencies ??= {}
pkg.devDependencies['@antfu/eslint-config'] = `^${version}`

if (!pkg.devDependencies.eslint)
pkg.devDependencies.eslint = '^8'

await fsp.writeFile(pathPackageJSON, JSON.stringify(pkg, null, 2))
console.log(c.green(`${CHECK} changes wrote to package.json`))

Expand Down

0 comments on commit da2129f

Please sign in to comment.