fix(cli): respect --json flag on lint error paths

When lint encountered an error (e.g., invalid directory), it output
plain text even with --json, breaking JSON parsing pipelines for
agents. Now wraps the entire lint command in try-catch that emits
JSON errors when --json is set.

Reproducer:
  npx hyperframes lint --json /nonexistent/path
  # Was: plain text "Not a directory: /nonexistent/path"
  # Now: {"ok": false, "error": "Not a directory: /nonexistent/path", ...}
This commit is contained in:
Miguel Ángel
2026-03-30 18:29:47 +02:00
parent 808d196fe0
commit f3ae3e2dc8
+23
View File
@@ -15,6 +15,7 @@ export default defineCommand({
json: { type: "boolean", description: "Output findings as JSON", default: false }, json: { type: "boolean", description: "Output findings as JSON", default: false },
}, },
async run({ args }) { async run({ args }) {
try {
const project = resolveProject(args.dir); const project = resolveProject(args.dir);
const htmlFiles = walkDir(project.dir).filter((f) => f.endsWith(".html")); const htmlFiles = walkDir(project.dir).filter((f) => f.endsWith(".html"));
@@ -73,5 +74,27 @@ export default defineCommand({
const summaryIcon = totalErrors > 0 ? c.error("◇") : c.success("◇"); const summaryIcon = totalErrors > 0 ? c.error("◇") : c.success("◇");
console.log(`\n${summaryIcon} ${totalErrors} error(s), ${totalWarnings} warning(s)`); console.log(`\n${summaryIcon} ${totalErrors} error(s), ${totalWarnings} warning(s)`);
process.exit(totalErrors > 0 ? 1 : 0); process.exit(totalErrors > 0 ? 1 : 0);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
if (args.json) {
console.log(
JSON.stringify(
withMeta({
ok: false,
error: message,
findings: [],
errorCount: 0,
warningCount: 0,
filesScanned: 0,
}),
null,
2,
),
);
process.exit(1);
}
console.error(message);
process.exit(1);
}
}, },
}); });