Repository navigation
Expand file tree
/
Copy pathformat.ts
More file actions
104 lines (90 loc) · 3.59 KB
/
Copy pathformat.ts
File metadata and controls
104 lines (90 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* format.ts — output modes, color, and argument helpers.
*
* Convention (matches Smriti): data goes to stdout, progress/errors go to
* stderr. The output MODE matches the official Tabstack CLI: pretty
* (human-readable) on a TTY, JSON when piped, forceable with -o/--output.
* Streaming commands emit one NDJSON line per event in JSON mode.
*/
export type OutputMode = "pretty" | "json";
/** A bad-invocation error: the CLI prints it with usage context and exits 2. */
export class UsageError extends Error {}
/**
* Decide the output mode. An explicit -o/--output (or the --json shorthand)
* wins; otherwise default to pretty on a terminal and json when piped, so
* `tabstack ... | jq .` just works.
*/
export function resolveMode(explicit: string | undefined, jsonFlag: boolean): OutputMode {
if (explicit) {
if (explicit !== "pretty" && explicit !== "json") {
throw new UsageError(`--output must be "pretty" or "json" (got "${explicit}")`);
}
return explicit;
}
if (jsonFlag) return "json";
return process.stdout.isTTY ? "pretty" : "json";
}
/** Pretty-print a value as JSON for json-mode output. */
export function json(value: unknown): string {
return JSON.stringify(value, null, 2);
}
/** One compact NDJSON line per stream event in json mode. */
export function ndjson(event: string, data: unknown): string {
return JSON.stringify({ event, data });
}
// ---------------------------------------------------------------------------
// Color — only when stderr is a TTY, and disabled by --no-color or the
// NO_COLOR convention (https://no-color.org).
// ---------------------------------------------------------------------------
// Gated per stream: dim() goes to stderr, green()/red() go to stdout — each
// colors only when its own stream is a TTY, so ANSI never leaks into a pipe.
let colorErr = Boolean(process.stderr.isTTY) && !process.env.NO_COLOR;
let colorOut = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
/** Turn color off (for --no-color). It can never be forced on. */
export function disableColor(): void {
colorErr = false;
colorOut = false;
}
function paint(enabled: boolean, code: string, text: string): string {
return enabled ? `\x1b[${code}m${text}\x1b[0m` : text;
}
export const dim = (t: string) => paint(colorErr, "2", t);
export const dimOut = (t: string) => paint(colorOut, "2", t);
export const green = (t: string) => paint(colorOut, "32", t);
export const red = (t: string) => paint(colorOut, "31", t);
/** Write a progress/status line to stderr so stdout stays pipeable. */
export function progress(line: string): void {
process.stderr.write(dim(line) + "\n");
}
/**
* Resolve a "schema-ish" argument into a parsed JSON object.
* Accepts:
* - "-" → read from stdin
* - "@path.json" → read from a file
* - inline JSON → parse directly
*/
export async function resolveJsonArg(value: string, label = "value"): Promise<unknown> {
const text = await resolveTextArg(value);
try {
return JSON.parse(text);
} catch (err) {
throw new Error(`Invalid JSON for ${label}: ${(err as Error).message}`);
}
}
/**
* Resolve a "text-ish" argument into a string.
* Accepts "-" (stdin), "@path" (file), or the literal string.
*/
export async function resolveTextArg(value: string): Promise<string> {
if (value === "-") return readStdin();
if (value.startsWith("@")) {
const path = value.slice(1);
const file = Bun.file(path);
if (!(await file.exists())) throw new Error(`File not found: ${path}`);
return file.text();
}
return value;
}
async function readStdin(): Promise<string> {
return Bun.stdin.text();
}