Move parser, tokenize, and area-aliases under cli/parse; runner and read-only under cli/run; printer under cli/output; help plus the docs/naming contract tests under cli/help. types.ts and helpers.ts stay at the cli root. Update imports, the app-root depth calculation, test fixtures, and the CLI doc generator path. No behavior change.
264 lines
11 KiB
TypeScript
264 lines
11 KiB
TypeScript
import { resolve } from "node:path";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
import type { createSealClient } from "../../core/http/factory.js";
|
|
import { usageError } from "../../core/errors.js";
|
|
import { runCapability, runCapabilityResult } from "../helpers.js";
|
|
import { exportDocument, readDocumentBaseline, readDocumentText } from "../../domains/seal/documents/files.js";
|
|
import { DocumentMutationError } from "../../domains/seal/documents/service.js";
|
|
import { ApprovalDocumentSchema } from "../../domains/seal/types.js";
|
|
import {
|
|
booleanOption,
|
|
jsonObject,
|
|
numberOption,
|
|
splitOption,
|
|
stringOption
|
|
} from "../parse/parser.js";
|
|
import { outputOptions, printJson } from "../output/printer.js";
|
|
import type { ParsedArgs } from "../types.js";
|
|
|
|
function contentFilePath(args: ParsedArgs): string | undefined {
|
|
return (
|
|
stringOption(args.options.contentFile) ??
|
|
stringOption(args.options["content-file"]) ??
|
|
stringOption(args.options.file) ??
|
|
stringOption(args.options.f)
|
|
);
|
|
}
|
|
|
|
function readContentInput(args: ParsedArgs, payload: Record<string, unknown>): string | undefined {
|
|
const filePath = contentFilePath(args);
|
|
const fileInputs = ["contentFile", "content-file", "file", "f"].filter(key => args.options[key] !== undefined).length;
|
|
const sources = fileInputs + Number(args.options.content !== undefined) + Number(payload.content !== undefined);
|
|
if (sources > 1) throw usageError("Specify exactly one document content source: --content-file, --content, or JSON content");
|
|
if (filePath) {
|
|
return readDocumentText(filePath);
|
|
}
|
|
const direct = stringOption(args.options.content);
|
|
if (direct !== undefined) return direct;
|
|
return typeof payload.content === "string" ? payload.content : undefined;
|
|
}
|
|
|
|
export async function handleDocumentsCommand(
|
|
action: string | undefined,
|
|
maybeName: string | undefined,
|
|
client: Awaited<ReturnType<typeof createSealClient>>,
|
|
corp: CorpConfig,
|
|
args: ParsedArgs
|
|
) {
|
|
const out = stringOption(args.options.out);
|
|
const format = stringOption(args.options["content-format"]) ?? stringOption(args.options.contentFormat);
|
|
if (format !== undefined && format !== "raw") throw usageError("--content-format supports raw only; Markdown conversion requires a verified platform codec");
|
|
if (out && action !== "get") throw usageError("--out is supported only by documents get");
|
|
if (format !== undefined && !["get", "create", "update"].includes(action ?? "")) throw usageError("--content-format is supported only by documents get/create/update");
|
|
if (out && outputOptions(args.options).outputFile) throw usageError("--out (body) and --output-file (JSON) are mutually exclusive");
|
|
if (booleanOption(args.options.overwrite) && !out) throw usageError("--overwrite requires documents get --out");
|
|
const contentPath = contentFilePath(args);
|
|
const jsonOutput = outputOptions(args.options).outputFile;
|
|
if (contentPath && jsonOutput && [resolve(contentPath), `${resolve(contentPath)}.seal-use.json`].includes(resolve(jsonOutput))) {
|
|
throw usageError("--output-file must not overwrite the input body or its export baseline");
|
|
}
|
|
|
|
const safety = {
|
|
dryRun: booleanOption(args.options["dry-run"]) ?? booleanOption(args.options.dryRun),
|
|
confirm: booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true,
|
|
allowEmpty: booleanOption(args.options["allow-empty"]) ?? booleanOption(args.options.allowEmpty)
|
|
};
|
|
const mutate = async (name: string, params: Record<string, unknown>) => {
|
|
try {
|
|
printJson(await runCapabilityResult(name, client, corp, params), outputOptions(args.options));
|
|
} catch (error) {
|
|
if (error instanceof DocumentMutationError) printJson(error.payload, outputOptions(args.options));
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
if (!action || action === "list") {
|
|
const scenarios = splitOption(args.options.scenarios);
|
|
const limit = numberOption(args.options.limit);
|
|
const offset = numberOption(args.options.offset);
|
|
await runCapability(
|
|
"sealApprovalDocumentsList",
|
|
client,
|
|
corp,
|
|
{ scenarios, limit, offset, summary: booleanOption(args.options.summary), fields: splitOption(args.options.fields) },
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "get") {
|
|
const documentId =
|
|
(maybeName && !maybeName.startsWith("-") ? maybeName : undefined) ??
|
|
stringOption(args.options.documentId) ??
|
|
stringOption(args.options["document-id"]) ??
|
|
stringOption(args.options.id);
|
|
if (!documentId) {
|
|
throw usageError("Usage: seal-use documents get <documentId> [--corp <corpId>]");
|
|
}
|
|
if (out) {
|
|
const document = ApprovalDocumentSchema.parse(await runCapabilityResult("sealApprovalDocumentGet", client, corp, { documentId }));
|
|
printJson(exportDocument(corp, document, out, booleanOption(args.options.overwrite)));
|
|
return;
|
|
}
|
|
await runCapability(
|
|
"sealApprovalDocumentGet",
|
|
client,
|
|
corp,
|
|
{ documentId },
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "search") {
|
|
const keyword = maybeName ?? stringOption(args.options.query);
|
|
if (!keyword) {
|
|
throw usageError("Usage: seal-use documents search <keyword> [--max-results 20] [--corp <corpId>]");
|
|
}
|
|
await runCapability(
|
|
"sealApprovalSearch",
|
|
client,
|
|
corp,
|
|
{
|
|
keywords: [keyword],
|
|
areas: ["documents"],
|
|
maxResults: numberOption(args.options["max-results"]) ?? numberOption(args.options.maxResults),
|
|
contextLines: numberOption(args.options["context-lines"]) ?? numberOption(args.options.contextLines),
|
|
refresh: booleanOption(args.options.refresh),
|
|
snippetOnly: booleanOption(args.options.snippetOnly) ?? booleanOption(args.options["snippet-only"]),
|
|
maxChars: numberOption(args.options.maxChars) ?? numberOption(args.options["max-chars"]),
|
|
fields: splitOption(args.options.fields)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "create") {
|
|
const payload = jsonObject(args.json);
|
|
const title = stringOption(args.options.title) ?? (typeof payload.title === "string" ? payload.title : undefined);
|
|
const purpose = stringOption(args.options.purpose) ?? (typeof payload.purpose === "string" ? payload.purpose : undefined);
|
|
const content = readContentInput(args, payload);
|
|
const contentType =
|
|
stringOption(args.options.type) ??
|
|
stringOption(args.options.contentType) ??
|
|
stringOption(args.options["content-type"]) ??
|
|
(typeof payload.contentType === "string" ? payload.contentType : "policy");
|
|
const status =
|
|
stringOption(args.options.status) ??
|
|
(typeof payload.status === "string" ? payload.status : "published");
|
|
const enabled =
|
|
booleanOption(args.options.enabled) ??
|
|
(typeof payload.enabled === "boolean" ? payload.enabled : undefined);
|
|
const scenarios =
|
|
splitOption(args.options.scenarios) ??
|
|
(Array.isArray(payload.scenarios) ? (payload.scenarios as string[]) : ["audit"]);
|
|
const tags =
|
|
splitOption(args.options.tags) ??
|
|
(Array.isArray(payload.tags) ? (payload.tags as string[]) : undefined);
|
|
|
|
if (!title || !purpose || content === undefined) {
|
|
throw usageError("Usage: seal-use documents create --title <title> --purpose <purpose> [--content <text>|--content-file <path>] [--type <policy|document|video|pdf|feishu_doc>] [--scenarios <audit,fill>] [--tags <tag1,tag2>] [--status <published|draft>] [--corp <corpId>]");
|
|
}
|
|
|
|
await mutate(
|
|
"sealApprovalDocumentCreate",
|
|
{
|
|
title,
|
|
purpose,
|
|
content,
|
|
contentType,
|
|
status,
|
|
enabled,
|
|
scenarios,
|
|
tags,
|
|
...safety
|
|
}
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "update") {
|
|
const payload = jsonObject(args.json);
|
|
const documentId =
|
|
(maybeName && !maybeName.startsWith("-") ? maybeName : undefined) ??
|
|
stringOption(args.options.documentId) ??
|
|
stringOption(args.options["document-id"]) ??
|
|
stringOption(args.options.id) ??
|
|
(typeof payload.documentId === "string" ? payload.documentId : undefined);
|
|
const title = stringOption(args.options.title) ?? (typeof payload.title === "string" ? payload.title : undefined);
|
|
const purpose = stringOption(args.options.purpose) ?? (typeof payload.purpose === "string" ? payload.purpose : undefined);
|
|
const content = readContentInput(args, payload);
|
|
const contentType =
|
|
stringOption(args.options.type) ??
|
|
stringOption(args.options.contentType) ??
|
|
stringOption(args.options["content-type"]) ??
|
|
(typeof payload.contentType === "string" ? payload.contentType : undefined);
|
|
const status =
|
|
stringOption(args.options.status) ??
|
|
(typeof payload.status === "string" ? payload.status : undefined);
|
|
const enabled =
|
|
booleanOption(args.options.enabled) ??
|
|
(typeof payload.enabled === "boolean" ? payload.enabled : undefined);
|
|
const scenarios =
|
|
splitOption(args.options.scenarios) ??
|
|
(Array.isArray(payload.scenarios) ? (payload.scenarios as string[]) : undefined);
|
|
const tags =
|
|
splitOption(args.options.tags) ??
|
|
(Array.isArray(payload.tags) ? (payload.tags as string[]) : undefined);
|
|
|
|
const hasChanges =
|
|
title !== undefined ||
|
|
purpose !== undefined ||
|
|
content !== undefined ||
|
|
contentType !== undefined ||
|
|
status !== undefined ||
|
|
enabled !== undefined ||
|
|
scenarios !== undefined ||
|
|
tags !== undefined;
|
|
|
|
if (!documentId || !hasChanges) {
|
|
throw usageError("Usage: seal-use documents update <documentId> [--title <title>] [--purpose <purpose>] [--content <text>|--content-file <path>] [--type <policy|document|video|pdf|feishu_doc>] [--enabled <true|false>] [--scenarios <audit,fill>] [--tags <tag1,tag2>] [--status <published|draft>] [--corp <corpId>]");
|
|
}
|
|
|
|
const filePath = contentFilePath(args);
|
|
const baseline = filePath ? readDocumentBaseline(filePath) : undefined;
|
|
if (content !== undefined && !baseline) console.error("Document input has no export baseline; changes made by others during editing cannot be detected (baselineAvailable:false).");
|
|
await mutate(
|
|
"sealApprovalDocumentUpdate",
|
|
{
|
|
documentId,
|
|
title,
|
|
purpose,
|
|
content,
|
|
contentType,
|
|
status,
|
|
enabled,
|
|
scenarios,
|
|
tags,
|
|
baseline,
|
|
...safety
|
|
}
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "delete") {
|
|
const documentId =
|
|
(maybeName && !maybeName.startsWith("-") ? maybeName : undefined) ??
|
|
stringOption(args.options.documentId) ??
|
|
stringOption(args.options["document-id"]) ??
|
|
stringOption(args.options.id);
|
|
if (!documentId) {
|
|
throw usageError("Usage: seal-use documents delete <documentId> --yes [--corp <corpId>]");
|
|
}
|
|
await mutate(
|
|
"sealApprovalDeleteDocument",
|
|
{ documentId, dryRun: safety.dryRun, confirm: safety.confirm }
|
|
);
|
|
return;
|
|
}
|
|
|
|
throw usageError(`Unknown documents command: ${action}. Available: list, get, search, create, update, delete.`);
|
|
}
|