Skip to content

Commit

Permalink
feat add nuxt / vue detection useful for #144
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Sep 2, 2024
1 parent 63d136f commit 0d067a2
Show file tree
Hide file tree
Showing 7 changed files with 1,079 additions and 1,004 deletions.
1,981 changes: 999 additions & 982 deletions dist/vue-mess-detector.es.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { checkRules } from './rulesCheck'
import { calculateCodeHealth } from './helpers/calculateCodeHealth'
import { groupRulesByRuleset } from './helpers/groupRulesByRuleset'
import type { AnalyzeParams } from './types'
import { isNuxtProject, isVueProject } from './helpers/projectTypeChecker'
import getProjectRoot from './helpers/getProjectRoot'

let filesCount = 0
let linesCount = 0
Expand Down Expand Up @@ -79,7 +81,12 @@ 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_RESET}` : 'N/A'

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

output.push({ info: `${BG_INFO}Analyzing Vue, TS and JS files in ${dir}${BG_RESET}` })
output.push({ info: ` Project type: ${BG_INFO}${isNuxt ? 'Nuxt' : ''}${isVue ? 'Vue' : ''}${!isNuxt && !isVue ? '?' : ''}${BG_RESET}` })
output.push({
info: `${applyingMessage}
Ignoring ${ignoredRulesets.length} rules/rulesets: ${ignoreRulesetsOutput}
Expand Down Expand Up @@ -109,5 +116,6 @@ export const analyze = async ({ dir, apply = [], ignore = [], exclude, groupBy,
output.push({ info: `\n${BG_OK}No code smells detected!${BG_RESET}` })
}


return { output, codeHealthOutput, reportOutput }
}
19 changes: 19 additions & 0 deletions src/helpers/getPackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import type { PackageJson } from '../types/PackageJson'

export const getPackageJson = async (projectRoot?: string): Promise<PackageJson> => {
let packageJsonPath = ''
if (!projectRoot) {
const filePath = fileURLToPath(import.meta.url)
const dirname = path.dirname(filePath)
const toolRoot = path.resolve(dirname, '..')
packageJsonPath = path.join(toolRoot, 'package.json')
}
if (projectRoot) {
packageJsonPath = path.join(projectRoot, 'package.json')
}
const packageJson: PackageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'))
return packageJson
}
19 changes: 11 additions & 8 deletions src/helpers/getProjectRoot.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import path from 'node:path'
import fs from 'node:fs/promises'

const getProjectRoot = async () => {
// eslint-disable-next-line node/prefer-global/process
let currentDir = process.cwd()
const getProjectRoot = async (startDir: string) => {
let currentDir = startDir

// eslint-disable-next-line no-unreachable-loop
while (currentDir !== path.parse(currentDir).root) {
// Check if package.json exists in the current directory
const packageJsonPath = path.join(currentDir, 'package.json')
await fs.access(packageJsonPath)
return currentDir
try {
await fs.access(packageJsonPath)
return currentDir
// eslint-disable-next-line unused-imports/no-unused-vars
} catch (e) {
// File doesn't exist, move up one directory level
currentDir = path.dirname(currentDir)
}
}

// Move up one directory level
currentDir = path.dirname(currentDir)
throw new Error('Project root not found')
}

export default getProjectRoot
29 changes: 29 additions & 0 deletions src/helpers/projectTypeChecker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'node:fs'
import path from 'node:path'
import getProjectRoot from './getProjectRoot'
import { getPackageJson } from './getPackageJson'

// eslint-disable-next-line node/prefer-global/process
const projectPath = await getProjectRoot(process.cwd()) || ''

const checkDependency = async (dependencyName: 'vue' | 'nuxt', projectRoot: string) => {
const packageJsonPath = path.join(projectPath, 'package.json')
if (fs.existsSync(packageJsonPath)) {
const packageJson = await getPackageJson(projectRoot)
return Boolean(packageJson.dependencies[dependencyName])
}
return false
}

const isNuxtProject = async (projectRoot: string) => {
const nuxtConfigFiles = ['nuxt.config.js', 'nuxt.config.ts']
return await checkDependency('nuxt', projectRoot) || nuxtConfigFiles.some(file => fs.existsSync(path.join(projectPath, file)))
}

const isVueProject = async (projectRoot: string) => {
const vueConfigFiles = ['vue.config.js', 'vue.config.ts']
const isNuxt = await isNuxtProject(projectRoot)
return !isNuxt && (await checkDependency('vue', projectRoot) || vueConfigFiles.some(file => fs.existsSync(path.join(projectPath, file))))
}

export { isNuxtProject, isVueProject }
20 changes: 6 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import Table from 'cli-table3'
Expand All @@ -13,20 +12,13 @@ import getProjectRoot from './helpers/getProjectRoot'
import coerceRules from './helpers/coerceRules'
import { FLAT_RULESETS_RULES } from './helpers/constants'
import { OUTPUT_FORMATS } from './types'
import { getPackageJson } from './helpers/getPackageJson'

const projectRoot = await getProjectRoot()
if (!projectRoot) {
console.error(`\n${BG_ERR}Cannot find project root.${BG_RESET}\n\n`)
// eslint-disable-next-line node/prefer-global/process
process.exit(1)
}

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const toolRoot = path.resolve(__dirname, '..')
const packageJsonPath = path.join(toolRoot, 'package.json')
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'))
// eslint-disable-next-line node/prefer-global/process
const pathArg = process.argv[2] == 'analyze' ? process.argv[3] : process.argv[4]
const projectRoot = await getProjectRoot(pathArg || './src')

const vmdPackageJson = await getPackageJson()
const configOutput: { info: string }[] = []

let config = {
Expand Down Expand Up @@ -188,6 +180,6 @@ yargs(hideBin(process.argv))
})
},
)
.version('version', 'Show version number', packageJson.version)
.version('version', 'Show version number', vmdPackageJson.version)
.alias('version', 'v')
.help().argv
7 changes: 7 additions & 0 deletions src/types/PackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface PackageJson {
version: string
dependencies: {
vue?: string
nuxt?: string
}
}

0 comments on commit 0d067a2

Please sign in to comment.