Skip to content

Commit

Permalink
Exit with code 1 when errors are found (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniilsapa authored Jul 19, 2024
1 parent b1866e8 commit 79f3552
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-crews-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'steiger': patch
---

Make Steiger exit with 1 if errors are reported
8 changes: 7 additions & 1 deletion packages/steiger/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ if (consoleArgs.watch) {
})
} else {
const diagnostics = await linter.run(resolve(consoleArgs._[0]))
let stillRelevantDiagnostics = diagnostics

reportPretty(diagnostics, process.cwd())

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

if (stillRelevantDiagnostics.length > 0) {
process.exit(1)
}
}
69 changes: 49 additions & 20 deletions packages/steiger/src/features/autofix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,55 @@ import { dirname, join } from 'node:path'
import { rename, open, mkdir, rm } from 'node:fs/promises'
import type { Diagnostic } from '@steiger/types'

export async function applyAutofixes(diagnostics: Array<Diagnostic>) {
export async function applyAutofixes<T extends Diagnostic>(diagnostics: Array<T>): Promise<T[]> {
const stillRelevantDiagnostics = []
const fixableDiagnostics = []

for (const diagnostic of diagnostics) {
const fixes = diagnostic.fixes

if (!fixes) {
// If we don't know how to fix, it's relevant right away
stillRelevantDiagnostics.push(diagnostic)
continue
}

fixableDiagnostics.push(diagnostic)
}

try {
await Promise.all(fixableDiagnostics.map(tryToApplyFixes))
} catch (error) {
// If for some reason, a fix failed
// then assume the diagnostics are still relevant
// TODO: enhance it to push only failed fixes instead of all
stillRelevantDiagnostics.push(...fixableDiagnostics)
console.error(error)
}

return stillRelevantDiagnostics
}

async function tryToApplyFixes(diagnostic: Diagnostic) {
const fixes = diagnostic.fixes ?? []

return Promise.all(
diagnostics
.flatMap((diagnostic) => diagnostic.fixes ?? [])
.map((fix) => {
switch (fix.type) {
case 'rename':
return rename(fix.path, join(dirname(fix.path), fix.newName))
case 'create-file':
return open(fix.path, 'w').then((file) => file.close())
case 'create-folder':
return mkdir(fix.path, { recursive: true })
case 'delete':
return rm(fix.path, { recursive: true })
case 'modify-file':
return open(fix.path, 'w').then(async (file) => {
await file.write(fix.content)
return file.close()
})
}
}),
fixes.map((fix) => {
switch (fix.type) {
case 'rename':
return rename(fix.path, join(dirname(fix.path), fix.newName))
case 'create-file':
return open(fix.path, 'w').then((file) => file.close())
case 'create-folder':
return mkdir(fix.path, { recursive: true })
case 'delete':
return rm(fix.path, { recursive: true })
case 'modify-file':
return open(fix.path, 'w').then(async (file) => {
await file.write(fix.content)
return file.close()
})
}
}),
)
}

0 comments on commit 79f3552

Please sign in to comment.