Skip to content

Commit

Permalink
Add JSON Reporter (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost authored Oct 26, 2024
1 parent 335be11 commit c0d66e9
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/steiger/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { linter } from './app'
import { processConfiguration } from './models/config'
import { applyAutofixes } from './features/autofix'
import fsd from '@feature-sliced/steiger-plugin'
import type { Diagnostic } from '@steiger/types'

const yargsProgram = yargs(hideBin(process.argv))
.scriptName('steiger')
Expand All @@ -34,6 +35,13 @@ const yargsProgram = yargs(hideBin(process.argv))
describe: 'exit with an error code if there are warnings',
type: 'boolean',
})
.option('reporter', {
demandOption: false,
describe: 'specify output format (pretty or json)',
type: 'string',
choices: ['pretty', 'json'],
default: 'pretty',
})
.string('_')
.check((argv) => {
const filePaths = argv._
Expand Down Expand Up @@ -81,11 +89,19 @@ try {
process.exit(101)
}

const printDiagnostics = (diagnostics: Array<Diagnostic>) => {
if (consoleArgs.reporter === 'json') {
console.log(JSON.stringify(diagnostics, null, 2))
} else {
reportPretty(diagnostics, process.cwd())
}
}

if (consoleArgs.watch) {
const [diagnosticsChanged, stopWatching] = await linter.watch(targetPath)
const unsubscribe = diagnosticsChanged.watch((state) => {
console.clear()
reportPretty(state, process.cwd())
printDiagnostics(state)
if (consoleArgs.fix) {
applyAutofixes(state)
}
Expand All @@ -98,7 +114,7 @@ if (consoleArgs.watch) {
const diagnostics = await linter.run(targetPath)
let stillRelevantDiagnostics = diagnostics

reportPretty(diagnostics, process.cwd())
printDiagnostics(diagnostics)

if (consoleArgs.fix) {
stillRelevantDiagnostics = await applyAutofixes(diagnostics)
Expand Down

0 comments on commit c0d66e9

Please sign in to comment.