Skip to content

Commit

Permalink
fix: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 7, 2024
1 parent 672a976 commit cd4e2b6
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 614 deletions.
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
}
},
"devDependencies": {
"@biomejs/biome": "^1.7.2",
"@biomejs/biome": "^1.8.3",
"@types/eslint": "^7.2.14",
"@types/fs-extra": "^11.0.1",
"@vue/language-core": "^2.0.14",
Expand Down
7 changes: 1 addition & 6 deletions packages/vite-plugin-checker/src/FileDiagnosticManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ class FileDiagnosticManager {
public diagnostics: NormalizedDiagnostic[] = []

/**
* Resets the diagnostics array
* Initialize and reset the diagnostics array
*/
public initWith(diagnostics: NormalizedDiagnostic[]) {
if (this.initialized) {
throw new Error('FileDiagnosticManager is already initialized')
}

this.diagnostics = [...diagnostics]
this.initialized = true
}

public getDiagnostics(fileName?: string) {
Expand Down
41 changes: 27 additions & 14 deletions packages/vite-plugin-checker/src/checkers/biome/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { exec } from 'child_process'
import { type NormalizedDiagnostic } from '../../logger.js'
import { exec } from 'node:child_process'
import strip from 'strip-ansi'
import { createFrame } from '../../codeFrame.js'
import type { NormalizedDiagnostic } from '../../logger.js'
import { DiagnosticLevel } from '../../types.js'
import type { BiomeOutput } from './types.js'

Expand All @@ -14,11 +16,17 @@ export function getBiomeCommand(command: string, flags: string, files: string) {
return ['biome', command || 'lint', flags, defaultFlags, files].filter(Boolean).join(' ')
}

export function runBiome(command: string) {
export function runBiome(command: string, cwd: string) {
return new Promise<NormalizedDiagnostic[]>((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
resolve([...parseBiomeOutput(stdout)])
})
exec(
command,
{
cwd,
},
(error, stdout, stderr) => {
resolve([...parseBiomeOutput(stdout)])
}
)
})
}

Expand All @@ -31,18 +39,23 @@ function parseBiomeOutput(output: string) {
}

const diagnostics: NormalizedDiagnostic[] = parsed.diagnostics.map((d) => {
const loc = {
file: d.location.path || '',
start: getLineAndColumn(d.location.sourceCode, d.location.span?.[0]),
end: getLineAndColumn(d.location.sourceCode, d.location.span?.[1]),
}

const codeFrame = createFrame(d.location.sourceCode || '', loc)

return {
level: severityMap[d.severity as keyof typeof severityMap] ?? DiagnosticLevel.Error,
message: `[${d.category}] ${d.description}`,
conclusion: '',
level: severityMap[d.severity as keyof typeof severityMap] ?? DiagnosticLevel.Error,
checker: 'Biome',
id: d.location.path.file,
codeFrame: d.source || '',
loc: {
file: d.location.path.file,
start: getLineAndColumn(d.location.sourceCode, d.location.span?.[0]),
end: getLineAndColumn(d.location.sourceCode, d.location.span?.[1]),
},
id: d.location.path?.file,
codeFrame,
stripedCodeFrame: codeFrame && strip(codeFrame),
loc,
}
})

Expand Down
28 changes: 14 additions & 14 deletions packages/vite-plugin-checker/src/checkers/biome/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { fileURLToPath } from 'url'
import { fileURLToPath } from 'node:url'
import chokidar from 'chokidar'

import path from 'node:path'
import { parentPort } from 'node:worker_threads'
import { Checker } from '../../Checker.js'
import { ACTION_TYPES, DiagnosticLevel, type CreateDiagnostic } from '../../types.js'
import { FileDiagnosticManager } from '../../FileDiagnosticManager.js'
import {
composeCheckerSummary,
consoleLog,
Expand All @@ -11,15 +13,13 @@ import {
filterLogLevel,
toClientPayload,
} from '../../logger.js'
import { FileDiagnosticManager } from '../../FileDiagnosticManager.js'
import { parentPort } from 'worker_threads'
import path from 'path'
import { ACTION_TYPES, type CreateDiagnostic, DiagnosticLevel } from '../../types.js'
import { getBiomeCommand, runBiome, severityMap } from './cli.js'

const __filename = fileURLToPath(import.meta.url)

const manager = new FileDiagnosticManager()
let createServeAndBuild
let createServeAndBuild: any

const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {
let overlay = true
Expand Down Expand Up @@ -53,9 +53,10 @@ const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {
const diagnostics = filterLogLevel(manager.getDiagnostics(), logLevel)

if (terminal) {
diagnostics.forEach((d) => {
for (const d of diagnostics) {
consoleLog(diagnosticToTerminalLog(d, 'Biome'))
})
}

const errorCount = diagnostics.filter((d) => d.level === DiagnosticLevel.Error).length
const warningCount = diagnostics.filter((d) => d.level === DiagnosticLevel.Warning).length
consoleLog(composeCheckerSummary('Biome', errorCount, warningCount))
Expand All @@ -81,11 +82,11 @@ const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {

if (isConfigFile) {
const runCommand = getBiomeCommand(command, flags, root)
const diagnostics = await runBiome(runCommand)
const diagnostics = await runBiome(runCommand, root)
manager.initWith(diagnostics)
} else {
const runCommand = getBiomeCommand(command, flags, absPath)
const diagnosticsOfChangedFile = await runBiome(runCommand)
const diagnosticsOfChangedFile = await runBiome(runCommand, root)
manager.updateByFileId(absPath, diagnosticsOfChangedFile)
}
}
Expand All @@ -95,10 +96,9 @@ const createDiagnostic: CreateDiagnostic<'biome'> = (pluginConfig) => {

// initial check
const runCommand = getBiomeCommand(command, flags, root)
const diagnostics = await runBiome(runCommand)
const diagnostics = await runBiome(runCommand, root)

manager.initWith(diagnostics)

dispatchDiagnostics()

// watch lint
Expand Down Expand Up @@ -126,9 +126,9 @@ export class BiomeChecker extends Checker<'biome'> {
buildBin: (pluginConfig) => {
if (typeof pluginConfig.biome === 'object') {
const { command, flags } = pluginConfig.biome
return ['biome', [command || 'check', flags || ''] as const]
return ['biome', [command || 'lint', flags || ''] as const]
}
return ['biome', ['check']]
return ['biome', ['lint']]
},
},
createDiagnostic,
Expand Down
Loading

0 comments on commit cd4e2b6

Please sign in to comment.