Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit with code 1 when errors are found #59

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
}
}),
)
}