| 1 | #!/usr/bin/env node |
| 2 | import * as path from "node:path" |
| 3 | import { computeJSDocInputHash, extractJSDocsSync, loadJSDocConfig, readJSDocModel, writeJSDocModel } from "./Jsdocs.ts" |
| 4 | |
| 5 | function reportDiagnostics(model: ReturnType<typeof extractJSDocsSync>, check: boolean) { |
| 6 | const diagnostics = model.files.reduce((count, file) => count + file.diagnostics.length, 0) |
| 7 | if (diagnostics > 0) { |
| 8 | for (const file of model.files) { |
| 9 | for (const diagnostic of file.diagnostics) { |
| 10 | process.stderr.write(`${file.file}: ${diagnostic.message}\n`) |
| 11 | } |
| 12 | } |
| 13 | if (check) process.exitCode = 1 |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | try { |
| 18 | const cwd = process.cwd() |
| 19 | const check = process.argv.includes("--check") |
| 20 | const config = loadJSDocConfig(cwd) |
| 21 | const inputHash = computeJSDocInputHash({ cwd, ...config }) |
| 22 | const cached = readJSDocModel(path.resolve(cwd, config.output)) |
| 23 | if (cached._tag === "Success" && cached.value.inputHash === inputHash) { |
| 24 | process.stdout.write(`Skipped ${config.output}\n`) |
| 25 | reportDiagnostics(cached.value, check) |
| 26 | } else { |
| 27 | const model = extractJSDocsSync({ cwd, ...config }) |
| 28 | writeJSDocModel(cwd, config.output, model) |
| 29 | process.stdout.write(`Wrote ${config.output}\n`) |
| 30 | reportDiagnostics(model, check) |
| 31 | } |
| 32 | } catch (error) { |
| 33 | process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`) |
| 34 | process.exitCode = 1 |
| 35 | } |