Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/cli/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable perfectionist/sort-objects */
import type { ExtraLibrariesOption, FrameworkOption, PromptResult } from './types'
import type { ExtraLibrariesOption, FrameworkOption, LintOption, PromptResult } from './types'

import fs from 'node:fs'
import path from 'node:path'
Expand Down Expand Up @@ -27,12 +27,17 @@ export interface CliRunOptions {
* Use the extra utils: formatter / perfectionist / unocss
*/
extra?: string[]
/**
* Configure lint script: keep / check / fix
*/
lint?: string
}

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()).filter(Boolean)
const argExtra = <ExtraLibrariesOption[]>options.extra?.map(m => m?.trim()).filter(Boolean)
const argLint = <LintOption>options.lint?.trim()

if (fs.existsSync(path.join(process.cwd(), 'eslint.config.js'))) {
p.log.warn(c.yellow`eslint.config.js already exists, migration wizard exited.`)
Expand All @@ -45,6 +50,7 @@ export async function run(options: CliRunOptions = {}): Promise<void> {
frameworks: argTemplate ?? [],
uncommittedConfirmed: false,
updateVscodeSettings: true,
lint: argLint ?? 'keep',
}

if (!argSkipPrompt) {
Expand Down Expand Up @@ -100,6 +106,36 @@ export async function run(options: CliRunOptions = {}): Promise<void> {
message: 'Update .vscode/settings.json for better VS Code experience?',
})
},
lint: async ({ results }) => {
if (!results.uncommittedConfirmed)
return 'keep'

const isArgLintValid = argLint && ['keep', 'check', 'fix'].includes(argLint)
if (isArgLintValid)
return argLint

const pkgPath = path.join(process.cwd(), 'package.json')
const existingScript = fs.existsSync(pkgPath)
? (JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))?.scripts?.lint as string | undefined)
: undefined

const options = [
{
label: existingScript
? `Keep existing script: ${c.dim(existingScript)}`
: 'Do not add script',
value: 'keep',
},
{ label: 'Add check script (eslint --cache)', value: 'check' },
{ label: 'Add fix script (eslint --fix --cache)', value: 'fix' },
]

return p.select({
message: 'Configure lint script in package.json?',
options,
initialValue: 'keep',
})
},
}, {
onCancel: () => {
p.cancel('Operation cancelled.')
Expand Down
9 changes: 9 additions & 0 deletions src/cli/stages/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export async function updatePackageJson(result: PromptResult): Promise<void> {
const pkg: Record<string, any> = JSON.parse(pkgContent)

pkg.devDependencies ??= {}

// Handle lint script configuration
if (result.lint !== 'keep') {
pkg.scripts ??= {}
pkg.scripts.lint = result.lint === 'fix'
? 'eslint --fix --cache'
: 'eslint --cache'
}

pkg.devDependencies['@antfu/eslint-config'] = `^${pkgJson.version}`
pkg.devDependencies.eslint ??= pkgJson
.devDependencies
Expand Down
3 changes: 3 additions & 0 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export type FrameworkOption = 'vue' | 'react' | 'svelte' | 'astro' | 'solid' | '

export type ExtraLibrariesOption = 'formatter' | 'unocss'

export type LintOption = 'keep' | 'check' | 'fix'

export interface PromptResult {
uncommittedConfirmed: boolean
frameworks: FrameworkOption[]
extra: ExtraLibrariesOption[]
updateVscodeSettings: unknown
lint: LintOption
}
39 changes: 39 additions & 0 deletions test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ it('package.json updated', async () => {
expect(stdout).toContain('Changes wrote to package.json')
})

it('lint script - keep should preserve existing script', async () => {
// Setup existing lint script
await fs.writeJSON(join(genPath, 'package.json'), {
scripts: {
lint: 'custom-lint-command',
},
}, { spaces: 2 })

const { stdout } = await run(['--lint=keep'])

const pkgContent: Record<string, any> = await fs.readJSON(join(genPath, 'package.json'))
expect(pkgContent.scripts?.lint).toBe('custom-lint-command')
expect(stdout).toContain('Changes wrote to package.json')
})

it('lint script - check should add check script', async () => {
const { stdout } = await run(['--lint=check'])

const pkgContent: Record<string, any> = await fs.readJSON(join(genPath, 'package.json'))
expect(pkgContent.scripts?.lint).toBe('eslint --cache')
expect(stdout).toContain('Changes wrote to package.json')
})

it('lint script - fix should add fix script', async () => {
const { stdout } = await run(['--lint=fix'])

const pkgContent: Record<string, any> = await fs.readJSON(join(genPath, 'package.json'))
expect(pkgContent.scripts?.lint).toBe('eslint --fix --cache')
expect(stdout).toContain('Changes wrote to package.json')
})

it('lint script - keep should not add script if none exists', async () => {
const { stdout } = await run(['--lint=keep'])

const pkgContent: Record<string, any> = await fs.readJSON(join(genPath, 'package.json'))
expect(pkgContent.scripts?.lint).toBeUndefined()
expect(stdout).toContain('Changes wrote to package.json')
})

it('esm eslint.config.js', async () => {
const pkgContent = await fs.readFile('package.json', 'utf-8')
await fs.writeFile(join(genPath, 'package.json'), JSON.stringify({ ...JSON.parse(pkgContent), type: 'module' }, null, 2))
Expand Down