Skip to content
This repository has been archived by the owner on May 22, 2022. It is now read-only.

Commit

Permalink
fix(cli): better work with prettier plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
risenforces committed Jul 4, 2021
1 parent 5e1d476 commit 393c5f3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-bees-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eslint-kit/cli': patch
---

better experience when upgrading the prettier plugin
2 changes: 1 addition & 1 deletion packages/cli/src/actions/alias/alias.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { updateDependencies } from '../../lib/dependencies-work'
import { getDependenciesToDelete } from '../../lib/get-dependencies-to-delete'
import { getDependenciesToInstall } from '../../lib/get-dependencies-to-install'
import { getRequiredDependencies } from '../../lib/get-required-dependencies'
import { getUpdatedPrettierConfig } from '../../lib/get-updated-prettier-config'
import { getWrongDependencies } from '../../lib/get-wrong-dependencies'
import { getWrongDependenciesToUpdate } from '../../lib/get-wrong-dependencies-to-update'
import { MESSAGES } from '../../lib/ui/messages'
Expand All @@ -12,7 +13,6 @@ import { updatePrettierConfig } from '../../lib/update-prettier-config'
import { log } from '../../lib/util/log'
import { askQuestions } from './ask-questions'
import { getUpdatedEslintConfig } from './get-updated-eslint-config'
import { getUpdatedPrettierConfig } from './get-updated-prettier-config'
import { LOCAL_MESSAGES } from './ui/local-messages'

export class AliasAction {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/actions/upgrade/ui/local-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ export const LOCAL_MESSAGES = {
NO_CONFIGS: 'No configs are installed',
ALREADY_UP_TO_DATE: 'Already up-to-date',
VERSIONS_UPGRADED: 'Successfully upgraded versions',
RE_RUN_ALIAS:
'The new version of prettier plugin uses the sort-imports plugin. ' +
"Please re-run the CLI alias command to fill the required prettier's config fields.",
}
38 changes: 34 additions & 4 deletions packages/cli/src/actions/upgrade/upgrade.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import chalk from 'chalk'
import { toConfigPackage } from '../../lib/config-name-helpers'
import { getDataBySchema } from '../../lib/data-by-schema'
import { upgradeDependenciesToLatest } from '../../lib/dependencies-work'
import { getUpdatedPrettierConfig } from '../../lib/get-updated-prettier-config'
import { MESSAGES } from '../../lib/ui/messages'
import { updatePrettierConfig } from '../../lib/update-prettier-config'
import { log } from '../../lib/util/log'
import { LOCAL_MESSAGES } from './ui/local-messages'

export class UpgradeAction {
static async process(): Promise<void> {
const { packageManager, installedConfigs } = await getDataBySchema({
packageManager: true,
installedConfigs: true,
})
const { rootDir, packageManager, installedConfigs, prettierConfigMeta } =
await getDataBySchema({
rootDir: true,
packageManager: true,
installedConfigs: true,
prettierConfigMeta: true,
})

if (installedConfigs.length === 0) {
log(LOCAL_MESSAGES.NO_CONFIGS, chalk.red)
Expand All @@ -23,8 +28,33 @@ export class UpgradeAction {
dependencies: installedConfigs.map(toConfigPackage),
})

if (installedConfigs.includes('prettier')) {
const updatedConfig = getUpdatedPrettierConfig({
prettierConfigMeta,
aliasesMeta: {
aliasMap: [],
},
})

if (updatedConfig)
await updatePrettierConfig({
rootDir,
prettierConfigMeta,
updatedConfig,
})
}

log(LOCAL_MESSAGES.VERSIONS_UPGRADED, chalk.green, [true, false])
log(MESSAGES.PLEASE_RESTART, [chalk.magenta, chalk.bold])

if (
prettierConfigMeta.found &&
prettierConfigMeta.supported &&
(!prettierConfigMeta.content.importOrder ||
!prettierConfigMeta.content.experimentalBabelParserPluginsList)
) {
log(LOCAL_MESSAGES.RE_RUN_ALIAS, chalk.red)
}
}

public static async handle(): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/add-recommended-prettier-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function addRecommendedPrettierConfig({
quoteProps: 'consistent',
endOfLine: 'lf',
importOrder,
experimentalBabelParserPluginsList: ['jsx', 'typescript'],
}

return FileSystemReader.writeFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
AliasesMeta,
PrettierConfigMeta,
PrettierConfig,
} from '../../lib/shared-types'
import { AliasesMeta, PrettierConfigMeta, PrettierConfig } from './shared-types'

function withNesting(regex: string): string {
return regex + '(\\/(.*))?$'
Expand All @@ -27,6 +23,10 @@ export function getUpdatedPrettierConfig({
updatedConfig.importOrder = ['^[./]']
}

if (!updatedConfig.experimentalBabelParserPluginsList) {
updatedConfig.experimentalBabelParserPluginsList = ['jsx', 'typescript']
}

const newRegexps = aliasesMeta.aliasMap.map(([name]) => {
return withNesting('^' + name)
})
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type EslintConfigMeta =

export interface PrettierConfig extends Json {
importOrder?: string[]
experimentalBabelParserPluginsList?: Array<'jsx' | 'typescript' | 'flow'>
}

export type PrettierConfigMeta =
Expand Down

0 comments on commit 393c5f3

Please sign in to comment.