Move duration, redact, runtime, secure-file, and semver under core/util; embedded-skill and skill-version under core/skill; update-cache under core/update. Move the dev-override test next to the script it covers, and update the embedded-skill generator output path and regenerated manifest. No behavior change.
76 lines
2.9 KiB
TypeScript
76 lines
2.9 KiB
TypeScript
import { writeFileSync } from "node:fs";
|
|
import { booleanOption, stringOption } from "./parser.js";
|
|
import { PayloadTooLargeError } from "../core/errors.js";
|
|
import { redactSensitiveText } from "../core/util/redact.js";
|
|
import type { OutputOptions } from "./types.js";
|
|
|
|
export { redactSensitiveText };
|
|
|
|
const RULE_LIST_FULL_COUNT_LIMIT = 100;
|
|
const RULE_LIST_FULL_BYTES_LIMIT = 120_000;
|
|
export const RULE_SUMMARY_FIELDS = ["code", "status", "strictness", "scope", "updatedAt"];
|
|
const DEFAULT_OUTPUT_BYTES_LIMIT = 200_000;
|
|
|
|
export function formatConsoleOutput(values: unknown[]): string {
|
|
return values.map((value) => (typeof value === "string" ? value : String(value))).join(" ");
|
|
}
|
|
|
|
export function joinOutput(lines: string[]): string {
|
|
if (!lines.length) return "";
|
|
return `${lines.join("\n")}\n`;
|
|
}
|
|
|
|
export function printJson(value: unknown, options: OutputOptions = {}) {
|
|
protectLargeOutput(value, options);
|
|
const json = `${JSON.stringify(value, null, 2)}\n`;
|
|
if (options.outputFile) {
|
|
writeFileSync(options.outputFile, json);
|
|
console.log(JSON.stringify({ outputFile: options.outputFile, bytes: Buffer.byteLength(json, "utf-8") }, null, 2));
|
|
return;
|
|
}
|
|
console.log(json.trimEnd());
|
|
}
|
|
|
|
function protectLargeOutput(result: unknown, options: OutputOptions = {}) {
|
|
if (options.allowFull || options.outputFile) return;
|
|
const bodyBytes = Buffer.byteLength(JSON.stringify(result), "utf-8");
|
|
if (bodyBytes > DEFAULT_OUTPUT_BYTES_LIMIT) {
|
|
throw new PayloadTooLargeError(
|
|
`Output is large (${bodyBytes} bytes). Use --summary, --fields, --count, or --output-file; pass --full only when you really need the full response.`
|
|
);
|
|
}
|
|
}
|
|
|
|
export function protectLargeRuleListOutput(result: unknown, allowFull: boolean) {
|
|
if (allowFull) return;
|
|
|
|
const data = result && typeof result === "object" ? (result as {
|
|
count?: unknown;
|
|
rules?: unknown;
|
|
}) : {};
|
|
const count = typeof data.count === "number" ? data.count : undefined;
|
|
const rulesCount = Array.isArray(data.rules) ? data.rules.length : undefined;
|
|
const bodyBytes = Buffer.byteLength(JSON.stringify(result), "utf-8");
|
|
const effectiveCount = count ?? rulesCount ?? 0;
|
|
|
|
if (effectiveCount > RULE_LIST_FULL_COUNT_LIMIT || bodyBytes > RULE_LIST_FULL_BYTES_LIMIT) {
|
|
throw new PayloadTooLargeError(
|
|
`Rule list is large (${effectiveCount} rules, ${bodyBytes} bytes). Use "seal-use rules list --summary", "seal-use rules count", or --output-file; pass --full only when you really need full descriptions.`,
|
|
"seal-use rules list --summary"
|
|
);
|
|
}
|
|
}
|
|
|
|
export function outputOptions(options: Record<string, string | boolean>): OutputOptions {
|
|
return {
|
|
allowFull: booleanOption(options.full) === true,
|
|
outputFile: stringOption(options.outputFile) ?? stringOption(options["output-file"])
|
|
};
|
|
}
|
|
|
|
import { renderHelp } from "./help.js";
|
|
|
|
export function printHelp(target: string[] = []) {
|
|
console.log(renderHelp(target));
|
|
}
|