@hop-top/kit 0.5.0-alpha.5 → 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 +43 -0
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +10 -10
- 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-ZC5TZYZJ.js → chunk-GHPJJV5L.js} +7 -3
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/chunk-NLMS7HGP.js +178 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/{cli-8rfITnv4.d.ts → cli-C5B5tnpA.d.ts} +1 -60
- package/dist/cli.d.ts +2 -1
- package/dist/cli.js +5 -3
- 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 +3 -0
- package/dist/completion.js +34 -0
- 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-DEyjGoo2.d.ts → index-DCe1boBz.d.ts} +1 -1
- package/dist/index.d.ts +10 -4
- package/dist/index.js +40 -20
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/toolspec-UrWnoJ3Y.d.ts +336 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +36 -0
- package/dist/tui/index.d.ts +3 -2
- package/package.json +43 -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 +8 -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-IQXEISMC.js +0 -142
- package/dist/config-Bm9fBXch.d.ts +0 -134
package/src/toolspec.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as yaml from "js-yaml";
|
|
3
|
+
import type {
|
|
4
|
+
Command as CommanderCommand,
|
|
5
|
+
Option as CommanderOption,
|
|
6
|
+
} from "commander";
|
|
3
7
|
|
|
4
8
|
// --- Types ---
|
|
5
9
|
|
|
@@ -47,14 +51,31 @@ export interface Flag {
|
|
|
47
51
|
short?: string;
|
|
48
52
|
type?: string;
|
|
49
53
|
description?: string;
|
|
54
|
+
/** Flag must be supplied (commander `makeOptionMandatory`). */
|
|
55
|
+
required?: boolean;
|
|
50
56
|
deprecated?: boolean;
|
|
51
57
|
replacedBy?: string;
|
|
52
58
|
}
|
|
53
59
|
|
|
60
|
+
/** A declared positional argument (mirrors Go toolspec.Arg). */
|
|
61
|
+
export interface Arg {
|
|
62
|
+
name: string;
|
|
63
|
+
/** False/absent for an optional argument. */
|
|
64
|
+
required?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
54
67
|
export interface Command {
|
|
55
68
|
name: string;
|
|
69
|
+
/** One-line summary of the command. */
|
|
70
|
+
short?: string;
|
|
56
71
|
aliases?: string[];
|
|
57
72
|
flags?: Flag[];
|
|
73
|
+
/** Declared positional arguments, in order. */
|
|
74
|
+
args?: Arg[];
|
|
75
|
+
/** Command takes positional arguments it does not declare. */
|
|
76
|
+
undeclaredArgs?: boolean;
|
|
77
|
+
/** Exit-code symbols the command may produce (kit/exit-codes). */
|
|
78
|
+
exitCodes?: string[];
|
|
58
79
|
children?: Command[];
|
|
59
80
|
contract?: Contract;
|
|
60
81
|
safety?: Safety;
|
|
@@ -103,11 +124,16 @@ function mapFlag(raw: any): Flag {
|
|
|
103
124
|
short: raw.short,
|
|
104
125
|
type: raw.type,
|
|
105
126
|
description: raw.description,
|
|
127
|
+
required: raw.required,
|
|
106
128
|
deprecated: raw.deprecated,
|
|
107
129
|
replacedBy: raw.replaced_by,
|
|
108
130
|
};
|
|
109
131
|
}
|
|
110
132
|
|
|
133
|
+
function mapArg(raw: any): Arg {
|
|
134
|
+
return { name: raw.name ?? "", required: raw.required };
|
|
135
|
+
}
|
|
136
|
+
|
|
111
137
|
function mapProvenance(raw: any): Provenance | undefined {
|
|
112
138
|
if (!raw) return undefined;
|
|
113
139
|
return {
|
|
@@ -157,8 +183,12 @@ function mapIntent(raw: any): Intent | undefined {
|
|
|
157
183
|
function mapCommand(raw: any): Command {
|
|
158
184
|
return {
|
|
159
185
|
name: raw.name ?? "",
|
|
186
|
+
short: raw.short,
|
|
160
187
|
aliases: raw.aliases,
|
|
161
188
|
flags: raw.flags?.map(mapFlag),
|
|
189
|
+
args: raw.args?.map(mapArg),
|
|
190
|
+
undeclaredArgs: raw.undeclared_args,
|
|
191
|
+
exitCodes: raw.exit_codes,
|
|
162
192
|
children: raw.children?.map(mapCommand),
|
|
163
193
|
contract: mapContract(raw.contract),
|
|
164
194
|
safety: mapSafety(raw.safety),
|
|
@@ -250,6 +280,9 @@ export function validateToolSpec(spec: ToolSpec): string[] {
|
|
|
250
280
|
` (must be safe|caution|dangerous)`,
|
|
251
281
|
);
|
|
252
282
|
}
|
|
283
|
+
if (cmd.exitCodes?.some((c) => typeof c !== "string" || c.trim() === "")) {
|
|
284
|
+
errors.push(`${path}: exit code symbols must be non-empty strings`);
|
|
285
|
+
}
|
|
253
286
|
if (cmd.children) {
|
|
254
287
|
for (const child of cmd.children) {
|
|
255
288
|
validateCmd(child, `${path}/${child.name || "<unnamed>"}`);
|
|
@@ -277,3 +310,129 @@ export function findCommand(
|
|
|
277
310
|
}
|
|
278
311
|
return undefined;
|
|
279
312
|
}
|
|
313
|
+
|
|
314
|
+
export * from "./toolspec-manifest.js";
|
|
315
|
+
export * from "./toolspec-spec.js";
|
|
316
|
+
|
|
317
|
+
// --- Build from a commander tree ---
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Per-command fields merged over what the commander tree yields.
|
|
321
|
+
* Structural fields (name, aliases, flags, args, children) always
|
|
322
|
+
* come from the tree.
|
|
323
|
+
*/
|
|
324
|
+
export type CommandAnnotation = Omit<
|
|
325
|
+
Partial<Command>,
|
|
326
|
+
"name" | "aliases" | "flags" | "args" | "children"
|
|
327
|
+
>;
|
|
328
|
+
|
|
329
|
+
export interface FromCommandOptions {
|
|
330
|
+
/** Annotations keyed by space-joined path below the root, e.g. "mission list". */
|
|
331
|
+
annotations?: Record<string, CommandAnnotation>;
|
|
332
|
+
/** Defaults to "1", the version the YAML toolspecs declare. */
|
|
333
|
+
schemaVersion?: string;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Build a ToolSpec from a constructed commander tree, so CLIs with
|
|
338
|
+
* dynamically registered subcommands need no hand-synced YAML.
|
|
339
|
+
* Mirrors Go's WalkCobra projection: hidden commands and options are
|
|
340
|
+
* omitted, flag names are undashed, root options become spec.flags.
|
|
341
|
+
*/
|
|
342
|
+
export function toolSpecFromCommand(
|
|
343
|
+
program: CommanderCommand,
|
|
344
|
+
opts: FromCommandOptions = {},
|
|
345
|
+
): ToolSpec {
|
|
346
|
+
const annotations = opts.annotations ?? {};
|
|
347
|
+
const spec: ToolSpec = {
|
|
348
|
+
name: program.name(),
|
|
349
|
+
schemaVersion: opts.schemaVersion ?? "1",
|
|
350
|
+
commands: projectChildren(program, [], annotations),
|
|
351
|
+
};
|
|
352
|
+
const flags = projectFlags(program);
|
|
353
|
+
if (flags) spec.flags = flags;
|
|
354
|
+
return spec;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function projectChildren(
|
|
358
|
+
cmd: CommanderCommand,
|
|
359
|
+
path: string[],
|
|
360
|
+
annotations: Record<string, CommandAnnotation>,
|
|
361
|
+
): Command[] {
|
|
362
|
+
// visibleCommands drops hidden subcommands but appends commander's
|
|
363
|
+
// implicit help placeholder, which is not in cmd.commands.
|
|
364
|
+
const visible = cmd.createHelp().visibleCommands(cmd);
|
|
365
|
+
return visible
|
|
366
|
+
.filter((c) => cmd.commands.includes(c))
|
|
367
|
+
.filter((c) => kitAnnotations(c)["kit/spec-command"] !== "true")
|
|
368
|
+
.map((c) => projectCommand(c, [...path, c.name()], annotations));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function projectCommand(
|
|
372
|
+
cmd: CommanderCommand,
|
|
373
|
+
path: string[],
|
|
374
|
+
annotations: Record<string, CommandAnnotation>,
|
|
375
|
+
): Command {
|
|
376
|
+
const out: Command = { name: cmd.name() };
|
|
377
|
+
const short = cmd.summary() || cmd.description();
|
|
378
|
+
if (short) out.short = short;
|
|
379
|
+
const aliases = cmd.aliases();
|
|
380
|
+
if (aliases.length > 0) out.aliases = [...aliases];
|
|
381
|
+
const flags = projectFlags(cmd);
|
|
382
|
+
if (flags) out.flags = flags;
|
|
383
|
+
const args = cmd.registeredArguments.map((a): Arg =>
|
|
384
|
+
a.required ? { name: a.name(), required: true } : { name: a.name() },
|
|
385
|
+
);
|
|
386
|
+
if (args.length > 0) out.args = args;
|
|
387
|
+
const exitCodes = splitCSV(kitAnnotations(cmd)["kit/exit-codes"]);
|
|
388
|
+
if (exitCodes) out.exitCodes = exitCodes;
|
|
389
|
+
const children = projectChildren(cmd, path, annotations);
|
|
390
|
+
if (children.length > 0) out.children = children;
|
|
391
|
+
|
|
392
|
+
const ann = annotations[path.join(" ")];
|
|
393
|
+
if (ann) {
|
|
394
|
+
for (const [k, v] of Object.entries(ann)) {
|
|
395
|
+
if (v !== undefined) (out as any)[k] = v;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return out;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* kit/* annotations on a commander command. commander has no
|
|
403
|
+
* annotations concept; they ride an optional `annotations` property,
|
|
404
|
+
* the same convention the MCP bridge reads.
|
|
405
|
+
*/
|
|
406
|
+
function kitAnnotations(cmd: CommanderCommand): Record<string, string> {
|
|
407
|
+
return (cmd as { annotations?: Record<string, string> }).annotations ?? {};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Comma-separated annotation value, trimmed, empties dropped. */
|
|
411
|
+
function splitCSV(s: string | undefined): string[] | undefined {
|
|
412
|
+
if (!s) return undefined;
|
|
413
|
+
const out = s.split(",").map((p) => p.trim()).filter((p) => p !== "");
|
|
414
|
+
return out.length > 0 ? out : undefined;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function projectFlags(cmd: CommanderCommand): Flag[] | undefined {
|
|
418
|
+
const out: Flag[] = [];
|
|
419
|
+
for (const opt of cmd.options as readonly CommanderOption[]) {
|
|
420
|
+
if (opt.hidden) continue;
|
|
421
|
+
const name = (opt.long ?? opt.short ?? "").replace(/^-+/, "");
|
|
422
|
+
if (!name) continue;
|
|
423
|
+
const flag: Flag = { name };
|
|
424
|
+
if (opt.long && opt.short) flag.short = opt.short.replace(/^-/, "");
|
|
425
|
+
flag.type = flagType(opt);
|
|
426
|
+
if (opt.description) flag.description = opt.description;
|
|
427
|
+
if (opt.mandatory) flag.required = true;
|
|
428
|
+
out.push(flag);
|
|
429
|
+
}
|
|
430
|
+
return out.length > 0 ? out : undefined;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** pflag-style type names, matching the Go walker's output. */
|
|
434
|
+
function flagType(opt: CommanderOption): string {
|
|
435
|
+
if (opt.isBoolean()) return "bool";
|
|
436
|
+
if (opt.variadic) return "stringSlice";
|
|
437
|
+
return "string";
|
|
438
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the shared cross-language toolspec fixture tree
|
|
3
|
+
* (sdk/tests/cross-lang/fixtures/toolspec-manifest.tree.json) as a
|
|
4
|
+
* commander program, mirroring the Go cobra builder in
|
|
5
|
+
* go/ai/toolspec/cli/manifest_crosslang_test.go.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from "fs";
|
|
8
|
+
import * as path from "path";
|
|
9
|
+
import { Command, Option } from "commander";
|
|
10
|
+
|
|
11
|
+
export const FIXTURES = path.resolve(__dirname, "../../../tests/cross-lang/fixtures");
|
|
12
|
+
export const CONTRACTS = path.resolve(__dirname, "../../../../contracts");
|
|
13
|
+
|
|
14
|
+
interface TreeFlag {
|
|
15
|
+
name: string;
|
|
16
|
+
short?: string;
|
|
17
|
+
type: "bool" | "string" | "stringSlice";
|
|
18
|
+
default?: string | string[];
|
|
19
|
+
description?: string;
|
|
20
|
+
hidden?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface TreeCommand {
|
|
24
|
+
name: string;
|
|
25
|
+
short?: string;
|
|
26
|
+
long?: string;
|
|
27
|
+
hidden?: boolean;
|
|
28
|
+
run?: boolean;
|
|
29
|
+
annotations?: Record<string, string>;
|
|
30
|
+
flags?: TreeFlag[];
|
|
31
|
+
commands?: TreeCommand[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Tree {
|
|
35
|
+
tool: string;
|
|
36
|
+
version: string;
|
|
37
|
+
reserved: string[];
|
|
38
|
+
commands: TreeCommand[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function addFixtureCommand(parent: Command, tc: TreeCommand): void {
|
|
42
|
+
const cmd = parent.command(tc.name, { hidden: tc.hidden ?? false });
|
|
43
|
+
if (tc.short && tc.long) cmd.summary(tc.short).description(tc.long);
|
|
44
|
+
else if (tc.short) cmd.description(tc.short);
|
|
45
|
+
if (tc.run) cmd.action(() => {});
|
|
46
|
+
if (tc.annotations) {
|
|
47
|
+
(cmd as Command & { annotations?: Record<string, string> }).annotations = {
|
|
48
|
+
...tc.annotations,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
for (const f of tc.flags ?? []) {
|
|
52
|
+
const short = f.short ? `-${f.short}, ` : "";
|
|
53
|
+
const arg =
|
|
54
|
+
f.type === "bool" ? "" : f.type === "string" ? " <value>" : " <values...>";
|
|
55
|
+
const opt = new Option(`${short}--${f.name}${arg}`, f.description);
|
|
56
|
+
if (f.default !== undefined) opt.default(f.default);
|
|
57
|
+
if (f.hidden) opt.hideHelp();
|
|
58
|
+
cmd.addOption(opt);
|
|
59
|
+
}
|
|
60
|
+
for (const child of tc.commands ?? []) addFixtureCommand(cmd, child);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The fixture program, with createCLI's `-v, --version` on the root.
|
|
65
|
+
*/
|
|
66
|
+
export function fixtureProgram(): { program: Command; tree: Tree } {
|
|
67
|
+
const tree = JSON.parse(
|
|
68
|
+
fs.readFileSync(path.join(FIXTURES, "toolspec-manifest.tree.json"), "utf-8"),
|
|
69
|
+
) as Tree;
|
|
70
|
+
const program = new Command(tree.tool).version(tree.version, "-v, --version");
|
|
71
|
+
for (const c of tree.commands) addFixtureCommand(program, c);
|
|
72
|
+
return { program, tree };
|
|
73
|
+
}
|
package/dist/chunk-IQXEISMC.js
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
|
|
2
|
-
|
|
3
|
-
var _chunkR5U7XKVJjs = require('./chunk-R5U7XKVJ.js');
|
|
4
|
-
|
|
5
|
-
// src/config.ts
|
|
6
|
-
var config_exports = {};
|
|
7
|
-
_chunkR5U7XKVJjs.__export.call(void 0, config_exports, {
|
|
8
|
-
load: () => load2
|
|
9
|
-
});
|
|
10
|
-
var _fs = require('fs'); var fs = _interopRequireWildcard(_fs);
|
|
11
|
-
var _jsyaml = require('js-yaml'); var yaml = _interopRequireWildcard(_jsyaml);
|
|
12
|
-
function load2(dst, opts = {}) {
|
|
13
|
-
const paths = [
|
|
14
|
-
opts.systemConfigPath,
|
|
15
|
-
opts.userConfigPath,
|
|
16
|
-
opts.projectConfigPath
|
|
17
|
-
];
|
|
18
|
-
for (const p of paths) {
|
|
19
|
-
if (!p) continue;
|
|
20
|
-
mergeFile(dst, p);
|
|
21
|
-
}
|
|
22
|
-
if (opts.envOverride) {
|
|
23
|
-
opts.envOverride(dst);
|
|
24
|
-
}
|
|
25
|
-
return dst;
|
|
26
|
-
}
|
|
27
|
-
function mergeFile(dst, filePath) {
|
|
28
|
-
let raw;
|
|
29
|
-
try {
|
|
30
|
-
raw = fs.readFileSync(filePath, "utf8");
|
|
31
|
-
} catch (err) {
|
|
32
|
-
if (isEnoent(err)) return;
|
|
33
|
-
throw err;
|
|
34
|
-
}
|
|
35
|
-
let parsed;
|
|
36
|
-
try {
|
|
37
|
-
parsed = yaml.load(raw);
|
|
38
|
-
checkBounds(parsed);
|
|
39
|
-
} catch (err) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
`config: failed to parse ${filePath}: ${err instanceof Error ? err.message : String(err)}`
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
if (isPlainObject(parsed)) {
|
|
45
|
-
mergeInto(dst, parsed);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
var MAX_DEPTH = 100;
|
|
49
|
-
var ALIAS_MIN_ALIASES = 100;
|
|
50
|
-
var ALIAS_MIN_DECODED = 1e3;
|
|
51
|
-
var ALIAS_RATIO_LOW = 4e5;
|
|
52
|
-
var ALIAS_RATIO_HIGH = 4e6;
|
|
53
|
-
function allowedAliasRatio(decoded) {
|
|
54
|
-
if (decoded <= ALIAS_RATIO_LOW) return 0.99;
|
|
55
|
-
if (decoded >= ALIAS_RATIO_HIGH) return 0.1;
|
|
56
|
-
return 0.99 - 0.89 * ((decoded - ALIAS_RATIO_LOW) / (ALIAS_RATIO_HIGH - ALIAS_RATIO_LOW));
|
|
57
|
-
}
|
|
58
|
-
function checkBounds(root) {
|
|
59
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
60
|
-
const stack = [[root, 1, false]];
|
|
61
|
-
let decoded = 0;
|
|
62
|
-
let aliased = 0;
|
|
63
|
-
while (stack.length > 0) {
|
|
64
|
-
const [node, depth, inAlias] = stack.pop();
|
|
65
|
-
let alias = inAlias;
|
|
66
|
-
const container = Array.isArray(node) || isPlainObject(node);
|
|
67
|
-
if (container) {
|
|
68
|
-
if (seen.has(node)) {
|
|
69
|
-
decoded++;
|
|
70
|
-
alias = true;
|
|
71
|
-
} else {
|
|
72
|
-
seen.add(node);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
decoded++;
|
|
76
|
-
if (alias) aliased++;
|
|
77
|
-
if (aliased > ALIAS_MIN_ALIASES && decoded > ALIAS_MIN_DECODED && aliased / decoded > allowedAliasRatio(decoded)) {
|
|
78
|
-
throw new Error("document contains excessive aliasing");
|
|
79
|
-
}
|
|
80
|
-
if (!container) continue;
|
|
81
|
-
if (depth > MAX_DEPTH) {
|
|
82
|
-
throw new Error(`nesting exceeded maxDepth (${MAX_DEPTH})`);
|
|
83
|
-
}
|
|
84
|
-
if (Array.isArray(node)) {
|
|
85
|
-
for (let i = node.length - 1; i >= 0; i--) stack.push([node[i], depth + 1, alias]);
|
|
86
|
-
} else {
|
|
87
|
-
const keys = Object.keys(node);
|
|
88
|
-
for (let i = keys.length - 1; i >= 0; i--) {
|
|
89
|
-
decoded++;
|
|
90
|
-
if (alias) aliased++;
|
|
91
|
-
stack.push([node[keys[i]], depth + 1, alias]);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
function mergeInto(dst, src) {
|
|
97
|
-
for (const key of Object.keys(src)) {
|
|
98
|
-
const next = src[key];
|
|
99
|
-
const has = Object.prototype.hasOwnProperty.call(dst, key);
|
|
100
|
-
const cur = has ? dst[key] : void 0;
|
|
101
|
-
if (next === null) {
|
|
102
|
-
if (has && cur !== null && cur !== void 0 && typeof cur !== "object") {
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
105
|
-
setOwn(dst, key, null);
|
|
106
|
-
} else if (isPlainObject(next) && isPlainObject(cur)) {
|
|
107
|
-
mergeInto(cur, next);
|
|
108
|
-
} else {
|
|
109
|
-
setOwn(dst, key, copy(next));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
function setOwn(dst, key, value) {
|
|
114
|
-
Object.defineProperty(dst, key, {
|
|
115
|
-
value,
|
|
116
|
-
writable: true,
|
|
117
|
-
enumerable: true,
|
|
118
|
-
configurable: true
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
function copy(v) {
|
|
122
|
-
if (Array.isArray(v)) return v.map(copy);
|
|
123
|
-
if (isPlainObject(v)) {
|
|
124
|
-
const out = {};
|
|
125
|
-
for (const k of Object.keys(v)) setOwn(out, k, copy(v[k]));
|
|
126
|
-
return out;
|
|
127
|
-
}
|
|
128
|
-
return v;
|
|
129
|
-
}
|
|
130
|
-
function isPlainObject(v) {
|
|
131
|
-
if (v === null || typeof v !== "object" || Array.isArray(v)) return false;
|
|
132
|
-
const proto = Object.getPrototypeOf(v);
|
|
133
|
-
return proto === Object.prototype || proto === null;
|
|
134
|
-
}
|
|
135
|
-
function isEnoent(err) {
|
|
136
|
-
return typeof err === "object" && err !== null && "code" in err && err.code === "ENOENT";
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
exports.load = load2; exports.config_exports = config_exports;
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @packageDocumentation
|
|
3
|
-
*
|
|
4
|
-
* Layered YAML configuration loader — TypeScript port of hop.top/kit/config.
|
|
5
|
-
*
|
|
6
|
-
* Merges configuration from up to three file layers in priority order:
|
|
7
|
-
* system → user → project → envOverride
|
|
8
|
-
*
|
|
9
|
-
* Each layer merges into `dst` with these rules:
|
|
10
|
-
*
|
|
11
|
-
* - Plain objects merge recursively, key by key, so a later layer only
|
|
12
|
-
* overrides the keys it sets (`db: {port: 2}` keeps an earlier `db.host`).
|
|
13
|
-
* - Arrays and scalars are replaced wholesale.
|
|
14
|
-
* - `null` clears an object or array; it leaves a scalar untouched.
|
|
15
|
-
* - On a type conflict (object vs non-object) the later value replaces the
|
|
16
|
-
* earlier one.
|
|
17
|
-
* - Non-plain objects (`Date`, `Map`, class instances) are never merged
|
|
18
|
-
* into; a later value replaces them.
|
|
19
|
-
*
|
|
20
|
-
* This matches Go's `kit/core/config` for struct fields. Go differs where its
|
|
21
|
-
* target type differs: a `map[...]` value or an `any` field is replaced
|
|
22
|
-
* whole rather than merged, and a type conflict on a typed field is a decode
|
|
23
|
-
* error rather than a replacement.
|
|
24
|
-
*
|
|
25
|
-
* Missing files are silently skipped. A file that exists but contains invalid
|
|
26
|
-
* YAML causes {@link load} to throw. A document whose root is not a mapping
|
|
27
|
-
* is ignored.
|
|
28
|
-
*
|
|
29
|
-
* Each file is checked before it merges, with aliases expanded, and
|
|
30
|
-
* {@link load} throws `config: failed to parse <path>: <reason>` when:
|
|
31
|
-
*
|
|
32
|
-
* - its aliases expand past Go yaml.v3's budget: more than 100 nodes inside
|
|
33
|
-
* alias expansions and more than 1000 in total, with the share inside
|
|
34
|
-
* aliases above 0.99 (up to 400k nodes), falling linearly to 0.10 at 4M
|
|
35
|
-
* (`document contains excessive aliasing`). Go counts only the keys its
|
|
36
|
-
* target struct reads; TS has no target, so it counts every key, and a
|
|
37
|
-
* file Go accepts because the aliases sit under ignored keys fails here.
|
|
38
|
-
* - collections nest deeper than 100, counted through aliases
|
|
39
|
-
* (`nesting exceeded maxDepth (100)`). Go allows 10000 levels of text
|
|
40
|
-
* nesting and caps alias chains only through the budget above.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* import { load } from "@hop-top/kit/config";
|
|
45
|
-
*
|
|
46
|
-
* interface AppConfig {
|
|
47
|
-
* host: string;
|
|
48
|
-
* port: number;
|
|
49
|
-
* debug: boolean;
|
|
50
|
-
* token: string;
|
|
51
|
-
* }
|
|
52
|
-
*
|
|
53
|
-
* const cfg: AppConfig = { host: "localhost", port: 3000, debug: false, token: "" };
|
|
54
|
-
*
|
|
55
|
-
* load(cfg, {
|
|
56
|
-
* systemConfigPath: "/etc/mytool/config.yaml",
|
|
57
|
-
* userConfigPath: `${process.env.HOME}/.config/mytool/config.yaml`,
|
|
58
|
-
* projectConfigPath: ".mytool.yaml",
|
|
59
|
-
* envOverride(c) {
|
|
60
|
-
* if (process.env.HOST) c.host = process.env.HOST;
|
|
61
|
-
* if (process.env.PORT) c.port = Number(process.env.PORT);
|
|
62
|
-
* if (process.env.TOKEN) c.token = process.env.TOKEN;
|
|
63
|
-
* },
|
|
64
|
-
* });
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
/**
|
|
68
|
-
* Configuration sources for {@link load}.
|
|
69
|
-
*
|
|
70
|
-
* All path fields are optional; an empty or omitted path is silently skipped.
|
|
71
|
-
* {@link Options.envOverride} is called last, after all file layers, so it can
|
|
72
|
-
* apply environment-variable overrides on top of file values.
|
|
73
|
-
*/
|
|
74
|
-
interface Options<T extends object = object> {
|
|
75
|
-
/**
|
|
76
|
-
* Path to the system-wide config file, e.g. `/etc/mytool/config.yaml`.
|
|
77
|
-
* Applied first (lowest priority).
|
|
78
|
-
*/
|
|
79
|
-
systemConfigPath?: string;
|
|
80
|
-
/**
|
|
81
|
-
* Path to the per-user config file, e.g. `~/.config/mytool/config.yaml`.
|
|
82
|
-
* Applied second; merges over system values (see the module merge rules).
|
|
83
|
-
*/
|
|
84
|
-
userConfigPath?: string;
|
|
85
|
-
/**
|
|
86
|
-
* Path to the project-level config file, e.g. `.mytool.yaml`.
|
|
87
|
-
* Applied third; merges over system and user values.
|
|
88
|
-
*/
|
|
89
|
-
projectConfigPath?: string;
|
|
90
|
-
/**
|
|
91
|
-
* Optional callback invoked after all file layers have been merged.
|
|
92
|
-
* Receives the fully-merged `dst` object so the caller can apply
|
|
93
|
-
* environment-variable overrides last (highest priority).
|
|
94
|
-
*
|
|
95
|
-
* @param cfg - The merged config object (same reference as `dst`).
|
|
96
|
-
*/
|
|
97
|
-
envOverride?: (cfg: T) => void;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Merges layered YAML configuration into `dst` and returns it.
|
|
101
|
-
*
|
|
102
|
-
* Merge order: `systemConfigPath` → `userConfigPath` → `projectConfigPath` →
|
|
103
|
-
* `envOverride`.
|
|
104
|
-
*
|
|
105
|
-
* - Objects merge recursively; arrays and scalars replace; `null` clears
|
|
106
|
-
* objects and arrays but not scalars (see the module docs for details).
|
|
107
|
-
* - Missing files (ENOENT) are silently skipped.
|
|
108
|
-
* - A file that exists but is not valid YAML causes this function to throw
|
|
109
|
-
* a descriptive error wrapping the parse failure.
|
|
110
|
-
* - `envOverride`, when provided, is called last with the merged `dst`.
|
|
111
|
-
* - Returns `dst` (the same object reference passed in).
|
|
112
|
-
*
|
|
113
|
-
* @param dst - Destination object to merge config into. Must be a non-null
|
|
114
|
-
* object; mutated in place.
|
|
115
|
-
* @param opts - Config sources. All fields are optional.
|
|
116
|
-
* @returns The mutated `dst` object.
|
|
117
|
-
* @throws {Error} When a file exists but cannot be parsed as YAML.
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* ```ts
|
|
121
|
-
* const cfg = { host: "localhost", port: 8080 };
|
|
122
|
-
* load(cfg, { projectConfigPath: ".tool.yaml" });
|
|
123
|
-
* console.log(cfg.host); // value from .tool.yaml, or "localhost" if file missing
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
declare function load<T extends object>(dst: T, opts?: Options<T>): T;
|
|
127
|
-
|
|
128
|
-
type config_Options<T extends object = object> = Options<T>;
|
|
129
|
-
declare const config_load: typeof load;
|
|
130
|
-
declare namespace config {
|
|
131
|
-
export { type config_Options as Options, config_load as load };
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export { type Options as O, config as c, load as l };
|