Skip to content

Commit

Permalink
Merge pull request #15 from eslint-kit/fix/better-package-json-scripts
Browse files Browse the repository at this point in the history
Better package json scripts
  • Loading branch information
Evgeny Zakharov authored Apr 29, 2023
2 parents f1a39c4 + 6778e10 commit f7346d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/modules/commands/init.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { EslintKitApiService } from '../eslint-kit-api'
import { MetaService } from '../meta'
import { InjectPackageManager } from '../package-manager'
import {
askForPackageJsonCommands,
askAboutPackageJsonScripts,
askForPrettierOverride,
askForReplacePermission,
confirmDependencies,
Expand Down Expand Up @@ -46,10 +46,13 @@ export class InitCommand implements CommandRunner {
const canWritePrettier =
!prettierLocation || (await askForPrettierOverride())

const canAddPackageJsonCommands =
!packageJson.scripts?.lint &&
!packageJson.scripts?.['lint:fix'] &&
(await askForPackageJsonCommands())
const hasLintScripts = Boolean(
packageJson.scripts?.lint || packageJson.scripts?.['lint:fix']
)

const canAddLintScripts = hasLintScripts
? await askAboutPackageJsonScripts('replace')
: await askAboutPackageJsonScripts('add')

const dependenciesToDelete = await this.meta.findOverlappingDependencies()

Expand Down Expand Up @@ -165,7 +168,7 @@ export class InitCommand implements CommandRunner {
presets.push(builder.preset('effector'))
}

if (canAddPackageJsonCommands) {
if (canAddLintScripts) {
const scripts = packageJson.scripts ?? {}
const ext = Array.from(extensions).join(',')
const dir = Array.from(directories).join(' ')
Expand Down
11 changes: 7 additions & 4 deletions src/modules/commands/init.questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ export async function askForPrettierOverride(): Promise<boolean> {
.then((answers) => answers[name])
}

export async function askForPackageJsonCommands(): Promise<boolean> {
export async function askAboutPackageJsonScripts(
action: 'add' | 'replace'
): Promise<boolean> {
const name = 'allowed'

const canSafelyAdd = action === 'add'

return inquirer
.prompt([
{
name,
type: 'confirm',
default: true,
message:
'Do you want to add "lint" and "lint:fix" scripts to your package.json?',
default: canSafelyAdd,
message: `Do you want to ${action} "lint" and "lint:fix" scripts to your package.json?`,
},
])
.then((answers) => answers[name])
Expand Down

0 comments on commit f7346d2

Please sign in to comment.