Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/body-files.ts?format=json
86 linesFirst import 88% · pinch 10% · infra-split 1%Last change pinch in version 281, 5 versions changed it
1// Bodies from a file or stdin: Windows PowerShell 5.1
2// passes a native command's argument badly, so a multi-line --comment,
3// --description or answer reached the server cut to its first line, and nobody
4// knew. Any text flag also takes --<flag>-file <path>, and the commands whose
5// text is their words (ask, answer, reply, announce) take --file <path>. A path of -
6// reads stdin. The file's text arrives whole on any shell.
7
8import { readFileSync } from "node:fs";
9import { resolve } from "node:path";
10import { type FlagSpec, type Parsed, UsageError } from "./args.ts";
12/** The text flags a command may take from a file instead. */
13const BODY_FLAGS = ["message", "comment", "object", "description", "note"] as const;
14/** Commands whose text is their last words: --file gives those words. */
15const WORDS_FROM_FILE = new Set(["ask", "answer", "reply", "announce"]);
17/** The command's flags, plus a -file twin for each text flag it takes, and --file where its words are its text. */
18export function withBodyFiles(command: string, spec: FlagSpec): FlagSpec {
19 const more: Record<string, "string"> = {};
20 for (const f of BODY_FLAGS) if (spec[f] === "string") more[`${f}-file`] = "string";
21 if (WORDS_FROM_FILE.has(command)) more.file = "string";
22 return { ...spec, ...more };
25/** The line --help adds for these flags, or none. */
26export function bodyFilesHelp(command: string, spec: FlagSpec): string | undefined {
27 const flags = BODY_FLAGS.filter((f) => spec[f] === "string").map((f) => `--${f}-file`);
28 if (WORDS_FROM_FILE.has(command)) flags.push("--file");
29 if (flags.length === 0) return undefined;
30 return `${flags.join(", ")} <path> read that text from a file, or stdin with -, so text with several lines arrives whole on any shell (Windows PowerShell 5.1 cuts a quoted argument at its first new line).`;
33/**
34 * Reads each -file flag given into the flag it stands for, and --file into the
35 * command's words, after the words before it. One trailing new line is
36 * dropped, as a shell's $(cat file) would. An empty text is refused.
37 */
38export function readBodyFiles(
39 command: string,
40 args: Parsed,
41 cwd: string,
42 readStdin: () => string = () => readFileSync(0, "utf8"),
43): Parsed {
44 let stdinUsed = false;
45 const read = (flag: string, path: string): string => {
46 let text: string;
47 if (path === "-") {
48 if (stdinUsed) throw new UsageError("only one of the -file flags can read stdin (-)");
49 stdinUsed = true;
50 text = readStdin();
51 } else {
52 try {
53 text = readFileSync(resolve(cwd, path), "utf8");
54 } catch {
55 throw new UsageError(`--${flag} ${path}: no such file to read`);
56 }
57 }
58 // A file saved on Windows has CRLF line endings; the text goes up with LF only, as typed text does
59 // (the Issues Worker refuses a carriage return as a character a reader can't see).
60 text = text.replace(/\r\n?/g, "\n").replace(/\n$/, "");
61 if (text.trim() === "") throw new UsageError(`--${flag} ${path}: the text is empty`);
62 return text;
63 };
64 const flags = new Map(args.flags);
65 let positionals = [...args.positionals];
66 for (const f of BODY_FLAGS) {
67 // A lone - as the text is a stdin read meant for the -file flag (abso issue 182): posting "-" helps nobody.
68 if (flags.get(f) === "-")
69 throw new UsageError(`--${f} - would send just "-"; --${f}-file - reads the text from stdin`);
70 const path = flags.get(`${f}-file`);
71 if (typeof path !== "string") continue;
72 if (flags.has(f)) throw new UsageError(`--${f} or --${f}-file, not both`);
73 flags.set(f, read(`${f}-file`, path));
74 flags.delete(`${f}-file`);
75 }
76 const path = flags.get("file");
77 if (WORDS_FROM_FILE.has(command) && typeof path === "string") {
78 // ask <who>, answer <ask> and reply <ask> keep their first word; announce's words are all text.
79 const keep = command === "announce" ? 0 : 1;
80 if (positionals.length > keep)
81 throw new UsageError(`--file gives ${command}'s text, so leave the words off the command line`);
82 positionals = [...positionals, read("file", path)];
83 flags.delete("file");
84 }
85 return { ...args, positionals, flags };