@aspruyt/xfg 8.1.1 → 8.1.3
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/dist/config/merge.d.ts +3 -3
- package/dist/config/merge.js +72 -29
- package/dist/config/normalizer.js +2 -2
- package/package.json +2 -2
package/dist/config/merge.d.ts
CHANGED
|
@@ -31,9 +31,9 @@ export declare function deepMerge(base: Record<string, unknown>, overlay: Record
|
|
|
31
31
|
* Works recursively on nested objects and arrays.
|
|
32
32
|
* Standard $-prefixed keys ($schema, $id, $ref, etc.) are preserved.
|
|
33
33
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
34
|
+
* A valid directive-only object that is the value of a key had no base array
|
|
35
|
+
* to merge with, and is replaced with its $values array. A directive-only object
|
|
36
|
+
* that is invalid, an array item, or the root is a ValidationError.
|
|
37
37
|
*
|
|
38
38
|
* @param location - Prefixes errors, e.g. the file name being stripped
|
|
39
39
|
*/
|
package/dist/config/merge.js
CHANGED
|
@@ -36,7 +36,15 @@ function childPath(path, key) {
|
|
|
36
36
|
}
|
|
37
37
|
function mergeError(ctx, path, message) {
|
|
38
38
|
const prefix = ctx.location ? `${ctx.location}: ` : "";
|
|
39
|
-
|
|
39
|
+
const pathPrefix = path ? `${path}: ` : "";
|
|
40
|
+
return new ValidationError(`${prefix}${pathPrefix}${message}`);
|
|
41
|
+
}
|
|
42
|
+
function describeValue(value) {
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
return "none";
|
|
45
|
+
if (typeof value === "string")
|
|
46
|
+
return `'${value}'`;
|
|
47
|
+
return JSON.stringify(value);
|
|
40
48
|
}
|
|
41
49
|
/**
|
|
42
50
|
* Returns the directive's $matchBy key, or undefined when it has none.
|
|
@@ -51,15 +59,39 @@ function readMatchBy(directive, ctx, path) {
|
|
|
51
59
|
}
|
|
52
60
|
const strategy = directive.$arrayMerge;
|
|
53
61
|
if (strategy !== "merge") {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
throw mergeError(ctx, path, `$matchBy requires $arrayMerge: merge, got ${describeValue(strategy)}`);
|
|
63
|
+
}
|
|
64
|
+
return matchBy;
|
|
65
|
+
}
|
|
66
|
+
function isDirectiveOnly(value) {
|
|
67
|
+
const keys = Object.keys(value);
|
|
68
|
+
return keys.length > 0 && keys.every((k) => XFG_DIRECTIVES.has(k));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Validates a value's directive keys and returns its $matchBy.
|
|
72
|
+
* An object of only directive keys must be a complete directive, or it would resolve to nothing.
|
|
73
|
+
*/
|
|
74
|
+
function readDirective(value, ctx, path) {
|
|
75
|
+
if (!isPlainObject(value))
|
|
76
|
+
return undefined;
|
|
77
|
+
const matchBy = readMatchBy(value, ctx, path);
|
|
78
|
+
if (!isDirectiveOnly(value))
|
|
79
|
+
return matchBy;
|
|
80
|
+
if (!isArrayMergeStrategy(value.$arrayMerge)) {
|
|
81
|
+
const strategies = [...arrayMergeStrategies.keys()].join(", ");
|
|
82
|
+
throw mergeError(ctx, path, `$arrayMerge must be one of ${strategies}, got ${describeValue(value.$arrayMerge)}`);
|
|
83
|
+
}
|
|
84
|
+
if (!Array.isArray(value.$values)) {
|
|
85
|
+
throw mergeError(ctx, path, `$values must be an array, got ${describeValue(value.$values)}`);
|
|
60
86
|
}
|
|
61
87
|
return matchBy;
|
|
62
88
|
}
|
|
89
|
+
/** Rejects directive-only objects; mixed-key objects keep their directive keys stripped silently. */
|
|
90
|
+
function assertNotDirective(value, ctx, path, position) {
|
|
91
|
+
if (isPlainObject(value) && isDirectiveOnly(value)) {
|
|
92
|
+
throw mergeError(ctx, path, `a directive must be the value of a key whose base is an array, not ${position}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
63
95
|
function assertKeyedItems(items, matchBy, label, ctx, path) {
|
|
64
96
|
const firstIndexByValue = new Map();
|
|
65
97
|
for (let i = 0; i < items.length; i++) {
|
|
@@ -142,7 +174,7 @@ const arrayMergeStrategies = new Map([
|
|
|
142
174
|
function isUnresolvedDirective(value) {
|
|
143
175
|
if (!isPlainObject(value))
|
|
144
176
|
return false;
|
|
145
|
-
return (
|
|
177
|
+
return (isDirectiveOnly(value) &&
|
|
146
178
|
isArrayMergeStrategy(value.$arrayMerge) &&
|
|
147
179
|
Array.isArray(value.$values));
|
|
148
180
|
}
|
|
@@ -161,6 +193,8 @@ function mergeArrays(base, overlay, strategy, ctx, path) {
|
|
|
161
193
|
* @param ctx - Merge context with array strategies
|
|
162
194
|
*/
|
|
163
195
|
export function deepMerge(base, overlay, ctx) {
|
|
196
|
+
assertNotDirective(base, ctx, "", "the content root");
|
|
197
|
+
assertNotDirective(overlay, ctx, "", "the content root");
|
|
164
198
|
return mergeObjects(base, overlay, ctx, "");
|
|
165
199
|
}
|
|
166
200
|
function mergeObjects(base, overlay, ctx, path) {
|
|
@@ -173,12 +207,10 @@ function mergeObjects(base, overlay, ctx, path) {
|
|
|
173
207
|
return result;
|
|
174
208
|
}
|
|
175
209
|
function mergeValue(baseValue, overlayValue, ctx, path) {
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
? resolveBaseDirective(baseValue, ctx, path)
|
|
179
|
-
: baseValue;
|
|
210
|
+
const resolvedBase = resolveBase(baseValue, ctx, path);
|
|
211
|
+
const overlayMatchBy = readDirective(overlayValue, ctx, path);
|
|
180
212
|
if (isArrayDirective(overlayValue)) {
|
|
181
|
-
const merged = applyArrayDirective(resolvedBase, overlayValue, ctx, path);
|
|
213
|
+
const merged = applyArrayDirective(resolvedBase, overlayValue, overlayMatchBy, ctx, path);
|
|
182
214
|
if (merged !== undefined)
|
|
183
215
|
return merged;
|
|
184
216
|
}
|
|
@@ -190,12 +222,18 @@ function mergeValue(baseValue, overlayValue, ctx, path) {
|
|
|
190
222
|
}
|
|
191
223
|
return overlayValue;
|
|
192
224
|
}
|
|
193
|
-
|
|
194
|
-
|
|
225
|
+
/**
|
|
226
|
+
* A directive left unresolved by an earlier layer with no base array acts as its $values.
|
|
227
|
+
* Any other base is returned as-is once its directive keys are valid.
|
|
228
|
+
*/
|
|
229
|
+
function resolveBase(base, ctx, path) {
|
|
230
|
+
const matchBy = readDirective(base, ctx, path);
|
|
231
|
+
if (!isUnresolvedDirective(base))
|
|
232
|
+
return base;
|
|
195
233
|
if (matchBy !== undefined) {
|
|
196
|
-
assertKeyedItems(
|
|
234
|
+
assertKeyedItems(base.$values, matchBy, "base", ctx, path);
|
|
197
235
|
}
|
|
198
|
-
return
|
|
236
|
+
return base.$values;
|
|
199
237
|
}
|
|
200
238
|
function isArrayDirective(value) {
|
|
201
239
|
return (isPlainObject(value) && ("$arrayMerge" in value || "$matchBy" in value));
|
|
@@ -208,10 +246,9 @@ function isArrayMergeStrategy(value) {
|
|
|
208
246
|
* Applies an overlay directive to the base array.
|
|
209
247
|
* Returns undefined when it doesn't apply, so the caller falls back to a plain merge.
|
|
210
248
|
*/
|
|
211
|
-
function applyArrayDirective(base, directive, ctx, path) {
|
|
249
|
+
function applyArrayDirective(base, directive, matchBy, ctx, path) {
|
|
212
250
|
const strategy = directive.$arrayMerge;
|
|
213
251
|
const values = directive.$values;
|
|
214
|
-
const matchBy = readMatchBy(directive, ctx, path);
|
|
215
252
|
if (matchBy !== undefined && Array.isArray(values)) {
|
|
216
253
|
assertKeyedItems(values, matchBy, "overlay", ctx, path);
|
|
217
254
|
}
|
|
@@ -231,17 +268,25 @@ function applyArrayDirective(base, directive, ctx, path) {
|
|
|
231
268
|
* Works recursively on nested objects and arrays.
|
|
232
269
|
* Standard $-prefixed keys ($schema, $id, $ref, etc.) are preserved.
|
|
233
270
|
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
271
|
+
* A valid directive-only object that is the value of a key had no base array
|
|
272
|
+
* to merge with, and is replaced with its $values array. A directive-only object
|
|
273
|
+
* that is invalid, an array item, or the root is a ValidationError.
|
|
237
274
|
*
|
|
238
275
|
* @param location - Prefixes errors, e.g. the file name being stripped
|
|
239
276
|
*/
|
|
240
277
|
export function stripMergeDirectives(obj, location) {
|
|
241
|
-
|
|
278
|
+
const errCtx = { location };
|
|
279
|
+
assertNotDirective(obj, errCtx, "", "the content root");
|
|
280
|
+
return stripObject(obj, errCtx, "");
|
|
242
281
|
}
|
|
243
282
|
function stripItems(items, errCtx, path) {
|
|
244
|
-
return items.map((item, i) =>
|
|
283
|
+
return items.map((item, i) => {
|
|
284
|
+
if (!isPlainObject(item))
|
|
285
|
+
return item;
|
|
286
|
+
const itemPath = `${path}[${i}]`;
|
|
287
|
+
assertNotDirective(item, errCtx, itemPath, "an array item");
|
|
288
|
+
return stripObject(item, errCtx, itemPath);
|
|
289
|
+
});
|
|
245
290
|
}
|
|
246
291
|
function stripObject(obj, errCtx, path) {
|
|
247
292
|
const result = {};
|
|
@@ -249,13 +294,11 @@ function stripObject(obj, errCtx, path) {
|
|
|
249
294
|
if (XFG_DIRECTIVES.has(key))
|
|
250
295
|
continue;
|
|
251
296
|
const keyPath = childPath(path, key);
|
|
252
|
-
// Validate before branching: an invalid
|
|
253
|
-
const matchBy =
|
|
254
|
-
? readMatchBy(value, errCtx, keyPath)
|
|
255
|
-
: undefined;
|
|
297
|
+
// Validate before branching: an invalid directive is not resolved and would be stripped silently
|
|
298
|
+
const matchBy = readDirective(value, errCtx, keyPath);
|
|
256
299
|
if (isUnresolvedDirective(value)) {
|
|
257
300
|
if (matchBy !== undefined) {
|
|
258
|
-
assertKeyedItems(value.$values, matchBy, "
|
|
301
|
+
assertKeyedItems(value.$values, matchBy, "$values", errCtx, keyPath);
|
|
259
302
|
}
|
|
260
303
|
result[key] = stripItems(value.$values, errCtx, keyPath);
|
|
261
304
|
}
|
|
@@ -29,7 +29,7 @@ function resolveFileContent(fileName, rootContent, repoOverride, mergeStrategy)
|
|
|
29
29
|
: null;
|
|
30
30
|
}
|
|
31
31
|
if (!repoOverride?.content) {
|
|
32
|
-
return
|
|
32
|
+
return cloneContent(rootContent, fileName);
|
|
33
33
|
}
|
|
34
34
|
return mergeContentPair(fileName, rootContent, repoOverride.content, mergeStrategy);
|
|
35
35
|
}
|
|
@@ -46,7 +46,7 @@ function mergeContentPair(fileName, base, overlay, strategy) {
|
|
|
46
46
|
const merged = deepMerge(structuredClone(base), overlay, ctx);
|
|
47
47
|
return stripMergeDirectives(merged, fileName);
|
|
48
48
|
}
|
|
49
|
-
return overlay;
|
|
49
|
+
return cloneContent(overlay, fileName);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Checks whether an object's `inherit` property is not explicitly set to false.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aspruyt/xfg",
|
|
3
|
-
"version": "8.1.
|
|
3
|
+
"version": "8.1.3",
|
|
4
4
|
"description": "Manage files, settings, and repositories across GitHub, Azure DevOps, and GitLab — declaratively, from a single YAML config",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"bin": {
|
|
14
|
-
"xfg": "
|
|
14
|
+
"xfg": "dist/cli.js"
|
|
15
15
|
},
|
|
16
16
|
"sideEffects": [
|
|
17
17
|
"./dist/cli.js"
|