@sous-io/sous-plugin-api 0.0.0-stage → 1.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -2
- package/dist/command-variables.d.ts +13 -0
- package/dist/command-variables.js +28 -0
- package/dist/config.d.ts +235 -0
- package/dist/config.js +1 -0
- package/dist/context.d.ts +159 -0
- package/dist/context.js +1 -0
- package/dist/entities.d.ts +60 -0
- package/dist/entities.js +1 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.js +138 -0
- package/dist/events.d.ts +90 -0
- package/dist/events.js +1 -0
- package/dist/handlers.d.ts +47 -0
- package/dist/handlers.js +16 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +15 -0
- package/dist/intents.d.ts +158 -0
- package/dist/intents.js +1 -0
- package/dist/json.d.ts +84 -0
- package/dist/json.js +7 -0
- package/dist/node.d.ts +81 -0
- package/dist/node.js +1 -0
- package/dist/outputs.d.ts +199 -0
- package/dist/outputs.js +292 -0
- package/dist/plugin.d.ts +181 -0
- package/dist/plugin.js +18 -0
- package/dist/prompter.d.ts +66 -0
- package/dist/prompter.js +32 -0
- package/dist/registry.d.ts +91 -0
- package/dist/registry.js +1 -0
- package/dist/report-styles.d.ts +221 -0
- package/dist/report-styles.js +60 -0
- package/dist/reporter.d.ts +270 -0
- package/dist/reporter.js +392 -0
- package/dist/schema.d.ts +131 -0
- package/dist/schema.js +211 -0
- package/dist/services.d.ts +68 -0
- package/dist/services.js +1 -0
- package/dist/timing.d.ts +31 -0
- package/dist/timing.js +22 -0
- package/package.json +34 -4
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plain JSON values, and the patches that change them.
|
|
3
|
+
*
|
|
4
|
+
* The config is always plain JSON, and every change to it, or to any
|
|
5
|
+
* document a plugin edits, is a list of primitive patches.
|
|
6
|
+
*/
|
|
7
|
+
/** A plain JSON value. */
|
|
8
|
+
export type JsonValue = JsonPrimitive | JsonArray | JsonObject;
|
|
9
|
+
/** A JSON scalar. */
|
|
10
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
11
|
+
/** A JSON array. */
|
|
12
|
+
export type JsonArray = JsonValue[];
|
|
13
|
+
/** A JSON object. */
|
|
14
|
+
export interface JsonObject {
|
|
15
|
+
[key: string]: JsonValue;
|
|
16
|
+
}
|
|
17
|
+
/** One step of a path: an object key, or an array index. */
|
|
18
|
+
export type PathSegment = string | number;
|
|
19
|
+
/**
|
|
20
|
+
* A path, as segments (`["subscriptions", "workflow/x", "enabled"]`) or as a string
|
|
21
|
+
* (`subscriptions."workflow/x".enabled`, `compilation.targets[7].id`). `""` and `[]` name the root.
|
|
22
|
+
*/
|
|
23
|
+
export type PathInput = string | readonly PathSegment[];
|
|
24
|
+
/** Sets one key of an object, creating missing objects along the path. */
|
|
25
|
+
export interface SetKeyPatch {
|
|
26
|
+
op: "setKey";
|
|
27
|
+
/** The object holding the key, as a canonical path string. */
|
|
28
|
+
path: string;
|
|
29
|
+
key: string;
|
|
30
|
+
value: JsonValue;
|
|
31
|
+
}
|
|
32
|
+
/** Deletes one key of an object. Deleting a missing key does nothing. */
|
|
33
|
+
export interface DeleteKeyPatch {
|
|
34
|
+
op: "deleteKey";
|
|
35
|
+
path: string;
|
|
36
|
+
key: string;
|
|
37
|
+
}
|
|
38
|
+
/** Renames one key of an object, keeping its place. An existing key named `to` is replaced. */
|
|
39
|
+
export interface RenameKeyPatch {
|
|
40
|
+
op: "renameKey";
|
|
41
|
+
path: string;
|
|
42
|
+
key: string;
|
|
43
|
+
to: string;
|
|
44
|
+
}
|
|
45
|
+
/** Sets one item of an array. */
|
|
46
|
+
export interface SetIndexPatch {
|
|
47
|
+
op: "setIndex";
|
|
48
|
+
path: string;
|
|
49
|
+
index: number;
|
|
50
|
+
value: JsonValue;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Inserts one item into an array, creating the array when it is missing. `"end"` appends, so an
|
|
54
|
+
* append never depends on the array's length when the patch was made.
|
|
55
|
+
*/
|
|
56
|
+
export interface InsertAtPatch {
|
|
57
|
+
op: "insertAt";
|
|
58
|
+
path: string;
|
|
59
|
+
index: number | "end";
|
|
60
|
+
value: JsonValue;
|
|
61
|
+
}
|
|
62
|
+
/** Removes one item from an array. */
|
|
63
|
+
export interface RemoveIndexPatch {
|
|
64
|
+
op: "removeIndex";
|
|
65
|
+
path: string;
|
|
66
|
+
index: number;
|
|
67
|
+
}
|
|
68
|
+
/** One primitive operation. Every other operation is a helper built on these. */
|
|
69
|
+
export type Patch = SetKeyPatch | DeleteKeyPatch | RenameKeyPatch | SetIndexPatch | InsertAtPatch | RemoveIndexPatch;
|
|
70
|
+
/** The name of a primitive operation. */
|
|
71
|
+
export type PatchOp = Patch["op"];
|
|
72
|
+
/**
|
|
73
|
+
* A named group of patches and other patch sets. A helper's work is one patch set
|
|
74
|
+
* (`removeWhere compilation.targets`), and one contributor run produces one patch set.
|
|
75
|
+
*/
|
|
76
|
+
export interface PatchSet {
|
|
77
|
+
/** What made it: a helper name (`removeWhere`) or a contributor id (`confd/610-mac.jsonc`). */
|
|
78
|
+
name: string;
|
|
79
|
+
/** The path the helper worked on, when it worked on one. */
|
|
80
|
+
path?: string;
|
|
81
|
+
patches: Array<Patch | PatchSet>;
|
|
82
|
+
}
|
|
83
|
+
/** The kind of a JSON value. */
|
|
84
|
+
export type ValueType = "object" | "array" | "string" | "number" | "boolean" | "null";
|
package/dist/json.js
ADDED
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { JsonValue, PathInput, PathSegment, ValueType } from "./json.js";
|
|
2
|
+
/**
|
|
3
|
+
* One place in a document, or in the config while it is built: the operations listed in "The
|
|
4
|
+
* Document-Editing Package". Inspecting methods read the value (the config builder records each
|
|
5
|
+
* read); every change becomes primitive patches.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const targets = config.atPath("compilation.targets");
|
|
9
|
+
*
|
|
10
|
+
* if (targets.isArray()) {
|
|
11
|
+
* targets.removeWhere((target) => target.id === "diagrams");
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* targets.append({ id: "diagrams", entryGlob: "docs/**\/*.mmd" });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export interface DocumentNode {
|
|
18
|
+
/** The path of this node, as segments. */
|
|
19
|
+
readonly path: readonly PathSegment[];
|
|
20
|
+
/** The path of this node, as a canonical string (`compilation.targets[7]`). */
|
|
21
|
+
readonly pathString: string;
|
|
22
|
+
/** A node below this one. */
|
|
23
|
+
atPath(path: PathInput): DocumentNode;
|
|
24
|
+
/** The item at an index of this array node. */
|
|
25
|
+
atIndex(index: number): DocumentNode;
|
|
26
|
+
/** The node holding this one; the root's parent is the root. */
|
|
27
|
+
parent(): DocumentNode;
|
|
28
|
+
/** Whether a value exists here. */
|
|
29
|
+
exists(): boolean;
|
|
30
|
+
/** The kind of value here, or `"missing"`. */
|
|
31
|
+
type(): ValueType | "missing";
|
|
32
|
+
isArray(): boolean;
|
|
33
|
+
isObject(): boolean;
|
|
34
|
+
/** The value here, or `undefined` when missing. */
|
|
35
|
+
get<T = JsonValue>(): T | undefined;
|
|
36
|
+
/** The value here, or the fallback when missing. */
|
|
37
|
+
valueOr<T>(fallback: T): T;
|
|
38
|
+
/** The keys of this object node; `[]` for anything else. */
|
|
39
|
+
keys(): string[];
|
|
40
|
+
/** The number of items of this array node; `0` for anything else. */
|
|
41
|
+
length(): number;
|
|
42
|
+
/** Sets the value here. `undefined` deletes it. Never reads. */
|
|
43
|
+
set(value: unknown): void;
|
|
44
|
+
/** The same as {@link set}, under the name the examples use. */
|
|
45
|
+
setValue(value: unknown): void;
|
|
46
|
+
/** Deletes the value here. Never reads. */
|
|
47
|
+
delete(): void;
|
|
48
|
+
/** Renames this node's key, keeping its place. Never reads. */
|
|
49
|
+
rename(newKey: string): void;
|
|
50
|
+
/** Merges an object key by key, joining arrays. Reads every key it touches. */
|
|
51
|
+
merge(data: unknown): void;
|
|
52
|
+
/** Replaces the value here whole. */
|
|
53
|
+
replace(value: unknown): void;
|
|
54
|
+
/** Appends an item, creating the array when missing. Never reads. */
|
|
55
|
+
append(item: unknown): void;
|
|
56
|
+
/** The same as {@link append}, under the name the examples use. */
|
|
57
|
+
appendValue(item: unknown): void;
|
|
58
|
+
/** Inserts an item first. Never reads. */
|
|
59
|
+
prepend(item: unknown): void;
|
|
60
|
+
/** The same as {@link prepend}. */
|
|
61
|
+
prependValue(item: unknown): void;
|
|
62
|
+
/** Inserts an item at an index. Never reads. */
|
|
63
|
+
insertAt(index: number, item: unknown): void;
|
|
64
|
+
/** Removes the item at an index. Never reads. */
|
|
65
|
+
removeAt(index: number): void;
|
|
66
|
+
/** Removes every matching item, highest index first. */
|
|
67
|
+
removeWhere(predicate: (item: any, index: number) => boolean): void;
|
|
68
|
+
/** Replaces the first matching item, or appends the item when nothing matches. */
|
|
69
|
+
upsertWhere(predicate: (item: any, index: number) => boolean, item: unknown): void;
|
|
70
|
+
/** A node for the first matching item, to edit in place. */
|
|
71
|
+
findWhere(predicate: (item: any, index: number) => boolean): DocumentNode | undefined;
|
|
72
|
+
/** Moves an item from one index to another. */
|
|
73
|
+
move(from: number, to: number): void;
|
|
74
|
+
/** Removes repeated items, keeping the first of each. */
|
|
75
|
+
dedupe(by?: (item: any) => unknown): void;
|
|
76
|
+
/**
|
|
77
|
+
* Runs a helper of your own: the primitives it makes are grouped in a patch set with this name
|
|
78
|
+
* and this node's path, so `--layers` shows them under it.
|
|
79
|
+
*/
|
|
80
|
+
patchSet(name: string, helper: (node: DocumentNode) => void): void;
|
|
81
|
+
}
|
package/dist/node.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an intent returns: `IntentOutputs`, one or more `IntentOutput`s in the order they are shown.
|
|
3
|
+
* Each output carries a result as a payload type a reporter knows how to show (`VariableSet`,
|
|
4
|
+
* `Table`, ...), and converts to plain values for machines (`--json`, MCP, a calling intent).
|
|
5
|
+
*
|
|
6
|
+
* A payload is also what a reporter is given to show a structure while an intent runs:
|
|
7
|
+
* `reporter.variables(...)` reports a `VariableSet`.
|
|
8
|
+
*/
|
|
9
|
+
import type { ReportEntryStyle, TableColumn, TreeNode, VariableEntry } from "./report-styles.js";
|
|
10
|
+
/** Marks a payload, so a reporter recognizes one even from another copy of this package. */
|
|
11
|
+
declare const PAYLOAD: unique symbol;
|
|
12
|
+
/** Marks an `IntentOutputs`, for the same reason. */
|
|
13
|
+
declare const OUTPUTS: unique symbol;
|
|
14
|
+
/** A plain object: what `toPlain()` returns. */
|
|
15
|
+
export type PlainObject = Record<string, unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* A structure a reporter knows how to show. Each payload holds plain values only, and converts to
|
|
18
|
+
* them: `toPlain()` gives a plain object, and each type adds its own conversion (`toObject()`,
|
|
19
|
+
* `toRows()`, ...). `toText()` is the payload as plain text, for a reporter that draws nothing.
|
|
20
|
+
*/
|
|
21
|
+
export declare abstract class Payload {
|
|
22
|
+
/** Which payload type this is, so a reporter can show it without `instanceof`. */
|
|
23
|
+
abstract readonly kind: string;
|
|
24
|
+
readonly [PAYLOAD] = true;
|
|
25
|
+
/** The payload as a plain object. */
|
|
26
|
+
abstract toPlain(): PlainObject;
|
|
27
|
+
/** The payload as plain text, one line per entry. */
|
|
28
|
+
abstract toText(): string;
|
|
29
|
+
/** Whether a value is a payload, from any copy of sous-plugin-api. */
|
|
30
|
+
static is(value: unknown): value is Payload;
|
|
31
|
+
}
|
|
32
|
+
/** Labels and values, read as one block: `Repository: /work/recipes`. */
|
|
33
|
+
export declare class VariableSet extends Payload {
|
|
34
|
+
readonly kind = "variable-set";
|
|
35
|
+
readonly entries: readonly VariableEntry[];
|
|
36
|
+
readonly title: string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The width labels are padded to, so blocks shown one under another line their colons up.
|
|
39
|
+
* Default: the longest label of this set.
|
|
40
|
+
*/
|
|
41
|
+
readonly labelWidth: number | undefined;
|
|
42
|
+
/** @param entries - The pairs, or a record read as label and value pairs in its key order. */
|
|
43
|
+
constructor(entries: readonly VariableEntry[] | Readonly<Record<string, unknown>>, options?: {
|
|
44
|
+
title?: string;
|
|
45
|
+
labelWidth?: number;
|
|
46
|
+
});
|
|
47
|
+
/** The pairs as one object: `{ Repository: "/work/recipes" }`. A label used twice keeps its last value. */
|
|
48
|
+
toObject(): PlainObject;
|
|
49
|
+
toPlain(): PlainObject;
|
|
50
|
+
toText(): string;
|
|
51
|
+
}
|
|
52
|
+
/** Rows under columns. */
|
|
53
|
+
export declare class Table extends Payload {
|
|
54
|
+
readonly kind = "table";
|
|
55
|
+
readonly columns: readonly TableColumn[];
|
|
56
|
+
readonly rows: readonly Readonly<Record<string, unknown>>[];
|
|
57
|
+
readonly title: string | undefined;
|
|
58
|
+
/** What to say instead of the table when there are no rows. */
|
|
59
|
+
readonly empty: string | undefined;
|
|
60
|
+
/** A note shown under a row, by the row's index; `undefined` for a row with none. */
|
|
61
|
+
readonly rowNotes: readonly (string | undefined)[] | undefined;
|
|
62
|
+
constructor(columns: readonly TableColumn[], rows: readonly Readonly<Record<string, unknown>>[], options?: {
|
|
63
|
+
title?: string;
|
|
64
|
+
empty?: string;
|
|
65
|
+
rowNotes?: readonly (string | undefined)[];
|
|
66
|
+
});
|
|
67
|
+
/** The rows. */
|
|
68
|
+
toRows(): Readonly<Record<string, unknown>>[];
|
|
69
|
+
toPlain(): PlainObject;
|
|
70
|
+
toText(): string;
|
|
71
|
+
}
|
|
72
|
+
/** A list of items, with an optional title before it and an optional sentence after it. */
|
|
73
|
+
export declare class ItemList extends Payload {
|
|
74
|
+
readonly kind = "item-list";
|
|
75
|
+
readonly items: readonly string[];
|
|
76
|
+
readonly title: string | undefined;
|
|
77
|
+
readonly footer: string | undefined;
|
|
78
|
+
constructor(items: readonly string[], options?: {
|
|
79
|
+
title?: string;
|
|
80
|
+
footer?: string;
|
|
81
|
+
});
|
|
82
|
+
/** The items. */
|
|
83
|
+
toArray(): string[];
|
|
84
|
+
toPlain(): PlainObject;
|
|
85
|
+
toText(): string;
|
|
86
|
+
}
|
|
87
|
+
/** Items nested under other items. */
|
|
88
|
+
export declare class Tree extends Payload {
|
|
89
|
+
readonly kind = "tree";
|
|
90
|
+
readonly nodes: readonly TreeNode[];
|
|
91
|
+
readonly title: string | undefined;
|
|
92
|
+
constructor(nodes: readonly TreeNode[], options?: {
|
|
93
|
+
title?: string;
|
|
94
|
+
});
|
|
95
|
+
toPlain(): PlainObject;
|
|
96
|
+
toText(): string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Text. A paragraph a reporter may wrap, or, with `verbatim`, text shown exactly as given (a value
|
|
100
|
+
* a script reads, `--help`).
|
|
101
|
+
*/
|
|
102
|
+
export declare class TextBlock extends Payload {
|
|
103
|
+
readonly kind = "text-block";
|
|
104
|
+
readonly text: string;
|
|
105
|
+
readonly verbatim: boolean;
|
|
106
|
+
constructor(text: string, options?: {
|
|
107
|
+
verbatim?: boolean;
|
|
108
|
+
});
|
|
109
|
+
toString(): string;
|
|
110
|
+
toPlain(): PlainObject;
|
|
111
|
+
toText(): string;
|
|
112
|
+
}
|
|
113
|
+
/** A structured value shown as data (a config as JSON). */
|
|
114
|
+
export declare class DataValue extends Payload {
|
|
115
|
+
readonly kind = "data-value";
|
|
116
|
+
readonly value: unknown;
|
|
117
|
+
constructor(value: unknown);
|
|
118
|
+
/** The value itself. */
|
|
119
|
+
toValue(): unknown;
|
|
120
|
+
/** The value when it is a plain object, else `{ value }`. */
|
|
121
|
+
toPlain(): PlainObject;
|
|
122
|
+
toText(): string;
|
|
123
|
+
}
|
|
124
|
+
/** What one {@link IntentOutput} holds. */
|
|
125
|
+
export interface IntentOutputInit<T extends object = object> {
|
|
126
|
+
/** A title above it, for people. */
|
|
127
|
+
readonly title?: string;
|
|
128
|
+
/** One or two sentences saying what it is or what happened, for people. */
|
|
129
|
+
readonly summary?: string;
|
|
130
|
+
/** The result, as a payload a reporter knows how to show. */
|
|
131
|
+
readonly result?: Payload;
|
|
132
|
+
/** What machines get (`--json`, MCP, a calling intent). Default: `result.toPlain()`. */
|
|
133
|
+
readonly data?: T;
|
|
134
|
+
/** A hint for the reporter about what this output is (`ReportEntryStyle.CommandVariables`, ...). */
|
|
135
|
+
readonly style?: ReportEntryStyle;
|
|
136
|
+
}
|
|
137
|
+
/** One part of what an intent returns. */
|
|
138
|
+
export declare class IntentOutput<T extends object = object> {
|
|
139
|
+
readonly title: string | undefined;
|
|
140
|
+
readonly summary: string | undefined;
|
|
141
|
+
readonly result: Payload | undefined;
|
|
142
|
+
readonly data: T | undefined;
|
|
143
|
+
readonly style: ReportEntryStyle | undefined;
|
|
144
|
+
constructor(init: IntentOutputInit<T>);
|
|
145
|
+
/** What machines get: `data`, else the result as a plain object, else `{}`. */
|
|
146
|
+
toPlain(): T;
|
|
147
|
+
/** The output as plain text: title, summary, then the result. */
|
|
148
|
+
toText(): string;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* What an intent returns: its outputs, in the order they are shown.
|
|
152
|
+
*
|
|
153
|
+
* ```ts
|
|
154
|
+
* return new IntentOutputs(new IntentOutput({ summary: "Subscribed to acme/git.", result: new VariableSet({ Recipe: "acme/git" }) }));
|
|
155
|
+
*
|
|
156
|
+
* const prepared = (await ctx.run("build.prepare", {})).toPlain(); // the last output's plain object
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export declare class IntentOutputs<T extends object = PlainObject> implements Iterable<IntentOutput> {
|
|
160
|
+
#private;
|
|
161
|
+
readonly [OUTPUTS] = true;
|
|
162
|
+
/** The exit code the command ends with (a build with errors ends with 1). Default: none, so 0. */
|
|
163
|
+
readonly exitCode: number | undefined;
|
|
164
|
+
constructor(outputs?: IntentOutput | readonly IntentOutput[], options?: {
|
|
165
|
+
exitCode?: number;
|
|
166
|
+
});
|
|
167
|
+
/** Outputs holding one plain value and nothing to show beyond it. */
|
|
168
|
+
static of<T extends object>(data: T, init?: Omit<IntentOutputInit<T>, "data">): IntentOutputs<T>;
|
|
169
|
+
/** Whether a value is an `IntentOutputs`, from any copy of sous-plugin-api. */
|
|
170
|
+
static is(value: unknown): value is IntentOutputs;
|
|
171
|
+
/** How many outputs there are. */
|
|
172
|
+
get length(): number;
|
|
173
|
+
/** The outputs, in order. */
|
|
174
|
+
toArray(): IntentOutput[];
|
|
175
|
+
/** The first output, or `undefined`. */
|
|
176
|
+
first(): IntentOutput | undefined;
|
|
177
|
+
/** The last output, or `undefined`. */
|
|
178
|
+
last(): IntentOutput | undefined;
|
|
179
|
+
/** The last output's plain object (`{}` with none). Never an array. */
|
|
180
|
+
toPlain(): T;
|
|
181
|
+
/** Every output as plain text, separated by a blank line. */
|
|
182
|
+
toText(): string;
|
|
183
|
+
/** New outputs with more appended, keeping the exit code (for a wrapper or an `after` listener). */
|
|
184
|
+
with(...more: IntentOutput[]): IntentOutputs<T>;
|
|
185
|
+
/**
|
|
186
|
+
* The same outputs with `data` as what machines get: set on the last output, or as one output
|
|
187
|
+
* holding only data when there is none. Use it when the last output shows something built from
|
|
188
|
+
* a larger result.
|
|
189
|
+
*
|
|
190
|
+
* ```ts
|
|
191
|
+
* return new IntentOutputs([commandVariables(ctx), table]).withData({ subscriptions });
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
withData<D extends object>(data: D): IntentOutputs<D>;
|
|
195
|
+
/** The same outputs with another exit code. */
|
|
196
|
+
withExitCode(exitCode: number): IntentOutputs<T>;
|
|
197
|
+
[Symbol.iterator](): Iterator<IntentOutput>;
|
|
198
|
+
}
|
|
199
|
+
export {};
|
package/dist/outputs.js
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an intent returns: `IntentOutputs`, one or more `IntentOutput`s in the order they are shown.
|
|
3
|
+
* Each output carries a result as a payload type a reporter knows how to show (`VariableSet`,
|
|
4
|
+
* `Table`, ...), and converts to plain values for machines (`--json`, MCP, a calling intent).
|
|
5
|
+
*
|
|
6
|
+
* A payload is also what a reporter is given to show a structure while an intent runs:
|
|
7
|
+
* `reporter.variables(...)` reports a `VariableSet`.
|
|
8
|
+
*/
|
|
9
|
+
/** Marks a payload, so a reporter recognizes one even from another copy of this package. */
|
|
10
|
+
const PAYLOAD = Symbol.for("sous.payload");
|
|
11
|
+
/** Marks an `IntentOutputs`, for the same reason. */
|
|
12
|
+
const OUTPUTS = Symbol.for("sous.intent-outputs");
|
|
13
|
+
/**
|
|
14
|
+
* A structure a reporter knows how to show. Each payload holds plain values only, and converts to
|
|
15
|
+
* them: `toPlain()` gives a plain object, and each type adds its own conversion (`toObject()`,
|
|
16
|
+
* `toRows()`, ...). `toText()` is the payload as plain text, for a reporter that draws nothing.
|
|
17
|
+
*/
|
|
18
|
+
export class Payload {
|
|
19
|
+
[PAYLOAD] = true;
|
|
20
|
+
/** Whether a value is a payload, from any copy of sous-plugin-api. */
|
|
21
|
+
static is(value) {
|
|
22
|
+
return typeof value === "object" && value !== null && value[PAYLOAD] === true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** A value as text: nothing for `undefined` and `null`, JSON for an object. */
|
|
26
|
+
function textOf(value) {
|
|
27
|
+
if (value === undefined || value === null) {
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
if (typeof value !== "object") {
|
|
31
|
+
return String(value);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
return JSON.stringify(value);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return String(value);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Labels and values, read as one block: `Repository: /work/recipes`. */
|
|
41
|
+
export class VariableSet extends Payload {
|
|
42
|
+
kind = "variable-set";
|
|
43
|
+
entries;
|
|
44
|
+
title;
|
|
45
|
+
/**
|
|
46
|
+
* The width labels are padded to, so blocks shown one under another line their colons up.
|
|
47
|
+
* Default: the longest label of this set.
|
|
48
|
+
*/
|
|
49
|
+
labelWidth;
|
|
50
|
+
/** @param entries - The pairs, or a record read as label and value pairs in its key order. */
|
|
51
|
+
constructor(entries, options = {}) {
|
|
52
|
+
super();
|
|
53
|
+
this.entries = Array.isArray(entries) ? [...entries] : Object.entries(entries).map(([label, value]) => ({ label, value }));
|
|
54
|
+
this.title = options.title;
|
|
55
|
+
this.labelWidth = options.labelWidth;
|
|
56
|
+
}
|
|
57
|
+
/** The pairs as one object: `{ Repository: "/work/recipes" }`. A label used twice keeps its last value. */
|
|
58
|
+
toObject() {
|
|
59
|
+
return Object.fromEntries(this.entries.filter((entry) => entry.label !== "").map((entry) => [entry.label, entry.value]));
|
|
60
|
+
}
|
|
61
|
+
toPlain() {
|
|
62
|
+
return this.toObject();
|
|
63
|
+
}
|
|
64
|
+
toText() {
|
|
65
|
+
const lines = this.entries.map((entry) => `${entry.label}: ${textOf(entry.value)}${entry.detail === undefined ? "" : ` (${entry.detail})`}`);
|
|
66
|
+
return [...(this.title === undefined ? [] : [this.title]), ...lines].join("\n");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Rows under columns. */
|
|
70
|
+
export class Table extends Payload {
|
|
71
|
+
kind = "table";
|
|
72
|
+
columns;
|
|
73
|
+
rows;
|
|
74
|
+
title;
|
|
75
|
+
/** What to say instead of the table when there are no rows. */
|
|
76
|
+
empty;
|
|
77
|
+
/** A note shown under a row, by the row's index; `undefined` for a row with none. */
|
|
78
|
+
rowNotes;
|
|
79
|
+
constructor(columns, rows, options = {}) {
|
|
80
|
+
super();
|
|
81
|
+
this.columns = columns;
|
|
82
|
+
this.rows = rows;
|
|
83
|
+
this.title = options.title;
|
|
84
|
+
this.empty = options.empty;
|
|
85
|
+
this.rowNotes = options.rowNotes;
|
|
86
|
+
}
|
|
87
|
+
/** The rows. */
|
|
88
|
+
toRows() {
|
|
89
|
+
return [...this.rows];
|
|
90
|
+
}
|
|
91
|
+
toPlain() {
|
|
92
|
+
return { rows: this.toRows() };
|
|
93
|
+
}
|
|
94
|
+
toText() {
|
|
95
|
+
if (this.rows.length === 0 && this.empty !== undefined) {
|
|
96
|
+
return this.empty;
|
|
97
|
+
}
|
|
98
|
+
const lines = this.rows.map((row) => this.columns.map((column) => textOf(row[column.key])).join(" "));
|
|
99
|
+
return [...(this.title === undefined ? [] : [this.title]), ...lines].join("\n");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** A list of items, with an optional title before it and an optional sentence after it. */
|
|
103
|
+
export class ItemList extends Payload {
|
|
104
|
+
kind = "item-list";
|
|
105
|
+
items;
|
|
106
|
+
title;
|
|
107
|
+
footer;
|
|
108
|
+
constructor(items, options = {}) {
|
|
109
|
+
super();
|
|
110
|
+
this.items = items;
|
|
111
|
+
this.title = options.title;
|
|
112
|
+
this.footer = options.footer;
|
|
113
|
+
}
|
|
114
|
+
/** The items. */
|
|
115
|
+
toArray() {
|
|
116
|
+
return [...this.items];
|
|
117
|
+
}
|
|
118
|
+
toPlain() {
|
|
119
|
+
return { items: this.toArray() };
|
|
120
|
+
}
|
|
121
|
+
toText() {
|
|
122
|
+
return [...(this.title === undefined ? [] : [this.title]), ...this.items.map((item) => `- ${item}`), ...(this.footer === undefined ? [] : [this.footer])].join("\n");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Items nested under other items. */
|
|
126
|
+
export class Tree extends Payload {
|
|
127
|
+
kind = "tree";
|
|
128
|
+
nodes;
|
|
129
|
+
title;
|
|
130
|
+
constructor(nodes, options = {}) {
|
|
131
|
+
super();
|
|
132
|
+
this.nodes = nodes;
|
|
133
|
+
this.title = options.title;
|
|
134
|
+
}
|
|
135
|
+
toPlain() {
|
|
136
|
+
return { nodes: this.nodes };
|
|
137
|
+
}
|
|
138
|
+
toText() {
|
|
139
|
+
const lines = (nodes, depth) => nodes.flatMap((node) => [`${" ".repeat(depth)}${node.label}${node.detail === undefined ? "" : ` (${node.detail})`}`, ...lines(node.children ?? [], depth + 1)]);
|
|
140
|
+
return [...(this.title === undefined ? [] : [this.title]), ...lines(this.nodes, 0)].join("\n");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Text. A paragraph a reporter may wrap, or, with `verbatim`, text shown exactly as given (a value
|
|
145
|
+
* a script reads, `--help`).
|
|
146
|
+
*/
|
|
147
|
+
export class TextBlock extends Payload {
|
|
148
|
+
kind = "text-block";
|
|
149
|
+
text;
|
|
150
|
+
verbatim;
|
|
151
|
+
constructor(text, options = {}) {
|
|
152
|
+
super();
|
|
153
|
+
this.text = text;
|
|
154
|
+
this.verbatim = options.verbatim === true;
|
|
155
|
+
}
|
|
156
|
+
toString() {
|
|
157
|
+
return this.text;
|
|
158
|
+
}
|
|
159
|
+
toPlain() {
|
|
160
|
+
return { text: this.text };
|
|
161
|
+
}
|
|
162
|
+
toText() {
|
|
163
|
+
return this.text;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** A structured value shown as data (a config as JSON). */
|
|
167
|
+
export class DataValue extends Payload {
|
|
168
|
+
kind = "data-value";
|
|
169
|
+
value;
|
|
170
|
+
constructor(value) {
|
|
171
|
+
super();
|
|
172
|
+
this.value = value;
|
|
173
|
+
}
|
|
174
|
+
/** The value itself. */
|
|
175
|
+
toValue() {
|
|
176
|
+
return this.value;
|
|
177
|
+
}
|
|
178
|
+
/** The value when it is a plain object, else `{ value }`. */
|
|
179
|
+
toPlain() {
|
|
180
|
+
return typeof this.value === "object" && this.value !== null && !Array.isArray(this.value) ? this.value : { value: this.value };
|
|
181
|
+
}
|
|
182
|
+
toText() {
|
|
183
|
+
try {
|
|
184
|
+
return JSON.stringify(this.value, null, 2) ?? String(this.value);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return String(this.value);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** One part of what an intent returns. */
|
|
192
|
+
export class IntentOutput {
|
|
193
|
+
title;
|
|
194
|
+
summary;
|
|
195
|
+
result;
|
|
196
|
+
data;
|
|
197
|
+
style;
|
|
198
|
+
constructor(init) {
|
|
199
|
+
this.title = init.title;
|
|
200
|
+
this.summary = init.summary;
|
|
201
|
+
this.result = init.result;
|
|
202
|
+
this.data = init.data;
|
|
203
|
+
this.style = init.style;
|
|
204
|
+
}
|
|
205
|
+
/** What machines get: `data`, else the result as a plain object, else `{}`. */
|
|
206
|
+
toPlain() {
|
|
207
|
+
return (this.data ?? this.result?.toPlain() ?? {});
|
|
208
|
+
}
|
|
209
|
+
/** The output as plain text: title, summary, then the result. */
|
|
210
|
+
toText() {
|
|
211
|
+
return [this.title, this.summary, this.result?.toText()].filter((part) => part !== undefined && part !== "").join("\n");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* What an intent returns: its outputs, in the order they are shown.
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* return new IntentOutputs(new IntentOutput({ summary: "Subscribed to acme/git.", result: new VariableSet({ Recipe: "acme/git" }) }));
|
|
219
|
+
*
|
|
220
|
+
* const prepared = (await ctx.run("build.prepare", {})).toPlain(); // the last output's plain object
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export class IntentOutputs {
|
|
224
|
+
[OUTPUTS] = true;
|
|
225
|
+
#outputs;
|
|
226
|
+
/** The exit code the command ends with (a build with errors ends with 1). Default: none, so 0. */
|
|
227
|
+
exitCode;
|
|
228
|
+
constructor(outputs = [], options = {}) {
|
|
229
|
+
this.#outputs = Array.isArray(outputs) ? [...outputs] : [outputs];
|
|
230
|
+
this.exitCode = options.exitCode;
|
|
231
|
+
}
|
|
232
|
+
/** Outputs holding one plain value and nothing to show beyond it. */
|
|
233
|
+
static of(data, init = {}) {
|
|
234
|
+
return new IntentOutputs(new IntentOutput({ ...init, data }));
|
|
235
|
+
}
|
|
236
|
+
/** Whether a value is an `IntentOutputs`, from any copy of sous-plugin-api. */
|
|
237
|
+
static is(value) {
|
|
238
|
+
return typeof value === "object" && value !== null && value[OUTPUTS] === true;
|
|
239
|
+
}
|
|
240
|
+
/** How many outputs there are. */
|
|
241
|
+
get length() {
|
|
242
|
+
return this.#outputs.length;
|
|
243
|
+
}
|
|
244
|
+
/** The outputs, in order. */
|
|
245
|
+
toArray() {
|
|
246
|
+
return [...this.#outputs];
|
|
247
|
+
}
|
|
248
|
+
/** The first output, or `undefined`. */
|
|
249
|
+
first() {
|
|
250
|
+
return this.#outputs[0];
|
|
251
|
+
}
|
|
252
|
+
/** The last output, or `undefined`. */
|
|
253
|
+
last() {
|
|
254
|
+
return this.#outputs[this.#outputs.length - 1];
|
|
255
|
+
}
|
|
256
|
+
/** The last output's plain object (`{}` with none). Never an array. */
|
|
257
|
+
toPlain() {
|
|
258
|
+
return (this.last()?.toPlain() ?? {});
|
|
259
|
+
}
|
|
260
|
+
/** Every output as plain text, separated by a blank line. */
|
|
261
|
+
toText() {
|
|
262
|
+
return this.#outputs.map((output) => output.toText()).filter((text) => text !== "").join("\n\n");
|
|
263
|
+
}
|
|
264
|
+
/** New outputs with more appended, keeping the exit code (for a wrapper or an `after` listener). */
|
|
265
|
+
with(...more) {
|
|
266
|
+
return new IntentOutputs([...this.#outputs, ...more], this.exitCode === undefined ? {} : { exitCode: this.exitCode });
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* The same outputs with `data` as what machines get: set on the last output, or as one output
|
|
270
|
+
* holding only data when there is none. Use it when the last output shows something built from
|
|
271
|
+
* a larger result.
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* return new IntentOutputs([commandVariables(ctx), table]).withData({ subscriptions });
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
withData(data) {
|
|
278
|
+
const outputs = [...this.#outputs];
|
|
279
|
+
const last = outputs.pop();
|
|
280
|
+
const replaced = last === undefined
|
|
281
|
+
? new IntentOutput({ data })
|
|
282
|
+
: new IntentOutput({ ...(last.title === undefined ? {} : { title: last.title }), ...(last.summary === undefined ? {} : { summary: last.summary }), ...(last.result === undefined ? {} : { result: last.result }), ...(last.style === undefined ? {} : { style: last.style }), data });
|
|
283
|
+
return new IntentOutputs([...outputs, replaced], this.exitCode === undefined ? {} : { exitCode: this.exitCode });
|
|
284
|
+
}
|
|
285
|
+
/** The same outputs with another exit code. */
|
|
286
|
+
withExitCode(exitCode) {
|
|
287
|
+
return new IntentOutputs([...this.#outputs], { exitCode });
|
|
288
|
+
}
|
|
289
|
+
[Symbol.iterator]() {
|
|
290
|
+
return this.#outputs[Symbol.iterator]();
|
|
291
|
+
}
|
|
292
|
+
}
|