@hop-top/kit 0.5.0-alpha.6 → 0.5.0-alpha.7
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/CHANGELOG.md +36 -0
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +3 -3
- package/dist/{alias-BOj0-VAr.d.ts → alias-BsJ2jGqQ.d.ts} +9 -1
- package/dist/alias.d.ts +1 -1
- package/dist/alias.js +4 -2
- package/dist/chunk-263IZ6Z6.js +915 -0
- package/dist/{chunk-S45UHXQF.js → chunk-DIDN4X5C.js} +8 -6
- package/dist/{chunk-7FDIN5KE.js → chunk-GHPJJV5L.js} +8 -4
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/{chunk-GAZPYCQD.js → chunk-NLMS7HGP.js} +93 -35
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/cli.js +6 -4
- package/dist/completion-sDKVXPUL.d.ts +109 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +2 -1
- package/dist/completion.js +23 -2
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -1
- package/dist/config.js +4 -2
- package/dist/index.d.ts +7 -5
- package/dist/index.js +26 -18
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +1 -1
- package/dist/log.js +2 -2
- package/dist/toolspec-UrWnoJ3Y.d.ts +336 -0
- package/dist/toolspec.d.ts +2 -1
- package/dist/toolspec.js +27 -2
- package/package.json +19 -3
- package/src/alias.test.ts +110 -1
- package/src/alias.ts +19 -4
- package/src/cli.ts +5 -0
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +70 -10
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +297 -0
- package/src/completion.test.ts +256 -145
- package/src/completion.ts +764 -121
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +41 -0
- package/src/config.ts +265 -18
- package/src/index.test.ts +2 -1
- package/src/index.ts +5 -1
- package/src/log.test.ts +270 -1
- package/src/log.ts +172 -44
- package/src/published-types.test.ts +18 -0
- package/src/toolspec-internal.ts +197 -0
- package/src/toolspec-manifest.test.ts +82 -0
- package/src/toolspec-manifest.ts +404 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +216 -0
- package/src/toolspec.ts +159 -0
- package/test/toolspec/fixture.ts +73 -0
- package/dist/chunk-6RFRV4VP.js +0 -128
- package/dist/chunk-IQXEISMC.js +0 -142
- package/dist/chunk-W7ERKNSQ.js +0 -171
- package/dist/completion-CVnPcWMZ.d.ts +0 -32
- package/dist/config-Bm9fBXch.d.ts +0 -134
- package/dist/log-uKjHsK-c.d.ts +0 -62
- package/dist/toolspec-DPBr18Ub.d.ts +0 -106
package/src/config.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
*
|
|
4
|
-
* Layered
|
|
4
|
+
* Layered configuration loader (YAML by default) — TypeScript port of
|
|
5
|
+
* hop.top/kit/config.
|
|
5
6
|
*
|
|
6
7
|
* Merges configuration from up to three file layers in priority order:
|
|
7
8
|
* system → user → project → envOverride
|
|
@@ -17,7 +18,13 @@
|
|
|
17
18
|
* - Non-plain objects (`Date`, `Map`, class instances) are never merged
|
|
18
19
|
* into; a later value replaces them.
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
+
* {@link mergeLayers} applies the same rules to objects you already parsed,
|
|
22
|
+
* and {@link MergeOptions} (also accepted by {@link load} as `merge`) can
|
|
23
|
+
* relax them: `null` keeps the lower value, objects merge only to a set depth,
|
|
24
|
+
* arrays concatenate or union, globally or per dotted key path. A file in
|
|
25
|
+
* another format goes through {@link Options.parse}.
|
|
26
|
+
*
|
|
27
|
+
* The defaults match Go's `kit/core/config` for struct fields. Go differs where its
|
|
21
28
|
* target type differs: a `map[...]` value or an `any` field is replaced
|
|
22
29
|
* whole rather than merged, and a type conflict on a typed field is a decode
|
|
23
30
|
* error rather than a replacement.
|
|
@@ -66,6 +73,7 @@
|
|
|
66
73
|
*/
|
|
67
74
|
|
|
68
75
|
import * as fs from "fs";
|
|
76
|
+
import * as path from "path";
|
|
69
77
|
import * as yaml from "js-yaml";
|
|
70
78
|
|
|
71
79
|
// ---------------------------------------------------------------------------
|
|
@@ -106,6 +114,124 @@ export interface Options<T extends object = object> {
|
|
|
106
114
|
* @param cfg - The merged config object (same reference as `dst`).
|
|
107
115
|
*/
|
|
108
116
|
envOverride?: (cfg: T) => void;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Parser for file contents; YAML when omitted. A function parses every
|
|
120
|
+
* file. A record maps a lowercased extension with its dot (`".json"`;
|
|
121
|
+
* `""` for an extensionless file such as `.toolrc`) to a parser, and files
|
|
122
|
+
* whose extension has no entry parse as YAML. The parser gets the text and
|
|
123
|
+
* the path (so a `.js` parser can `require` the path instead); a throw
|
|
124
|
+
* fails the load like a YAML error, and a result that is not a plain
|
|
125
|
+
* object is ignored. Without a parser a `.js` file parses as YAML text
|
|
126
|
+
* and is never executed.
|
|
127
|
+
*/
|
|
128
|
+
parse?: ConfigParser | Record<string, ConfigParser>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Merge policies for the file layers, `dst` being the lowest layer. They
|
|
132
|
+
* do not touch {@link envOverride}, which assigns whatever it sets.
|
|
133
|
+
* Defaults are the module merge rules.
|
|
134
|
+
*/
|
|
135
|
+
merge?: MergeOptions;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Parses one config file's text; see {@link Options.parse}. */
|
|
139
|
+
export type ConfigParser = (text: string, filePath: string) => unknown;
|
|
140
|
+
|
|
141
|
+
/** What a `null` in a higher layer does; see {@link MergeOptions.nullPolicy}. */
|
|
142
|
+
export type NullPolicy = "clear" | "keep-lower";
|
|
143
|
+
|
|
144
|
+
/** How two arrays at one key combine; see {@link MergeOptions.arrayPolicy}. */
|
|
145
|
+
export type ArrayPolicy = "replace" | "concat" | "union";
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Custom array merge for one path: gets the lower array (already in the
|
|
149
|
+
* result), a copy of the higher array and the dotted path; returns the value
|
|
150
|
+
* to store.
|
|
151
|
+
*/
|
|
152
|
+
export type ArrayMergeFn = (lower: unknown[], higher: unknown[], path: string) => unknown;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Merge policies for {@link mergeLayers} and {@link Options.merge}. All
|
|
156
|
+
* policies act only where a higher layer's key meets a key already in the
|
|
157
|
+
* result; a value with nothing under it (a new key, a type conflict, an
|
|
158
|
+
* object past `maxDepth`) is copied whole, as is.
|
|
159
|
+
*/
|
|
160
|
+
export interface MergeOptions {
|
|
161
|
+
/**
|
|
162
|
+
* `"clear"` (default): `null` clears an object or array, sets a key that
|
|
163
|
+
* is absent or already `null`, and leaves a scalar untouched.
|
|
164
|
+
* `"keep-lower"`: `null` is skipped, so the lower value stays and an
|
|
165
|
+
* absent key stays absent.
|
|
166
|
+
*/
|
|
167
|
+
nullPolicy?: NullPolicy;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* How many object levels below the root merge key by key; deeper objects
|
|
171
|
+
* replace whole. `Infinity` (default) merges at every depth, `1` merges the
|
|
172
|
+
* root and the objects directly under it (`{db: {opts: {...}}}` merges
|
|
173
|
+
* `db` but replaces `db.opts`), `0` assigns root keys only, like
|
|
174
|
+
* `Object.assign`. Must be a non-negative integer or `Infinity`.
|
|
175
|
+
*/
|
|
176
|
+
maxDepth?: number;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* How two arrays at one key combine. `"replace"` (default): the higher
|
|
180
|
+
* array wins. `"concat"`: lower items, then higher items. `"union"`: lower
|
|
181
|
+
* then higher items, keeping the first of equal items, where plain objects
|
|
182
|
+
* and arrays compare structurally (object key order ignored) and other
|
|
183
|
+
* values by SameValueZero. A policy acts only when both sides are arrays;
|
|
184
|
+
* otherwise the higher value replaces.
|
|
185
|
+
*/
|
|
186
|
+
arrayPolicy?: ArrayPolicy;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Per-path override of {@link arrayPolicy}: dotted key path from the root
|
|
190
|
+
* (`"scope.hints"`, split on every `.` like the Go SDK's dotted keys, so a
|
|
191
|
+
* key containing a dot cannot be named) to a policy or an
|
|
192
|
+
* {@link ArrayMergeFn}, which gets the concrete path. A segment that is
|
|
193
|
+
* exactly `*` matches any one key (`"scopes.*.hints"`, never zero or
|
|
194
|
+
* several; `a*` is literal, `**` throws). When several entries match, the
|
|
195
|
+
* exact path wins, then the pattern with the fewest `*`, then the
|
|
196
|
+
* lexically smallest pattern (UTF-16 order, so `*.b.c` beats `a.*.c`).
|
|
197
|
+
*/
|
|
198
|
+
arrayPolicyByPath?: Record<string, ArrayPolicy | ArrayMergeFn>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Merges already-parsed layers, lowest priority first, into a new object and
|
|
203
|
+
* returns it. Layers that are not plain objects (`null`, arrays, scalars)
|
|
204
|
+
* are skipped; the layers are never mutated or shared with the result.
|
|
205
|
+
* Defaults follow the module merge rules; {@link MergeOptions} relaxes them.
|
|
206
|
+
*
|
|
207
|
+
* @throws {Error} `config: layer <i>: <reason>` when a layer nests deeper
|
|
208
|
+
* than 100 levels or contains a cycle, and on an invalid option.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* const cfg = mergeLayers([globalRc, projectRc], {
|
|
213
|
+
* nullPolicy: "keep-lower",
|
|
214
|
+
* maxDepth: 1,
|
|
215
|
+
* arrayPolicyByPath: { hints: "union" },
|
|
216
|
+
* });
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
export function mergeLayers<T extends object = Record<string, unknown>>(
|
|
220
|
+
layers: readonly unknown[],
|
|
221
|
+
opts: MergeOptions = {},
|
|
222
|
+
): T {
|
|
223
|
+
const policy = resolvePolicy(opts);
|
|
224
|
+
const out: Record<string, unknown> = {};
|
|
225
|
+
layers.forEach((layer, i) => {
|
|
226
|
+
if (!isPlainObject(layer)) return;
|
|
227
|
+
try {
|
|
228
|
+
checkBounds(layer);
|
|
229
|
+
} catch (err: unknown) {
|
|
230
|
+
throw new Error(`config: layer ${i}: ${errMessage(err)}`);
|
|
231
|
+
}
|
|
232
|
+
mergeInto(out, layer, policy, 0, "");
|
|
233
|
+
});
|
|
234
|
+
return out as T;
|
|
109
235
|
}
|
|
110
236
|
|
|
111
237
|
/**
|
|
@@ -115,7 +241,9 @@ export interface Options<T extends object = object> {
|
|
|
115
241
|
* `envOverride`.
|
|
116
242
|
*
|
|
117
243
|
* - Objects merge recursively; arrays and scalars replace; `null` clears
|
|
118
|
-
* objects and arrays but not scalars (see the module docs for details)
|
|
244
|
+
* objects and arrays but not scalars (see the module docs for details),
|
|
245
|
+
* unless {@link Options.merge} says otherwise.
|
|
246
|
+
* - Files parse as YAML unless {@link Options.parse} supplies a parser.
|
|
119
247
|
* - Missing files (ENOENT) are silently skipped.
|
|
120
248
|
* - A file that exists but is not valid YAML causes this function to throw
|
|
121
249
|
* a descriptive error wrapping the parse failure.
|
|
@@ -126,7 +254,8 @@ export interface Options<T extends object = object> {
|
|
|
126
254
|
* object; mutated in place.
|
|
127
255
|
* @param opts - Config sources. All fields are optional.
|
|
128
256
|
* @returns The mutated `dst` object.
|
|
129
|
-
* @throws {Error} When a file exists but cannot be parsed
|
|
257
|
+
* @throws {Error} When a file exists but cannot be parsed, or on an
|
|
258
|
+
* invalid {@link Options.merge}.
|
|
130
259
|
*
|
|
131
260
|
* @example
|
|
132
261
|
* ```ts
|
|
@@ -136,6 +265,7 @@ export interface Options<T extends object = object> {
|
|
|
136
265
|
* ```
|
|
137
266
|
*/
|
|
138
267
|
export function load<T extends object>(dst: T, opts: Options<T> = {}): T {
|
|
268
|
+
const policy = resolvePolicy(opts.merge ?? {});
|
|
139
269
|
const paths = [
|
|
140
270
|
opts.systemConfigPath,
|
|
141
271
|
opts.userConfigPath,
|
|
@@ -144,7 +274,7 @@ export function load<T extends object>(dst: T, opts: Options<T> = {}): T {
|
|
|
144
274
|
|
|
145
275
|
for (const p of paths) {
|
|
146
276
|
if (!p) continue;
|
|
147
|
-
mergeFile(dst, p);
|
|
277
|
+
mergeFile(dst, p, opts.parse, policy);
|
|
148
278
|
}
|
|
149
279
|
|
|
150
280
|
if (opts.envOverride) {
|
|
@@ -159,10 +289,16 @@ export function load<T extends object>(dst: T, opts: Options<T> = {}): T {
|
|
|
159
289
|
// ---------------------------------------------------------------------------
|
|
160
290
|
|
|
161
291
|
/**
|
|
162
|
-
* Read `path`, parse
|
|
163
|
-
* ENOENT is silently ignored; other errors
|
|
292
|
+
* Read `path`, parse it (YAML unless `parse` picks a parser), and merge into
|
|
293
|
+
* `dst` with {@link mergeInto}. ENOENT is silently ignored; other errors
|
|
294
|
+
* propagate.
|
|
164
295
|
*/
|
|
165
|
-
function mergeFile<T extends object>(
|
|
296
|
+
function mergeFile<T extends object>(
|
|
297
|
+
dst: T,
|
|
298
|
+
filePath: string,
|
|
299
|
+
parse: Options["parse"],
|
|
300
|
+
policy: Policy,
|
|
301
|
+
): void {
|
|
166
302
|
let raw: string;
|
|
167
303
|
try {
|
|
168
304
|
raw = fs.readFileSync(filePath, "utf8");
|
|
@@ -173,19 +309,72 @@ function mergeFile<T extends object>(dst: T, filePath: string): void {
|
|
|
173
309
|
|
|
174
310
|
let parsed: unknown;
|
|
175
311
|
try {
|
|
176
|
-
|
|
312
|
+
const parser = parserFor(parse, filePath);
|
|
313
|
+
parsed = parser ? parser(raw, filePath) : yaml.load(raw);
|
|
177
314
|
checkBounds(parsed);
|
|
178
315
|
} catch (err: unknown) {
|
|
179
|
-
throw new Error(
|
|
180
|
-
`config: failed to parse ${filePath}: ${err instanceof Error ? err.message : String(err)}`,
|
|
181
|
-
);
|
|
316
|
+
throw new Error(`config: failed to parse ${filePath}: ${errMessage(err)}`);
|
|
182
317
|
}
|
|
183
318
|
|
|
184
319
|
if (isPlainObject(parsed)) {
|
|
185
|
-
mergeInto(dst as Record<string, unknown>, parsed);
|
|
320
|
+
mergeInto(dst as Record<string, unknown>, parsed, policy, 0, "");
|
|
186
321
|
}
|
|
187
322
|
}
|
|
188
323
|
|
|
324
|
+
function parserFor(parse: Options["parse"], filePath: string): ConfigParser | undefined {
|
|
325
|
+
if (parse === undefined) return undefined;
|
|
326
|
+
if (typeof parse === "function") return parse;
|
|
327
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
328
|
+
return Object.prototype.hasOwnProperty.call(parse, ext) ? parse[ext] : undefined;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function errMessage(err: unknown): string {
|
|
332
|
+
return err instanceof Error ? err.message : String(err);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** {@link MergeOptions} validated, with defaults filled in. */
|
|
336
|
+
interface Policy {
|
|
337
|
+
keepLower: boolean;
|
|
338
|
+
maxDepth: number;
|
|
339
|
+
array: ArrayPolicy;
|
|
340
|
+
byPath: Map<string, ArrayPolicy | ArrayMergeFn>;
|
|
341
|
+
/** Wildcard patterns, best first: fewest `*`, then lexically smallest. */
|
|
342
|
+
wildcards: { pattern: string; segs: string[]; rule: ArrayPolicy | ArrayMergeFn }[];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const ARRAY_POLICIES: readonly string[] = ["replace", "concat", "union"];
|
|
346
|
+
|
|
347
|
+
function resolvePolicy(opts: MergeOptions): Policy {
|
|
348
|
+
const { nullPolicy = "clear", maxDepth = Infinity, arrayPolicy = "replace", arrayPolicyByPath } = opts;
|
|
349
|
+
if (nullPolicy !== "clear" && nullPolicy !== "keep-lower") {
|
|
350
|
+
throw new Error(`config: invalid nullPolicy ${JSON.stringify(nullPolicy)}`);
|
|
351
|
+
}
|
|
352
|
+
if (maxDepth !== Infinity && !(Number.isInteger(maxDepth) && maxDepth >= 0)) {
|
|
353
|
+
throw new Error(`config: invalid maxDepth ${String(maxDepth)}: want a non-negative integer or Infinity`);
|
|
354
|
+
}
|
|
355
|
+
if (!ARRAY_POLICIES.includes(arrayPolicy)) {
|
|
356
|
+
throw new Error(`config: invalid arrayPolicy ${JSON.stringify(arrayPolicy)}`);
|
|
357
|
+
}
|
|
358
|
+
const byPath = new Map<string, ArrayPolicy | ArrayMergeFn>();
|
|
359
|
+
const wildcards: Policy["wildcards"] = [];
|
|
360
|
+
for (const [p, v] of Object.entries(arrayPolicyByPath ?? {})) {
|
|
361
|
+
if (typeof v !== "function" && !ARRAY_POLICIES.includes(v)) {
|
|
362
|
+
throw new Error(`config: invalid arrayPolicyByPath[${JSON.stringify(p)}] ${JSON.stringify(v)}`);
|
|
363
|
+
}
|
|
364
|
+
const segs = p.split(".");
|
|
365
|
+
if (segs.includes("**")) {
|
|
366
|
+
throw new Error(`config: invalid arrayPolicyByPath[${JSON.stringify(p)}]: ** is not supported`);
|
|
367
|
+
}
|
|
368
|
+
byPath.set(p, v);
|
|
369
|
+
if (segs.includes("*")) wildcards.push({ pattern: p, segs, rule: v });
|
|
370
|
+
}
|
|
371
|
+
const stars = (segs: string[]): number => segs.filter((x) => x === "*").length;
|
|
372
|
+
wildcards.sort(
|
|
373
|
+
(a, b) => stars(a.segs) - stars(b.segs) || (a.pattern < b.pattern ? -1 : a.pattern > b.pattern ? 1 : 0),
|
|
374
|
+
);
|
|
375
|
+
return { keepLower: nullPolicy === "keep-lower", maxDepth, array: arrayPolicy, byPath, wildcards };
|
|
376
|
+
}
|
|
377
|
+
|
|
189
378
|
/**
|
|
190
379
|
* Collection nesting limit, counted through aliases. js-yaml already rejects
|
|
191
380
|
* text nested deeper than its `maxDepth` (default 100, not in its typings),
|
|
@@ -267,27 +456,85 @@ function checkBounds(root: unknown): void {
|
|
|
267
456
|
}
|
|
268
457
|
|
|
269
458
|
/**
|
|
270
|
-
* Merges `src` into `dst` in place: plain objects
|
|
271
|
-
* replaces, and `null` clears only objects and
|
|
459
|
+
* Merges `src` into `dst` in place. With the default policy: plain objects
|
|
460
|
+
* recurse, everything else replaces, and `null` clears only objects and
|
|
461
|
+
* arrays. `depth` counts object levels below the root; `at` is the dotted
|
|
462
|
+
* path of `dst`.
|
|
272
463
|
*/
|
|
273
|
-
function mergeInto(
|
|
464
|
+
function mergeInto(
|
|
465
|
+
dst: Record<string, unknown>,
|
|
466
|
+
src: Record<string, unknown>,
|
|
467
|
+
policy: Policy,
|
|
468
|
+
depth: number,
|
|
469
|
+
at: string,
|
|
470
|
+
): void {
|
|
274
471
|
for (const key of Object.keys(src)) {
|
|
275
472
|
const next = src[key];
|
|
276
473
|
const has = Object.prototype.hasOwnProperty.call(dst, key);
|
|
277
474
|
const cur = has ? dst[key] : undefined;
|
|
278
475
|
if (next === null) {
|
|
476
|
+
if (policy.keepLower) continue; // null never replaces, never adds
|
|
279
477
|
if (has && cur !== null && cur !== undefined && typeof cur !== "object") {
|
|
280
478
|
continue; // null leaves a scalar untouched
|
|
281
479
|
}
|
|
282
480
|
setOwn(dst, key, null);
|
|
283
|
-
} else if (isPlainObject(next) && isPlainObject(cur)) {
|
|
284
|
-
mergeInto(cur, next);
|
|
481
|
+
} else if (isPlainObject(next) && isPlainObject(cur) && depth < policy.maxDepth) {
|
|
482
|
+
mergeInto(cur, next, policy, depth + 1, at === "" ? key : `${at}.${key}`);
|
|
483
|
+
} else if (Array.isArray(next) && Array.isArray(cur)) {
|
|
484
|
+
setOwn(dst, key, mergeArrays(cur, next, policy, at === "" ? key : `${at}.${key}`));
|
|
285
485
|
} else {
|
|
286
486
|
setOwn(dst, key, copy(next));
|
|
287
487
|
}
|
|
288
488
|
}
|
|
289
489
|
}
|
|
290
490
|
|
|
491
|
+
function mergeArrays(lower: unknown[], higher: unknown[], policy: Policy, at: string): unknown {
|
|
492
|
+
const rule = policy.byPath.get(at) ?? matchWildcard(policy, at) ?? policy.array;
|
|
493
|
+
const items = copy(higher) as unknown[];
|
|
494
|
+
if (typeof rule === "function") return rule(lower, items, at);
|
|
495
|
+
switch (rule) {
|
|
496
|
+
case "concat":
|
|
497
|
+
return [...lower, ...items];
|
|
498
|
+
case "union": {
|
|
499
|
+
const out: unknown[] = [];
|
|
500
|
+
for (const item of [...lower, ...items]) {
|
|
501
|
+
if (!out.some((o) => deepEqual(o, item))) out.push(item);
|
|
502
|
+
}
|
|
503
|
+
return out;
|
|
504
|
+
}
|
|
505
|
+
default:
|
|
506
|
+
return items;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** The best wildcard pattern matching `at`; `*` stands for one whole segment. */
|
|
511
|
+
function matchWildcard(policy: Policy, at: string): ArrayPolicy | ArrayMergeFn | undefined {
|
|
512
|
+
if (policy.wildcards.length === 0) return undefined;
|
|
513
|
+
const segs = at.split(".");
|
|
514
|
+
const hit = policy.wildcards.find(
|
|
515
|
+
(w) => w.segs.length === segs.length && w.segs.every((x, i) => x === "*" || x === segs[i]),
|
|
516
|
+
);
|
|
517
|
+
return hit?.rule;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Structural equality for {@link MergeOptions.arrayPolicy} `"union"`: plain
|
|
522
|
+
* objects (key order ignored) and arrays compare by content, anything else
|
|
523
|
+
* by SameValueZero.
|
|
524
|
+
*/
|
|
525
|
+
function deepEqual(a: unknown, b: unknown): boolean {
|
|
526
|
+
if (a === b || (a !== a && b !== b)) return true;
|
|
527
|
+
if (Array.isArray(a)) {
|
|
528
|
+
return Array.isArray(b) && a.length === b.length && a.every((v, i) => deepEqual(v, b[i]));
|
|
529
|
+
}
|
|
530
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
531
|
+
const ka = Object.keys(a);
|
|
532
|
+
if (ka.length !== Object.keys(b).length) return false;
|
|
533
|
+
return ka.every((k) => Object.prototype.hasOwnProperty.call(b, k) && deepEqual(a[k], b[k]));
|
|
534
|
+
}
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
|
|
291
538
|
/** Own data property even for keys like `__proto__`, never a setter call. */
|
|
292
539
|
function setOwn(dst: Record<string, unknown>, key: string, value: unknown): void {
|
|
293
540
|
Object.defineProperty(dst, key, {
|
package/src/index.test.ts
CHANGED
|
@@ -55,9 +55,10 @@ describe('exports map contract', () => {
|
|
|
55
55
|
|
|
56
56
|
describe('root barrel', () => {
|
|
57
57
|
it('re-exports every public subpath except sqlstore as a namespace', () => {
|
|
58
|
+
// Kebab-case subpaths become camelCase namespaces (completion-values -> completionValues).
|
|
58
59
|
const expected = Object.keys(pkg.exports)
|
|
59
60
|
.filter((p) => p !== '.' && p !== './sqlstore')
|
|
60
|
-
.map((p) => p.replace(/^\.\//, ''));
|
|
61
|
+
.map((p) => p.replace(/^\.\//, '').replace(/-([a-z])/g, (_, c: string) => c.toUpperCase()));
|
|
61
62
|
for (const name of expected) {
|
|
62
63
|
expect(kit, name).toHaveProperty(name);
|
|
63
64
|
expect(typeof (kit as Record<string, unknown>)[name], name).toBe('object');
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Root barrel: namespace re-exports of every public subpath, so
|
|
6
6
|
* `require('@hop-top/kit')` / `import ... from '@hop-top/kit'` resolve.
|
|
7
|
-
* Namespaces mirror the package exports map one-to-one
|
|
7
|
+
* Namespaces mirror the package exports map one-to-one; kebab-case
|
|
8
|
+
* subpaths become camelCase namespaces (`completion-values` ->
|
|
9
|
+
* `completionValues`).
|
|
8
10
|
*
|
|
9
11
|
* `sqlstore` is intentionally NOT re-exported here: an eager re-export
|
|
10
12
|
* would load better-sqlite3 native bindings on every bare import of the
|
|
@@ -18,6 +20,8 @@ export * as api from './api.js';
|
|
|
18
20
|
export * as auth from './auth.js';
|
|
19
21
|
export * as cli from './cli.js';
|
|
20
22
|
export * as completion from './completion.js';
|
|
23
|
+
export * as completionValues from './completion-values.js';
|
|
24
|
+
export * as compliance from './compliance.js';
|
|
21
25
|
export * as config from './config.js';
|
|
22
26
|
export * as errcorrect from './errcorrect.js';
|
|
23
27
|
export * as id from './id/index.js';
|