Skip to content

Commit

Permalink
refactor move out config handlening from cli.ts to analyze.ts fix #291
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Sep 19, 2024
1 parent 60f5738 commit ec49713
Show file tree
Hide file tree
Showing 10 changed files with 1,480 additions and 1,500 deletions.
2,513 changes: 1,259 additions & 1,254 deletions dist/vue-mess-detector.es.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'node:path'
import { parse } from '@vue/compiler-sfc'
import { setIsNuxt } from './context'
import { calculateCodeHealth } from './helpers/calculateCodeHealth'
import { getConfig } from './helpers/getConfig'
import getProjectRoot from './helpers/getProjectRoot'
import { groupRulesByRuleset } from './helpers/groupRulesByRuleset'
import { isNuxtProject, isVueProject } from './helpers/projectTypeChecker'
Expand Down Expand Up @@ -74,7 +75,19 @@ const walkAsync = async (dir: string) => {
return overwievMessages
}

export const analyze = async ({ dir, apply = [], ignore = [], exclude, groupBy, level, sortBy }: AnalyzeParams): Promise<AnalyzeOutput> => {
// TODO use values from config
export const analyze = async ({ dir, apply = [], ignore = [], exclude = '', groupBy = 'rule', level = 'all', sortBy = 'desc' }: AnalyzeParams): Promise<AnalyzeOutput> => {
const projectRoot = await getProjectRoot(dir)
const config = await getConfig(projectRoot)

// Use config values if not provided in cli params
apply = apply.length ? apply : config.apply.split(',')
ignore = ignore.length ? ignore : config.ignore ? config.ignore.split(',') : []
exclude = exclude || config.exclude
groupBy = groupBy || config.group
level = level || config.level
sortBy = sortBy || config.sort

const appliedRules = apply.filter(rule => !ignore.includes(rule))

const { rulesets, individualRules } = groupRulesByRuleset(appliedRules)
Expand All @@ -92,14 +105,17 @@ export const analyze = async ({ dir, apply = [], ignore = [], exclude, groupBy,
const ignoredRulesets = ignore.filter(ruleset => !rulesets.includes(ruleset as RuleSetType))
const ignoreRulesetsOutput = ignoredRulesets.length ? `<bg_info>${ignoredRulesets.join(', ')}</bg_info>` : 'N/A'

const projectRoot = await getProjectRoot(dir)
const isVue = await isVueProject(projectRoot)
const isNuxt = await isNuxtProject(projectRoot)
setIsNuxt(isNuxt)

const output: { info: string }[] = []

output.push({ info: `<bg_info>Analyzing Vue, TS and JS files in ${dir}</bg_info>` })

const configMessage = config.isDefault ? `👉 Using <bg_info>default</bg_info> configuration` : `👉 Using configuration from <bg_info>vue-mess-detector.json</bg_info>`
output.push({ info: configMessage })

output.push({ info: ` Project type: <bg_info>${isNuxt ? 'Nuxt' : ''}${isVue ? 'Vue' : ''}${!isNuxt && !isVue ? '?' : ''}</bg_info>` })
output.push({
info: `${applyingMessage}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('yarn analyze command with configuration file', () => {

it('should execute without any flags and path', async () => {
const { stdout } = await execa('yarn', ['analyze'])
expect(stdout).toContain(`👉 Using configuration from vue-mess-detector.json`)
expect(stdout).toContain(`👉 Using configuration from ${BG_INFO}vue-mess-detector.json${BG_RESET}`)
expect(stdout).toContain('Analyzing Vue, TS and JS files in ')
expect(stdout).toContain(`Applying 2 rulesets: ${BG_INFO}vue-recommended, vue-strong${BG_RESET}`)
})
Expand Down
Loading

0 comments on commit ec49713

Please sign in to comment.