mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-03 04:38:33 +00:00
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:
@@ -15,63 +15,86 @@ 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 }) {
|
||||||
const project = resolveProject(args.dir);
|
try {
|
||||||
const htmlFiles = walkDir(project.dir).filter((f) => f.endsWith(".html"));
|
const project = resolveProject(args.dir);
|
||||||
|
const htmlFiles = walkDir(project.dir).filter((f) => f.endsWith(".html"));
|
||||||
|
|
||||||
const allFindings: (HyperframeLintFinding & { file: string })[] = [];
|
const allFindings: (HyperframeLintFinding & { file: string })[] = [];
|
||||||
let totalErrors = 0;
|
let totalErrors = 0;
|
||||||
let totalWarnings = 0;
|
let totalWarnings = 0;
|
||||||
|
|
||||||
for (const file of htmlFiles) {
|
for (const file of htmlFiles) {
|
||||||
const html = readFileSync(join(project.dir, file), "utf-8");
|
const html = readFileSync(join(project.dir, file), "utf-8");
|
||||||
const result = lintHyperframeHtml(html, { filePath: file });
|
const result = lintHyperframeHtml(html, { filePath: file });
|
||||||
for (const f of result.findings) {
|
for (const f of result.findings) {
|
||||||
allFindings.push({ ...f, file });
|
allFindings.push({ ...f, file });
|
||||||
|
}
|
||||||
|
totalErrors += result.errorCount;
|
||||||
|
totalWarnings += result.warningCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.json) {
|
||||||
|
console.log(
|
||||||
|
JSON.stringify(
|
||||||
|
withMeta({
|
||||||
|
ok: totalErrors === 0,
|
||||||
|
findings: allFindings,
|
||||||
|
errorCount: totalErrors,
|
||||||
|
warningCount: totalWarnings,
|
||||||
|
filesScanned: htmlFiles.length,
|
||||||
|
}),
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
process.exit(totalErrors > 0 ? 1 : 0);
|
||||||
}
|
}
|
||||||
totalErrors += result.errorCount;
|
|
||||||
totalWarnings += result.warningCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.json) {
|
|
||||||
console.log(
|
console.log(
|
||||||
JSON.stringify(
|
`${c.accent("◆")} Linting ${c.accent(project.name)} (${htmlFiles.length} HTML files)`,
|
||||||
withMeta({
|
|
||||||
ok: totalErrors === 0,
|
|
||||||
findings: allFindings,
|
|
||||||
errorCount: totalErrors,
|
|
||||||
warningCount: totalWarnings,
|
|
||||||
filesScanned: htmlFiles.length,
|
|
||||||
}),
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
console.log();
|
||||||
|
|
||||||
|
if (allFindings.length === 0) {
|
||||||
|
console.log(`${c.success("◇")} ${c.success("0 errors, 0 warnings")}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const finding of allFindings) {
|
||||||
|
const prefix = finding.severity === "error" ? c.error("✗") : c.warn("⚠");
|
||||||
|
const loc = finding.elementId ? ` ${c.accent(`[${finding.elementId}]`)}` : "";
|
||||||
|
console.log(
|
||||||
|
`${prefix} ${c.bold(finding.code)}${loc}: ${finding.message} ${c.dim(finding.file)}`,
|
||||||
|
);
|
||||||
|
if (finding.fixHint) {
|
||||||
|
console.log(` ${c.dim(`Fix: ${finding.fixHint}`)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const summaryIcon = totalErrors > 0 ? c.error("◇") : c.success("◇");
|
||||||
|
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);
|
||||||
console.log(
|
if (args.json) {
|
||||||
`${c.accent("◆")} Linting ${c.accent(project.name)} (${htmlFiles.length} HTML files)`,
|
console.log(
|
||||||
);
|
JSON.stringify(
|
||||||
console.log();
|
withMeta({
|
||||||
|
ok: false,
|
||||||
if (allFindings.length === 0) {
|
error: message,
|
||||||
console.log(`${c.success("◇")} ${c.success("0 errors, 0 warnings")}`);
|
findings: [],
|
||||||
return;
|
errorCount: 0,
|
||||||
}
|
warningCount: 0,
|
||||||
|
filesScanned: 0,
|
||||||
for (const finding of allFindings) {
|
}),
|
||||||
const prefix = finding.severity === "error" ? c.error("✗") : c.warn("⚠");
|
null,
|
||||||
const loc = finding.elementId ? ` ${c.accent(`[${finding.elementId}]`)}` : "";
|
2,
|
||||||
console.log(
|
),
|
||||||
`${prefix} ${c.bold(finding.code)}${loc}: ${finding.message} ${c.dim(finding.file)}`,
|
);
|
||||||
);
|
process.exit(1);
|
||||||
if (finding.fixHint) {
|
|
||||||
console.log(` ${c.dim(`Fix: ${finding.fixHint}`)}`);
|
|
||||||
}
|
}
|
||||||
|
console.error(message);
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const summaryIcon = totalErrors > 0 ? c.error("◇") : c.success("◇");
|
|
||||||
console.log(`\n${summaryIcon} ${totalErrors} error(s), ${totalWarnings} warning(s)`);
|
|
||||||
process.exit(totalErrors > 0 ? 1 : 0);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user