@pbuilder/sdk 0.4.0 → 0.5.0
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 +42 -2
- package/dist/commons/index.js +5 -2
- package/dist/conformance/index.d.ts +2 -2
- package/dist/conformance/index.js +38 -19
- package/dist/core/define-dialect.js +15 -8
- package/dist/core/dialect-handle.js +1 -1
- package/dist/core/json-value.d.ts +14 -0
- package/dist/core/json-value.js +43 -0
- package/dist/dialects/json/ast.d.ts +24 -0
- package/dist/dialects/json/ast.js +21 -0
- package/dist/dialects/json/index.d.ts +55 -0
- package/dist/dialects/json/index.js +54 -0
- package/dist/dialects/json/ops.d.ts +46 -0
- package/dist/dialects/json/ops.js +561 -0
- package/dist/dialects/yaml/ast.d.ts +15 -0
- package/dist/dialects/yaml/ast.js +154 -0
- package/dist/dialects/yaml/documents.d.ts +8 -0
- package/dist/dialects/yaml/documents.js +24 -0
- package/dist/dialects/yaml/index.d.ts +69 -0
- package/dist/dialects/yaml/index.js +70 -0
- package/dist/dialects/yaml/paths.d.ts +55 -0
- package/dist/dialects/yaml/paths.js +208 -0
- package/dist/dialects/yaml/presentation.d.ts +24 -0
- package/dist/dialects/yaml/presentation.js +264 -0
- package/dist/dialects/yaml/structural.d.ts +39 -0
- package/dist/dialects/yaml/structural.js +307 -0
- package/dist/runner-manifest.json +3 -3
- package/dist/transport/error-text.d.ts +6 -1
- package/dist/transport/error-text.js +94 -31
- package/package.json +16 -3
- package/schemas/project-builder.schema.json +112 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// Presentation ops only YAML has: comments, anchors, aliases, merge keys, styles, and tags.
|
|
2
|
+
import { Alias, Document, Pair, Scalar, isAlias, isMap, isScalar, isSeq, parseDocument, visit } from "yaml";
|
|
3
|
+
import { deepEqual } from "../../core/deep-equal.js";
|
|
4
|
+
import { assertAliasesResolve, assertPath, fail, findAnchored, findPair, resolve, selectDocument, toValue, writeSlot, } from "./paths.js";
|
|
5
|
+
function existingSlot(op, document, path) {
|
|
6
|
+
const slot = resolve(op, document, path, "write");
|
|
7
|
+
if (slot.node === null)
|
|
8
|
+
fail(op, path, "no node exists at this path.");
|
|
9
|
+
return slot;
|
|
10
|
+
}
|
|
11
|
+
function position(op, path, options) {
|
|
12
|
+
const value = options?.position ?? "before";
|
|
13
|
+
if (value !== "before" && value !== "inline")
|
|
14
|
+
fail(op, path, `position must be "before" or "inline", got ${String(value)}.`);
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
const isBlockCollection = (node) => (isMap(node) || isSeq(node)) && node.flow !== true;
|
|
18
|
+
/** The object and field holding the comment at `where` for the node at `path`. */
|
|
19
|
+
function commentHolder(op, document, path, where) {
|
|
20
|
+
if (path.length === 0) {
|
|
21
|
+
if (where === "inline")
|
|
22
|
+
fail(op, path, "the document has no inline comment position; use position \"before\".");
|
|
23
|
+
return { holder: document, field: "commentBefore" };
|
|
24
|
+
}
|
|
25
|
+
const slot = existingSlot(op, document, path);
|
|
26
|
+
if (where === "before") {
|
|
27
|
+
if (isMap(slot.parent))
|
|
28
|
+
return { holder: findPair(slot.parent, slot.segment).key, field: "commentBefore" };
|
|
29
|
+
return { holder: slot.node, field: "commentBefore" };
|
|
30
|
+
}
|
|
31
|
+
if (isBlockCollection(slot.node)) {
|
|
32
|
+
if (isSeq(slot.parent))
|
|
33
|
+
fail(op, path, "a block collection item has no inline position; address a scalar inside it.");
|
|
34
|
+
if (isMap(slot.parent))
|
|
35
|
+
return { holder: findPair(slot.parent, slot.segment).key, field: "comment" };
|
|
36
|
+
}
|
|
37
|
+
return { holder: slot.node, field: "comment" };
|
|
38
|
+
}
|
|
39
|
+
const toCommentText = (comment) => comment
|
|
40
|
+
.split(/\r?\n/)
|
|
41
|
+
.map((line) => (line === "" ? "" : ` ${line}`))
|
|
42
|
+
.join("\n");
|
|
43
|
+
/** Sets the comment before a node, or after it on the same line; multi-line text gives one `#` line each. */
|
|
44
|
+
export function setComment(ast, path, comment, options) {
|
|
45
|
+
assertPath("setComment", path);
|
|
46
|
+
if (typeof comment !== "string")
|
|
47
|
+
fail("setComment", path, "comment must be a string.");
|
|
48
|
+
const where = position("setComment", path, options);
|
|
49
|
+
const document = selectDocument("setComment", ast, options);
|
|
50
|
+
const { holder, field } = commentHolder("setComment", document, path, where);
|
|
51
|
+
const text = toCommentText(comment);
|
|
52
|
+
if (holder[field] !== text)
|
|
53
|
+
holder[field] = text;
|
|
54
|
+
}
|
|
55
|
+
/** Removes the comment at the given position; no comment is a no-op. */
|
|
56
|
+
export function removeComment(ast, path, options) {
|
|
57
|
+
assertPath("removeComment", path);
|
|
58
|
+
const where = position("removeComment", path, options);
|
|
59
|
+
const document = selectDocument("removeComment", ast, options);
|
|
60
|
+
const { holder, field } = commentHolder("removeComment", document, path, where);
|
|
61
|
+
if (holder[field])
|
|
62
|
+
holder[field] = undefined;
|
|
63
|
+
}
|
|
64
|
+
// ns-anchor-char: any printable non-space character except the flow indicators.
|
|
65
|
+
const ANCHOR_NAME = /^[^\s,[\]{}\p{Cc}]+$/u;
|
|
66
|
+
function anchoredTarget(op, document, path) {
|
|
67
|
+
const { node } = existingSlot(op, document, path);
|
|
68
|
+
if (isAlias(node))
|
|
69
|
+
fail(op, path, `the value is the alias *${node.source}; anchors go on the anchored node.`);
|
|
70
|
+
return node;
|
|
71
|
+
}
|
|
72
|
+
function aliasesOf(document, node) {
|
|
73
|
+
const aliases = [];
|
|
74
|
+
visit(document, {
|
|
75
|
+
Alias(_, alias) {
|
|
76
|
+
if (alias.resolve(document) === node)
|
|
77
|
+
aliases.push(alias);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return aliases;
|
|
81
|
+
}
|
|
82
|
+
/** Puts `&name` on the node at `path`; renaming an anchor re-points its aliases. */
|
|
83
|
+
export function setAnchor(ast, path, name, options) {
|
|
84
|
+
assertPath("setAnchor", path);
|
|
85
|
+
if (typeof name !== "string" || !ANCHOR_NAME.test(name))
|
|
86
|
+
fail("setAnchor", path, `"${String(name)}" is not a valid YAML anchor name.`);
|
|
87
|
+
const document = selectDocument("setAnchor", ast, options);
|
|
88
|
+
const node = anchoredTarget("setAnchor", document, path);
|
|
89
|
+
if (node.anchor === name)
|
|
90
|
+
return;
|
|
91
|
+
if (findAnchored(document, name) !== undefined)
|
|
92
|
+
fail("setAnchor", path, `another node already carries &${name}.`);
|
|
93
|
+
for (const alias of aliasesOf(document, node))
|
|
94
|
+
alias.source = name;
|
|
95
|
+
node.anchor = name;
|
|
96
|
+
}
|
|
97
|
+
/** Removes the anchor, replacing every alias to it with a copy so the data is unchanged. */
|
|
98
|
+
export function removeAnchor(ast, path, options) {
|
|
99
|
+
assertPath("removeAnchor", path);
|
|
100
|
+
const document = selectDocument("removeAnchor", ast, options);
|
|
101
|
+
const node = anchoredTarget("removeAnchor", document, path);
|
|
102
|
+
if (!node.anchor)
|
|
103
|
+
return;
|
|
104
|
+
const aliases = new Set(aliasesOf(document, node));
|
|
105
|
+
visit(document, {
|
|
106
|
+
Alias(_, alias) {
|
|
107
|
+
if (!aliases.has(alias))
|
|
108
|
+
return undefined;
|
|
109
|
+
const copy = node.clone();
|
|
110
|
+
visit(copy, (__, inner) => {
|
|
111
|
+
if (inner !== null && typeof inner === "object" && "anchor" in inner)
|
|
112
|
+
inner.anchor = undefined;
|
|
113
|
+
});
|
|
114
|
+
copy.anchor = undefined;
|
|
115
|
+
copy.comment = alias.comment;
|
|
116
|
+
copy.commentBefore = alias.commentBefore;
|
|
117
|
+
copy.spaceBefore = alias.spaceBefore;
|
|
118
|
+
return copy;
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
node.anchor = undefined;
|
|
122
|
+
}
|
|
123
|
+
/** Sets the value at `path` to `*anchor`, creating missing parents like `setValue`. */
|
|
124
|
+
export function setAlias(ast, path, anchor, options) {
|
|
125
|
+
assertPath("setAlias", path);
|
|
126
|
+
if (typeof anchor !== "string")
|
|
127
|
+
fail("setAlias", path, "anchor must be a string.");
|
|
128
|
+
const document = selectDocument("setAlias", ast, options);
|
|
129
|
+
if (findAnchored(document, anchor) === undefined)
|
|
130
|
+
fail("setAlias", path, `no node carries the anchor &${anchor}.`);
|
|
131
|
+
const slot = resolve("setAlias", document, path, "create");
|
|
132
|
+
if (isAlias(slot.node) && slot.node.source === anchor)
|
|
133
|
+
return;
|
|
134
|
+
if (isSeq(slot.parent) && slot.segment >= slot.parent.items.length) {
|
|
135
|
+
fail("setAlias", path, `index ${String(slot.segment)} is out of range.`);
|
|
136
|
+
}
|
|
137
|
+
const alias = new Alias(anchor);
|
|
138
|
+
if (slot.node !== null) {
|
|
139
|
+
alias.commentBefore = slot.node.commentBefore;
|
|
140
|
+
alias.comment = slot.node.comment;
|
|
141
|
+
alias.spaceBefore = slot.node.spaceBefore;
|
|
142
|
+
}
|
|
143
|
+
writeSlot(document, slot, alias);
|
|
144
|
+
if (findAnchored(document, anchor) === undefined)
|
|
145
|
+
fail("setAlias", path, `the alias *${anchor} would replace its own anchor.`);
|
|
146
|
+
assertAliasesResolve("setAlias", document);
|
|
147
|
+
}
|
|
148
|
+
const isMergeKey = (pair) => isScalar(pair.key) && typeof pair.key.value === "symbol";
|
|
149
|
+
/** Adds `<<: *anchor` as the first entry of the mapping at `path`. */
|
|
150
|
+
export function addMergeKey(ast, path, anchor, options) {
|
|
151
|
+
assertPath("addMergeKey", path);
|
|
152
|
+
if (typeof anchor !== "string")
|
|
153
|
+
fail("addMergeKey", path, "anchor must be a string.");
|
|
154
|
+
const document = selectDocument("addMergeKey", ast, options);
|
|
155
|
+
const anchored = findAnchored(document, anchor);
|
|
156
|
+
if (anchored === undefined)
|
|
157
|
+
fail("addMergeKey", path, `no node carries the anchor &${anchor}.`);
|
|
158
|
+
if (!isMap(anchored))
|
|
159
|
+
fail("addMergeKey", path, `the node anchored &${anchor} is not a mapping.`);
|
|
160
|
+
const { node } = existingSlot("addMergeKey", document, path);
|
|
161
|
+
if (isAlias(node))
|
|
162
|
+
fail("addMergeKey", path, `the value is the alias *${node.source}; edit the anchored node instead.`);
|
|
163
|
+
if (!isMap(node))
|
|
164
|
+
fail("addMergeKey", path, "the value is not a mapping.");
|
|
165
|
+
const map = node;
|
|
166
|
+
const existing = map.items.find(isMergeKey);
|
|
167
|
+
const sources = existing === undefined ? [] : isSeq(existing.value) ? existing.value.items : [existing.value];
|
|
168
|
+
if (sources.some((source) => isAlias(source) && source.source === anchor))
|
|
169
|
+
return;
|
|
170
|
+
if (existing === undefined) {
|
|
171
|
+
map.items.unshift(new Pair(MERGE_KEY.clone(), new Alias(anchor)));
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
const seq = document.createNode([], { flow: true });
|
|
175
|
+
seq.items.push(...sources, new Alias(anchor));
|
|
176
|
+
existing.value = seq;
|
|
177
|
+
}
|
|
178
|
+
assertAliasesResolve("addMergeKey", document);
|
|
179
|
+
}
|
|
180
|
+
// The merge key is a schema-specific scalar, so it is taken from a parse rather than built.
|
|
181
|
+
const MERGE_KEY = parseDocument("<<: null\n", { merge: true }).contents.items[0].key;
|
|
182
|
+
const SCALAR_TYPES = {
|
|
183
|
+
plain: Scalar.PLAIN,
|
|
184
|
+
single: Scalar.QUOTE_SINGLE,
|
|
185
|
+
double: Scalar.QUOTE_DOUBLE,
|
|
186
|
+
literal: Scalar.BLOCK_LITERAL,
|
|
187
|
+
folded: Scalar.BLOCK_FOLDED,
|
|
188
|
+
};
|
|
189
|
+
function printsAs(scalar, type) {
|
|
190
|
+
const copy = scalar.clone();
|
|
191
|
+
copy.type = type;
|
|
192
|
+
copy.comment = undefined;
|
|
193
|
+
copy.commentBefore = undefined;
|
|
194
|
+
copy.anchor = undefined;
|
|
195
|
+
const text = new Document(copy).toString({ lineWidth: 0 }).trimStart();
|
|
196
|
+
const lead = text.replace(/^!\S*\s*/, "")[0];
|
|
197
|
+
const expected = { PLAIN: undefined, QUOTE_SINGLE: "'", QUOTE_DOUBLE: '"', BLOCK_LITERAL: "|", BLOCK_FOLDED: ">" }[type];
|
|
198
|
+
if (expected === undefined)
|
|
199
|
+
return lead !== "'" && lead !== '"' && lead !== "|" && lead !== ">";
|
|
200
|
+
return lead === expected;
|
|
201
|
+
}
|
|
202
|
+
/** Sets a collection to block or flow, or a scalar to a quoting or block style. */
|
|
203
|
+
export function setStyle(ast, path, style, options) {
|
|
204
|
+
assertPath("setStyle", path);
|
|
205
|
+
const document = selectDocument("setStyle", ast, options);
|
|
206
|
+
const { node } = existingSlot("setStyle", document, path);
|
|
207
|
+
if (isAlias(node))
|
|
208
|
+
fail("setStyle", path, `the value is the alias *${node.source}; aliases have no style.`);
|
|
209
|
+
if (style === "block" || style === "flow") {
|
|
210
|
+
if (!isMap(node) && !isSeq(node))
|
|
211
|
+
fail("setStyle", path, `"${style}" is a collection style and the value is a scalar.`);
|
|
212
|
+
const flow = style === "flow";
|
|
213
|
+
if ((node.flow === true) !== flow)
|
|
214
|
+
node.flow = flow;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const type = SCALAR_TYPES[style];
|
|
218
|
+
if (type === undefined)
|
|
219
|
+
fail("setStyle", path, `unknown style "${String(style)}".`);
|
|
220
|
+
if (!isScalar(node))
|
|
221
|
+
fail("setStyle", path, `"${style}" is a scalar style and the value is a collection.`);
|
|
222
|
+
const scalar = node;
|
|
223
|
+
if (scalar.type === type)
|
|
224
|
+
return;
|
|
225
|
+
if (!printsAs(scalar, type))
|
|
226
|
+
fail("setStyle", path, `the value cannot be written ${style} without changing it.`);
|
|
227
|
+
const before = toValue(document, scalar);
|
|
228
|
+
const previous = scalar.type;
|
|
229
|
+
scalar.type = type;
|
|
230
|
+
if (!deepEqual(new Document(scalar.clone()).toJS(), before)) {
|
|
231
|
+
scalar.type = previous;
|
|
232
|
+
fail("setStyle", path, `the value cannot be written ${style} without changing it.`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const TAG_SUFFIX = /^[A-Za-z0-9\-#;/?:@&=+$_.~*'()%]+$/;
|
|
236
|
+
function normalizeTag(op, path, document, tag) {
|
|
237
|
+
if (/^!<[^<>\s]+>$/.test(tag))
|
|
238
|
+
return tag.slice(2, -1);
|
|
239
|
+
if (tag.startsWith("!!") && TAG_SUFFIX.test(tag.slice(2)))
|
|
240
|
+
return `tag:yaml.org,2002:${tag.slice(2)}`;
|
|
241
|
+
const named = /^(![A-Za-z0-9-]*!)(.+)$/.exec(tag);
|
|
242
|
+
if (named !== null && named[1] !== "!!") {
|
|
243
|
+
const prefix = document.directives?.tags[named[1]];
|
|
244
|
+
if (prefix === undefined || !TAG_SUFFIX.test(named[2]))
|
|
245
|
+
fail(op, path, `"${tag}" uses an undeclared tag handle.`);
|
|
246
|
+
return prefix + named[2];
|
|
247
|
+
}
|
|
248
|
+
if (tag.startsWith("!") && TAG_SUFFIX.test(tag.slice(1)))
|
|
249
|
+
return tag;
|
|
250
|
+
return fail(op, path, `"${tag}" is not a valid YAML tag.`);
|
|
251
|
+
}
|
|
252
|
+
/** Sets the node's tag, keeping its value and style; `null` removes it. */
|
|
253
|
+
export function setTag(ast, path, tag, options) {
|
|
254
|
+
assertPath("setTag", path);
|
|
255
|
+
if (tag !== null && typeof tag !== "string")
|
|
256
|
+
fail("setTag", path, "tag must be a string or null.");
|
|
257
|
+
const document = selectDocument("setTag", ast, options);
|
|
258
|
+
const { node } = existingSlot("setTag", document, path);
|
|
259
|
+
if (isAlias(node))
|
|
260
|
+
fail("setTag", path, `the value is the alias *${node.source}; aliases have no tag.`);
|
|
261
|
+
const next = tag === null ? undefined : normalizeTag("setTag", path, document, tag);
|
|
262
|
+
if (node.tag !== next)
|
|
263
|
+
node.tag = next;
|
|
264
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { YamlStream } from "./ast.ts";
|
|
2
|
+
import { type DocumentOption, type PositionOption, type YamlPath, type YamlValue } from "./paths.ts";
|
|
3
|
+
export type KeyOptions = DocumentOption & PositionOption;
|
|
4
|
+
/** Sets the value at `path`, creating missing parent mappings; an equal value changes nothing. */
|
|
5
|
+
export declare function setValue(ast: YamlStream, path: YamlPath, value: YamlValue, options?: KeyOptions): void;
|
|
6
|
+
/** Adds a mapping key that does not exist yet; an equal existing value is a no-op. */
|
|
7
|
+
export declare function addKey(ast: YamlStream, path: YamlPath, value: YamlValue, options?: KeyOptions): void;
|
|
8
|
+
/** Removes a mapping key and its comments; an absent key is a no-op. */
|
|
9
|
+
export declare function removeKey(ast: YamlStream, path: YamlPath, options?: DocumentOption): void;
|
|
10
|
+
/** Renames a mapping key in place, keeping its value, position, and comments. */
|
|
11
|
+
export declare function renameKey(ast: YamlStream, path: YamlPath, newKey: string, options?: DocumentOption): void;
|
|
12
|
+
/** Inserts a sequence item before `index`; `index` equal to the length appends. */
|
|
13
|
+
export declare function insertItem(ast: YamlStream, path: YamlPath, index: number, value: YamlValue, options?: DocumentOption): void;
|
|
14
|
+
/** Appends a sequence item, creating the sequence; `{ unique: true }` skips a deep-equal duplicate. */
|
|
15
|
+
export declare function appendItem(ast: YamlStream, path: YamlPath, value: YamlValue, options?: DocumentOption & {
|
|
16
|
+
unique?: boolean;
|
|
17
|
+
}): void;
|
|
18
|
+
/** Removes the sequence item at `index`. */
|
|
19
|
+
export declare function removeItem(ast: YamlStream, path: YamlPath, index: number, options?: DocumentOption): void;
|
|
20
|
+
/** Removes every sequence item deep-equal to `value`; no match is a no-op. */
|
|
21
|
+
export declare function removeValue(ast: YamlStream, path: YamlPath, value: YamlValue, options?: DocumentOption): void;
|
|
22
|
+
/** Replaces the sequence item at `index`, keeping its comments. */
|
|
23
|
+
export declare function replaceItem(ast: YamlStream, path: YamlPath, index: number, value: YamlValue, options?: DocumentOption): void;
|
|
24
|
+
/** Moves the item at `from` so it sits at `to`; the others keep their relative order. */
|
|
25
|
+
export declare function moveItem(ast: YamlStream, path: YamlPath, from: number, to: number, options?: DocumentOption): void;
|
|
26
|
+
/** Deep-merges a plain object into the mapping at `path`, creating it when absent. */
|
|
27
|
+
export declare function merge(ast: YamlStream, path: YamlPath, value: {
|
|
28
|
+
[key: string]: YamlValue;
|
|
29
|
+
}, options?: DocumentOption): void;
|
|
30
|
+
/** Replaces the value at `path` with `fn(current)`; `current` is undefined when absent. */
|
|
31
|
+
export declare function updateValue<T extends YamlValue>(ast: YamlStream, path: YamlPath, fn: (current: YamlValue | undefined) => T, options?: DocumentOption): void;
|
|
32
|
+
/** Moves the value at `from` to `to` (RFC 6902 move): a number last segment inserts. */
|
|
33
|
+
export declare function moveValue(ast: YamlStream, from: YamlPath, to: YamlPath, options?: DocumentOption): void;
|
|
34
|
+
/** Copies the resolved data at `from` to `to` with `setValue`'s rules; anchors are not copied. */
|
|
35
|
+
export declare function copyValue(ast: YamlStream, from: YamlPath, to: YamlPath, options?: DocumentOption): void;
|
|
36
|
+
/** Orders a mapping's keys by code point; `{ recursive: true }` also sorts nested mappings. */
|
|
37
|
+
export declare function sortKeys(ast: YamlStream, path: YamlPath, options?: DocumentOption & {
|
|
38
|
+
recursive?: boolean;
|
|
39
|
+
}): void;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// Structural ops: the JSON dialect's catalogue over `yaml` nodes, with "object" read as mapping
|
|
2
|
+
// and "array" as sequence.
|
|
3
|
+
import { isAlias, isMap, isSeq } from "yaml";
|
|
4
|
+
import { deepEqual } from "../../core/deep-equal.js";
|
|
5
|
+
import { formatPath } from "../../core/json-value.js";
|
|
6
|
+
import { assertAliasesResolve, assertPath, assertPosition, assertValue, createNode, fail, findPair, isPlainObject, keyLabel, replacement, resolve, resolveAlias, selectDocument, toValue, writeSlot, } from "./paths.js";
|
|
7
|
+
function upsert(op, document, path, value, options) {
|
|
8
|
+
assertPosition(op, path, document, options);
|
|
9
|
+
const slot = resolve(op, document, path, "create");
|
|
10
|
+
if (slot.node !== null && deepEqual(toValue(document, slot.node), value))
|
|
11
|
+
return;
|
|
12
|
+
if (isSeq(slot.parent) && slot.segment >= slot.parent.items.length) {
|
|
13
|
+
fail(op, path, `index ${String(slot.segment)} is out of range for a sequence of ${slot.parent.items.length}.`);
|
|
14
|
+
}
|
|
15
|
+
writeSlot(document, slot, replacement(document, slot.node, value), options);
|
|
16
|
+
}
|
|
17
|
+
/** Sets the value at `path`, creating missing parent mappings; an equal value changes nothing. */
|
|
18
|
+
export function setValue(ast, path, value, options) {
|
|
19
|
+
assertPath("setValue", path);
|
|
20
|
+
assertValue("setValue", path, value);
|
|
21
|
+
const document = selectDocument("setValue", ast, options, true);
|
|
22
|
+
upsert("setValue", document, path, value, options);
|
|
23
|
+
assertAliasesResolve("setValue", document);
|
|
24
|
+
}
|
|
25
|
+
/** Adds a mapping key that does not exist yet; an equal existing value is a no-op. */
|
|
26
|
+
export function addKey(ast, path, value, options) {
|
|
27
|
+
assertPath("addKey", path);
|
|
28
|
+
assertValue("addKey", path, value);
|
|
29
|
+
if (typeof path[path.length - 1] !== "string")
|
|
30
|
+
fail("addKey", path, "the last segment must be a key.");
|
|
31
|
+
const document = selectDocument("addKey", ast, options);
|
|
32
|
+
assertPosition("addKey", path, document, options);
|
|
33
|
+
const slot = resolve("addKey", document, path, "write");
|
|
34
|
+
if (!isMap(slot.parent))
|
|
35
|
+
fail("addKey", path, "the parent mapping does not exist.");
|
|
36
|
+
if (slot.node !== null) {
|
|
37
|
+
if (deepEqual(toValue(document, slot.node), value))
|
|
38
|
+
return;
|
|
39
|
+
fail("addKey", path, "the key already exists with a different value.");
|
|
40
|
+
}
|
|
41
|
+
writeSlot(document, slot, replacement(document, null, value), options);
|
|
42
|
+
}
|
|
43
|
+
/** Removes a mapping key and its comments; an absent key is a no-op. */
|
|
44
|
+
export function removeKey(ast, path, options) {
|
|
45
|
+
assertPath("removeKey", path);
|
|
46
|
+
if (typeof path[path.length - 1] !== "string")
|
|
47
|
+
fail("removeKey", path, "the last segment must be a key.");
|
|
48
|
+
const document = selectDocument("removeKey", ast, options);
|
|
49
|
+
const slot = resolve("removeKey", document, path, "write");
|
|
50
|
+
if (slot.node === null || !isMap(slot.parent))
|
|
51
|
+
return;
|
|
52
|
+
const map = slot.parent;
|
|
53
|
+
map.items.splice(map.items.indexOf(findPair(map, slot.segment)), 1);
|
|
54
|
+
assertAliasesResolve("removeKey", document);
|
|
55
|
+
}
|
|
56
|
+
/** Renames a mapping key in place, keeping its value, position, and comments. */
|
|
57
|
+
export function renameKey(ast, path, newKey, options) {
|
|
58
|
+
assertPath("renameKey", path);
|
|
59
|
+
if (typeof path[path.length - 1] !== "string")
|
|
60
|
+
fail("renameKey", path, "the last segment must be a key.");
|
|
61
|
+
if (typeof newKey !== "string")
|
|
62
|
+
fail("renameKey", path, "newKey must be a string.");
|
|
63
|
+
const document = selectDocument("renameKey", ast, options);
|
|
64
|
+
const slot = resolve("renameKey", document, path, "write");
|
|
65
|
+
const map = isMap(slot.parent) ? slot.parent : null;
|
|
66
|
+
const target = map === null ? undefined : findPair(map, newKey);
|
|
67
|
+
if (slot.node === null) {
|
|
68
|
+
if (target !== undefined)
|
|
69
|
+
return;
|
|
70
|
+
fail("renameKey", path, `neither the key nor "${newKey}" exists.`);
|
|
71
|
+
}
|
|
72
|
+
if (target !== undefined && newKey !== slot.segment)
|
|
73
|
+
fail("renameKey", path, `"${newKey}" already exists.`);
|
|
74
|
+
const pair = findPair(map, slot.segment);
|
|
75
|
+
pair.key.value = newKey;
|
|
76
|
+
}
|
|
77
|
+
function sequenceAt(op, document, path, mode) {
|
|
78
|
+
const slot = resolve(op, document, path, mode);
|
|
79
|
+
if (slot.node === null) {
|
|
80
|
+
if (mode !== "create")
|
|
81
|
+
return null;
|
|
82
|
+
const seq = createNode(document, []);
|
|
83
|
+
writeSlot(document, slot, seq);
|
|
84
|
+
return seq;
|
|
85
|
+
}
|
|
86
|
+
if (isAlias(slot.node))
|
|
87
|
+
fail(op, path, `the value is the alias *${slot.node.source}; edit the anchored node instead.`);
|
|
88
|
+
if (!isSeq(slot.node))
|
|
89
|
+
fail(op, path, "the value is not a sequence.");
|
|
90
|
+
return slot.node;
|
|
91
|
+
}
|
|
92
|
+
function requireSequence(op, document, path) {
|
|
93
|
+
const seq = sequenceAt(op, document, path, "write");
|
|
94
|
+
if (seq === null)
|
|
95
|
+
fail(op, path, "no sequence exists at this path.");
|
|
96
|
+
return seq;
|
|
97
|
+
}
|
|
98
|
+
function assertIndex(op, path, index, max) {
|
|
99
|
+
if (typeof index !== "number" || !Number.isInteger(index) || index < 0 || index > max) {
|
|
100
|
+
fail(op, path, `index ${String(index)} is out of range (0..${max}).`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Inserts a sequence item before `index`; `index` equal to the length appends. */
|
|
104
|
+
export function insertItem(ast, path, index, value, options) {
|
|
105
|
+
assertPath("insertItem", path);
|
|
106
|
+
assertValue("insertItem", path, value);
|
|
107
|
+
const document = selectDocument("insertItem", ast, options);
|
|
108
|
+
const seq = requireSequence("insertItem", document, path);
|
|
109
|
+
assertIndex("insertItem", path, index, seq.items.length);
|
|
110
|
+
seq.items.splice(index, 0, createNode(document, value));
|
|
111
|
+
}
|
|
112
|
+
/** Appends a sequence item, creating the sequence; `{ unique: true }` skips a deep-equal duplicate. */
|
|
113
|
+
export function appendItem(ast, path, value, options) {
|
|
114
|
+
assertPath("appendItem", path);
|
|
115
|
+
assertValue("appendItem", path, value);
|
|
116
|
+
const document = selectDocument("appendItem", ast, options, true);
|
|
117
|
+
const seq = sequenceAt("appendItem", document, path, "create");
|
|
118
|
+
if (options?.unique === true && seq.items.some((item) => deepEqual(toValue(document, item), value)))
|
|
119
|
+
return;
|
|
120
|
+
seq.items.push(createNode(document, value));
|
|
121
|
+
}
|
|
122
|
+
/** Removes the sequence item at `index`. */
|
|
123
|
+
export function removeItem(ast, path, index, options) {
|
|
124
|
+
assertPath("removeItem", path);
|
|
125
|
+
const document = selectDocument("removeItem", ast, options);
|
|
126
|
+
const seq = requireSequence("removeItem", document, path);
|
|
127
|
+
assertIndex("removeItem", path, index, seq.items.length - 1);
|
|
128
|
+
seq.items.splice(index, 1);
|
|
129
|
+
assertAliasesResolve("removeItem", document);
|
|
130
|
+
}
|
|
131
|
+
/** Removes every sequence item deep-equal to `value`; no match is a no-op. */
|
|
132
|
+
export function removeValue(ast, path, value, options) {
|
|
133
|
+
assertPath("removeValue", path);
|
|
134
|
+
assertValue("removeValue", path, value);
|
|
135
|
+
const document = selectDocument("removeValue", ast, options);
|
|
136
|
+
const seq = sequenceAt("removeValue", document, path, "write");
|
|
137
|
+
if (seq === null)
|
|
138
|
+
return;
|
|
139
|
+
seq.items = seq.items.filter((item) => !deepEqual(toValue(document, item), value));
|
|
140
|
+
assertAliasesResolve("removeValue", document);
|
|
141
|
+
}
|
|
142
|
+
/** Replaces the sequence item at `index`, keeping its comments. */
|
|
143
|
+
export function replaceItem(ast, path, index, value, options) {
|
|
144
|
+
assertPath("replaceItem", path);
|
|
145
|
+
assertValue("replaceItem", path, value);
|
|
146
|
+
const document = selectDocument("replaceItem", ast, options);
|
|
147
|
+
const seq = requireSequence("replaceItem", document, path);
|
|
148
|
+
assertIndex("replaceItem", path, index, seq.items.length - 1);
|
|
149
|
+
const old = seq.items[index];
|
|
150
|
+
if (deepEqual(toValue(document, old), value))
|
|
151
|
+
return;
|
|
152
|
+
seq.items[index] = replacement(document, old, value);
|
|
153
|
+
assertAliasesResolve("replaceItem", document);
|
|
154
|
+
}
|
|
155
|
+
/** Moves the item at `from` so it sits at `to`; the others keep their relative order. */
|
|
156
|
+
export function moveItem(ast, path, from, to, options) {
|
|
157
|
+
assertPath("moveItem", path);
|
|
158
|
+
const document = selectDocument("moveItem", ast, options);
|
|
159
|
+
const seq = requireSequence("moveItem", document, path);
|
|
160
|
+
assertIndex("moveItem", path, from, seq.items.length - 1);
|
|
161
|
+
assertIndex("moveItem", path, to, seq.items.length - 1);
|
|
162
|
+
const [item] = seq.items.splice(from, 1);
|
|
163
|
+
seq.items.splice(to, 0, item);
|
|
164
|
+
assertAliasesResolve("moveItem", document);
|
|
165
|
+
}
|
|
166
|
+
function changes(current, incoming) {
|
|
167
|
+
if (!isPlainObject(current))
|
|
168
|
+
return true;
|
|
169
|
+
return Object.entries(incoming).some(([key, value]) => isPlainObject(value) && isPlainObject(current[key]) ? changes(current[key], value) : !deepEqual(current[key], value));
|
|
170
|
+
}
|
|
171
|
+
function mergeInto(document, map, incoming, path) {
|
|
172
|
+
for (const [key, value] of Object.entries(incoming)) {
|
|
173
|
+
const pair = findPair(map, key);
|
|
174
|
+
const keyPath = [...path, key];
|
|
175
|
+
const current = pair === undefined ? undefined : toValue(document, pair.value);
|
|
176
|
+
if (pair !== undefined && !(isPlainObject(value) ? changes(current, value) : !deepEqual(current, value)))
|
|
177
|
+
continue;
|
|
178
|
+
const node = pair?.value ?? null;
|
|
179
|
+
if (isPlainObject(value) && isPlainObject(current)) {
|
|
180
|
+
if (isAlias(node))
|
|
181
|
+
fail("merge", keyPath, `the value is the alias *${node.source}; edit the anchored node instead.`);
|
|
182
|
+
mergeInto(document, node, value, keyPath);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
writeSlot(document, { parent: map, segment: key, node }, replacement(document, node, value));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** Deep-merges a plain object into the mapping at `path`, creating it when absent. */
|
|
189
|
+
export function merge(ast, path, value, options) {
|
|
190
|
+
assertPath("merge", path);
|
|
191
|
+
if (!isPlainObject(value))
|
|
192
|
+
fail("merge", path, "the merged value must be a plain object.");
|
|
193
|
+
assertValue("merge", path, value);
|
|
194
|
+
const document = selectDocument("merge", ast, options, true);
|
|
195
|
+
const slot = resolve("merge", document, path, "create");
|
|
196
|
+
if (slot.node !== null && !changes(toValue(document, slot.node), value))
|
|
197
|
+
return;
|
|
198
|
+
if (isAlias(slot.node))
|
|
199
|
+
fail("merge", path, `the value is the alias *${slot.node.source}; edit the anchored node instead.`);
|
|
200
|
+
if (slot.node !== null && !isMap(slot.node))
|
|
201
|
+
fail("merge", path, "the target is not a mapping.");
|
|
202
|
+
let map = slot.node;
|
|
203
|
+
if (map === null) {
|
|
204
|
+
map = createNode(document, {});
|
|
205
|
+
writeSlot(document, slot, map);
|
|
206
|
+
}
|
|
207
|
+
mergeInto(document, map, value, path);
|
|
208
|
+
}
|
|
209
|
+
/** Replaces the value at `path` with `fn(current)`; `current` is undefined when absent. */
|
|
210
|
+
export function updateValue(ast, path, fn, options) {
|
|
211
|
+
assertPath("updateValue", path);
|
|
212
|
+
if (typeof fn !== "function")
|
|
213
|
+
fail("updateValue", path, "fn must be a function.");
|
|
214
|
+
const document = selectDocument("updateValue", ast, options, true);
|
|
215
|
+
const current = toValue(document, resolve("updateValue", document, path, "read").node);
|
|
216
|
+
const next = fn(current);
|
|
217
|
+
assertValue("updateValue", path, next);
|
|
218
|
+
upsert("updateValue", document, path, next);
|
|
219
|
+
assertAliasesResolve("updateValue", document);
|
|
220
|
+
}
|
|
221
|
+
const isInside = (inner, outer) => inner.length > outer.length && outer.every((segment, index) => inner[index] === segment);
|
|
222
|
+
// Returns the detached node and the comment above its key, which travels with it.
|
|
223
|
+
function detach(document, path) {
|
|
224
|
+
const slot = resolve("moveValue", document, path, "write");
|
|
225
|
+
const node = slot.node;
|
|
226
|
+
let keyComment;
|
|
227
|
+
if (slot.parent === null)
|
|
228
|
+
document.contents = null;
|
|
229
|
+
else if (isMap(slot.parent)) {
|
|
230
|
+
const pair = findPair(slot.parent, slot.segment);
|
|
231
|
+
keyComment = pair.key.commentBefore ?? undefined;
|
|
232
|
+
slot.parent.items.splice(slot.parent.items.indexOf(pair), 1);
|
|
233
|
+
}
|
|
234
|
+
else
|
|
235
|
+
slot.parent.items.splice(slot.segment, 1);
|
|
236
|
+
return { node, keyComment };
|
|
237
|
+
}
|
|
238
|
+
/** Moves the value at `from` to `to` (RFC 6902 move): a number last segment inserts. */
|
|
239
|
+
export function moveValue(ast, from, to, options) {
|
|
240
|
+
assertPath("moveValue", from);
|
|
241
|
+
assertPath("moveValue", to);
|
|
242
|
+
const document = selectDocument("moveValue", ast, options);
|
|
243
|
+
if (isInside(to, from))
|
|
244
|
+
fail("moveValue", to, `the destination lies inside ${formatPath(from)}.`);
|
|
245
|
+
if (deepEqual(from, to))
|
|
246
|
+
return;
|
|
247
|
+
const source = resolve("moveValue", document, from, "write");
|
|
248
|
+
if (source.node === null) {
|
|
249
|
+
if (resolve("moveValue", document, to, "read").node !== null)
|
|
250
|
+
return;
|
|
251
|
+
fail("moveValue", from, "neither the source nor the destination exists.");
|
|
252
|
+
}
|
|
253
|
+
const { node, keyComment } = detach(document, from);
|
|
254
|
+
const last = to[to.length - 1];
|
|
255
|
+
if (typeof last === "number") {
|
|
256
|
+
const seq = requireSequence("moveValue", document, to.slice(0, -1));
|
|
257
|
+
assertIndex("moveValue", to, last, seq.items.length);
|
|
258
|
+
if (keyComment !== undefined)
|
|
259
|
+
node.commentBefore = node.commentBefore ? `${keyComment}\n${node.commentBefore}` : keyComment;
|
|
260
|
+
seq.items.splice(last, 0, node);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
const slot = resolve("moveValue", document, to, "create");
|
|
264
|
+
writeSlot(document, slot, node);
|
|
265
|
+
if (slot.node === null && keyComment !== undefined && isMap(slot.parent)) {
|
|
266
|
+
findPair(slot.parent, last).key.commentBefore = keyComment;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
assertAliasesResolve("moveValue", document);
|
|
270
|
+
}
|
|
271
|
+
/** Copies the resolved data at `from` to `to` with `setValue`'s rules; anchors are not copied. */
|
|
272
|
+
export function copyValue(ast, from, to, options) {
|
|
273
|
+
assertPath("copyValue", from);
|
|
274
|
+
assertPath("copyValue", to);
|
|
275
|
+
const document = selectDocument("copyValue", ast, options);
|
|
276
|
+
const source = resolve("copyValue", document, from, "read").node;
|
|
277
|
+
if (source === null)
|
|
278
|
+
fail("copyValue", from, "the source does not exist.");
|
|
279
|
+
const value = toValue(document, resolveAlias(document, source));
|
|
280
|
+
assertValue("copyValue", from, value);
|
|
281
|
+
upsert("copyValue", document, to, value);
|
|
282
|
+
assertAliasesResolve("copyValue", document);
|
|
283
|
+
}
|
|
284
|
+
function sortMap(map, recursive) {
|
|
285
|
+
const label = (pair) => keyLabel(pair.key) ?? "";
|
|
286
|
+
map.items.sort((a, b) => (label(a) < label(b) ? -1 : label(a) > label(b) ? 1 : 0));
|
|
287
|
+
if (recursive)
|
|
288
|
+
map.items.forEach((pair) => sortNested(pair.value));
|
|
289
|
+
}
|
|
290
|
+
function sortNested(node) {
|
|
291
|
+
if (isMap(node))
|
|
292
|
+
sortMap(node, true);
|
|
293
|
+
else if (isSeq(node))
|
|
294
|
+
node.items.forEach((item) => sortNested(item));
|
|
295
|
+
}
|
|
296
|
+
/** Orders a mapping's keys by code point; `{ recursive: true }` also sorts nested mappings. */
|
|
297
|
+
export function sortKeys(ast, path, options) {
|
|
298
|
+
assertPath("sortKeys", path);
|
|
299
|
+
const document = selectDocument("sortKeys", ast, options);
|
|
300
|
+
const node = resolve("sortKeys", document, path, "write").node;
|
|
301
|
+
if (isAlias(node))
|
|
302
|
+
fail("sortKeys", path, `the value is the alias *${node.source}; edit the anchored node instead.`);
|
|
303
|
+
if (!isMap(node))
|
|
304
|
+
fail("sortKeys", path, "the value is not a mapping.");
|
|
305
|
+
sortMap(node, options?.recursive === true);
|
|
306
|
+
assertAliasesResolve("sortKeys", document);
|
|
307
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifestVersion": 1,
|
|
3
3
|
"algorithm": "sha256",
|
|
4
4
|
"entry": "dist/bin/pbuilder-runner.js",
|
|
5
|
-
"packageVersion": "0.
|
|
5
|
+
"packageVersion": "0.5.0",
|
|
6
6
|
"files": [
|
|
7
7
|
{
|
|
8
8
|
"path": "dist/bin/pbuilder-runner.js",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
},
|
|
63
63
|
{
|
|
64
64
|
"path": "dist/transport/error-text.js",
|
|
65
|
-
"sha256": "
|
|
65
|
+
"sha256": "e857d5f2e844564f9d21930e833bd2e7b179253235d3c38a3af389f2573ca329"
|
|
66
66
|
},
|
|
67
67
|
{
|
|
68
68
|
"path": "dist/transport/exit-codes.js",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
{
|
|
100
100
|
"path": "package.json",
|
|
101
|
-
"sha256": "
|
|
101
|
+
"sha256": "be7fadf1443334e3af115de2b5c9fc3a081a19cda6a58f0659adc0e0530ef754"
|
|
102
102
|
}
|
|
103
103
|
]
|
|
104
104
|
}
|
|
@@ -6,4 +6,9 @@ export declare function boundMessage(message: string): string;
|
|
|
6
6
|
export declare function formatRelativeCandidate(candidate: string): string;
|
|
7
7
|
export declare function toProjectRelativePath(subjectPath: string, projectRoot?: string): string;
|
|
8
8
|
export declare function composeWithToken(prefix: string, token: string, suffix?: string): string;
|
|
9
|
-
export
|
|
9
|
+
export interface ScrubAnchor {
|
|
10
|
+
token: string;
|
|
11
|
+
path: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function hostAnchors(projectRoot?: string): ScrubAnchor[];
|
|
14
|
+
export declare function scrubAbsolutePaths(message: string, projectRoot?: string, anchors?: readonly ScrubAnchor[]): string;
|