@kjanat/dreamcli 2.5.0 → 3.0.0-rc.2

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.
Files changed (160) hide show
  1. package/CHANGELOG.md +153 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.mjs +214 -1
  4. package/dist/core/cli/help-links.mjs +41 -1
  5. package/dist/core/cli/index.d.mts +109 -16
  6. package/dist/core/cli/index.mjs +901 -1
  7. package/dist/core/cli/planner.d.mts +27 -5
  8. package/dist/core/cli/planner.mjs +300 -1
  9. package/dist/core/cli/plugin.mjs +41 -1
  10. package/dist/core/cli/propagate.mjs +57 -1
  11. package/dist/core/cli/root-help.d.mts +16 -2
  12. package/dist/core/cli/root-help.mjs +208 -6
  13. package/dist/core/cli/root-surface.d.mts +18 -3
  14. package/dist/core/cli/root-surface.mjs +27 -1
  15. package/dist/core/cli/runtime-preflight.d.mts +16 -1
  16. package/dist/core/cli/runtime-preflight.mjs +168 -1
  17. package/dist/core/completion/index.d.mts +32 -1
  18. package/dist/core/completion/index.mjs +90 -1
  19. package/dist/core/completion/shells/bash.mjs +304 -3
  20. package/dist/core/completion/shells/fish.mjs +159 -2
  21. package/dist/core/completion/shells/powershell.mjs +353 -2
  22. package/dist/core/completion/shells/shared.d.mts +10 -0
  23. package/dist/core/completion/shells/shared.mjs +154 -1
  24. package/dist/core/completion/shells/zsh.mjs +244 -2
  25. package/dist/core/config/index.mjs +246 -1
  26. package/dist/core/config/package-json.mjs +385 -3
  27. package/dist/core/errors/index.mjs +87 -1
  28. package/dist/core/execution/index.mjs +189 -1
  29. package/dist/core/help/ansi.mjs +122 -2
  30. package/dist/core/help/index.d.mts +45 -1
  31. package/dist/core/help/index.mjs +329 -7
  32. package/dist/core/json-schema/index.mjs +487 -9
  33. package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
  34. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  35. package/dist/core/output/activity.mjs +452 -1
  36. package/dist/core/output/bind.mjs +56 -1
  37. package/dist/core/output/contracts.mjs +57 -1
  38. package/dist/core/output/display-value.mjs +32 -1
  39. package/dist/core/output/index.d.mts +21 -0
  40. package/dist/core/output/index.mjs +442 -2
  41. package/dist/core/output/renderers.mjs +46 -1
  42. package/dist/core/output/writer.mjs +12 -1
  43. package/dist/core/parse/index.d.mts +15 -1
  44. package/dist/core/parse/index.mjs +555 -1
  45. package/dist/core/prompt/index.d.mts +11 -4
  46. package/dist/core/prompt/index.mjs +348 -6
  47. package/dist/core/resolve/args.mjs +105 -1
  48. package/dist/core/resolve/coerce.mjs +279 -1
  49. package/dist/core/resolve/config.mjs +19 -1
  50. package/dist/core/resolve/errors.mjs +140 -2
  51. package/dist/core/resolve/flags.mjs +196 -1
  52. package/dist/core/resolve/index.mjs +85 -1
  53. package/dist/core/resolve/property.d.mts +2 -1
  54. package/dist/core/resolve/property.mjs +45 -1
  55. package/dist/core/schema/arg.d.mts +90 -10
  56. package/dist/core/schema/arg.mjs +463 -1
  57. package/dist/core/schema/command.d.mts +21 -1
  58. package/dist/core/schema/command.mjs +712 -1
  59. package/dist/core/schema/flag.d.mts +92 -2
  60. package/dist/core/schema/flag.mjs +453 -1
  61. package/dist/core/schema/index.d.mts +2 -1
  62. package/dist/core/schema/middleware.mjs +41 -1
  63. package/dist/core/schema/number-constraints.d.mts +96 -0
  64. package/dist/core/schema/number-constraints.mjs +60 -0
  65. package/dist/core/schema/prompt.d.mts +15 -0
  66. package/dist/core/schema/run.d.mts +4 -4
  67. package/dist/core/schema-dsl/runtime.mjs +271 -2
  68. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  69. package/dist/core/testkit/index.d.mts +9 -5
  70. package/dist/core/testkit/index.mjs +83 -1
  71. package/dist/index.d.mts +3 -1
  72. package/dist/index.mjs +22 -1
  73. package/dist/runtime/adapter.mjs +82 -1
  74. package/dist/runtime/auto.mjs +46 -1
  75. package/dist/runtime/bun.mjs +28 -1
  76. package/dist/runtime/deno.mjs +217 -2
  77. package/dist/runtime/detect.mjs +45 -1
  78. package/dist/runtime/node.mjs +169 -2
  79. package/dist/runtime/paths.mjs +48 -1
  80. package/dist/runtime.mjs +7 -1
  81. package/dist/strings.mjs +22 -1
  82. package/dist/testkit.mjs +5 -1
  83. package/dist/version.mjs +7 -1
  84. package/examples/basic.ts +5 -1
  85. package/examples/gh/package.json +2 -7
  86. package/examples/pwsh-demo/package.json +1 -7
  87. package/examples/pwsh-demo/src/main.test.ts +1 -4
  88. package/examples/pwsh-demo/src/main.ts +14 -4
  89. package/examples/transport-launcher.ts +85 -0
  90. package/package.json +23 -41
  91. package/dist/runtime/support.d.mts +0 -42
  92. package/dist/runtime/support.mjs +0 -1
  93. package/src/core/cli/dispatch.ts +0 -349
  94. package/src/core/cli/help-links.ts +0 -82
  95. package/src/core/cli/index.ts +0 -1413
  96. package/src/core/cli/planner.ts +0 -487
  97. package/src/core/cli/plugin.ts +0 -117
  98. package/src/core/cli/propagate.ts +0 -97
  99. package/src/core/cli/root-help.ts +0 -252
  100. package/src/core/cli/root-surface.ts +0 -74
  101. package/src/core/cli/runtime-preflight.ts +0 -438
  102. package/src/core/completion/index.ts +0 -88
  103. package/src/core/completion/shells/bash.ts +0 -459
  104. package/src/core/completion/shells/fish.ts +0 -259
  105. package/src/core/completion/shells/powershell.ts +0 -439
  106. package/src/core/completion/shells/shared.ts +0 -283
  107. package/src/core/completion/shells/zsh.ts +0 -381
  108. package/src/core/config/index.ts +0 -417
  109. package/src/core/config/package-json.ts +0 -560
  110. package/src/core/errors/index.ts +0 -193
  111. package/src/core/execution/index.ts +0 -268
  112. package/src/core/help/ansi.ts +0 -144
  113. package/src/core/help/index.ts +0 -516
  114. package/src/core/json-schema/index.ts +0 -891
  115. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  116. package/src/core/output/activity.ts +0 -575
  117. package/src/core/output/bind.ts +0 -59
  118. package/src/core/output/contracts.ts +0 -194
  119. package/src/core/output/display-value.ts +0 -48
  120. package/src/core/output/index.ts +0 -704
  121. package/src/core/output/renderers.ts +0 -125
  122. package/src/core/output/writer.ts +0 -46
  123. package/src/core/parse/index.ts +0 -681
  124. package/src/core/prompt/index.ts +0 -583
  125. package/src/core/resolve/args.ts +0 -144
  126. package/src/core/resolve/coerce.ts +0 -432
  127. package/src/core/resolve/config.ts +0 -26
  128. package/src/core/resolve/contracts.ts +0 -135
  129. package/src/core/resolve/errors.ts +0 -227
  130. package/src/core/resolve/flags.ts +0 -255
  131. package/src/core/resolve/index.ts +0 -108
  132. package/src/core/resolve/property.ts +0 -99
  133. package/src/core/schema/activity.ts +0 -240
  134. package/src/core/schema/arg.ts +0 -719
  135. package/src/core/schema/command.ts +0 -1498
  136. package/src/core/schema/flag.ts +0 -801
  137. package/src/core/schema/index.ts +0 -91
  138. package/src/core/schema/middleware.ts +0 -171
  139. package/src/core/schema/prompt.ts +0 -156
  140. package/src/core/schema/run.ts +0 -198
  141. package/src/core/schema-dsl/index.ts +0 -109
  142. package/src/core/schema-dsl/parse.ts +0 -262
  143. package/src/core/schema-dsl/runtime.ts +0 -609
  144. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  145. package/src/core/testkit/index.ts +0 -123
  146. package/src/index.ts +0 -179
  147. package/src/runtime/adapter.ts +0 -303
  148. package/src/runtime/auto.ts +0 -110
  149. package/src/runtime/bun.ts +0 -55
  150. package/src/runtime/deno.ts +0 -369
  151. package/src/runtime/detect.ts +0 -105
  152. package/src/runtime/index.ts +0 -21
  153. package/src/runtime/node-builtins.d.ts +0 -27
  154. package/src/runtime/node.ts +0 -300
  155. package/src/runtime/paths.ts +0 -64
  156. package/src/runtime/support.ts +0 -135
  157. package/src/runtime.ts +0 -31
  158. package/src/strings.ts +0 -34
  159. package/src/testkit.ts +0 -25
  160. package/src/version.ts +0 -20
@@ -1,349 +0,0 @@
1
- /**
2
- * Recursive command dispatch for nested command trees.
3
- *
4
- * Walks argv segments, matching command names at each level of the tree.
5
- * Returns a discriminated union describing the dispatch outcome: a matched
6
- * command with its ancestor path, a group that needs a subcommand, or an
7
- * unknown command with suggestion candidates.
8
- *
9
- * @module dreamcli/core/cli/dispatch
10
- * @internal
11
- */
12
-
13
- import { buildFlagLookup, flagExpectsValue } from '#internals/core/parse/index.ts';
14
- import type { CommandSchema, ErasedCommand } from '#internals/core/schema/command.ts';
15
- import type { FlagSchema } from '#internals/core/schema/flag.ts';
16
-
17
- /**
18
- * Flag lookup (name/alias → `[canonicalName, schema]`) used to make the
19
- * command-name scan aware of value-flag arity. Built from the flags valid at
20
- * the current dispatch level via {@link buildFlagLookup}.
21
- *
22
- * @internal
23
- */
24
- type ValueFlagLookup = ReadonlyMap<string, readonly [name: string, schema: FlagSchema]>;
25
-
26
- // --- Dispatch result types (discriminated union)
27
-
28
- /** Successful dispatch — target command found with argv path. */
29
- interface DispatchMatch {
30
- /** Discriminant — a command name in argv matched a registered command. */
31
- readonly kind: 'match';
32
- /** The matched (target) command. */
33
- readonly command: ErasedCommand;
34
- /** Root → target (inclusive). Used for `collectPropagatedFlags()`. */
35
- readonly commandPath: readonly CommandSchema[];
36
- /** argv after consuming command name segments. */
37
- readonly remainingArgv: readonly string[];
38
- }
39
-
40
- /** Target has subcommands but no handler and no subcommand was specified/matched. */
41
- interface DispatchNeedsSubcommand {
42
- /** Discriminant — command group reached without a subcommand or handler. */
43
- readonly kind: 'needs-subcommand';
44
- /** The group command that needs a subcommand. */
45
- readonly command: ErasedCommand;
46
- /** Root → group (inclusive). */
47
- readonly commandPath: readonly CommandSchema[];
48
- }
49
-
50
- /** Unknown command name at this dispatch level. */
51
- interface DispatchUnknown {
52
- /** Discriminant — no registered command matched the input token. */
53
- readonly kind: 'unknown';
54
- /** The unrecognised input token. Empty string when no token present. */
55
- readonly input: string;
56
- /** Commands available at the level where matching failed. */
57
- readonly candidates: readonly ErasedCommand[];
58
- /** Ancestor path up to (but not including) the unknown level. */
59
- readonly parentPath: readonly CommandSchema[];
60
- }
61
-
62
- /** Discriminated result of recursive command dispatch. */
63
- type DispatchResult = DispatchMatch | DispatchNeedsSubcommand | DispatchUnknown;
64
-
65
- // --- Recursive dispatch
66
-
67
- /**
68
- * Recursively walk argv, consuming command name segments from the front.
69
- *
70
- * At each level, the first non-flag token is tested as a command name
71
- * against the provided command map. If matched and the matched command
72
- * has subcommands, dispatch recurses into the child level with the
73
- * remaining argv.
74
- *
75
- * Ambiguity resolution for commands that have both an action handler AND
76
- * subcommands (e.g. `git remote` lists remotes, `git remote add` dispatches):
77
- * - If the next token matches a subcommand → descend
78
- * - If the next token is unknown and the command has a handler → match here
79
- * - If the next token is unknown and no handler → propagate unknown error
80
- * - If no next token and command has a handler → match here
81
- * - If no next token and no handler → needs-subcommand
82
- *
83
- * @param argv - Remaining argv tokens (command names + flags + args).
84
- * @param commands - Command map at the current tree level.
85
- * @param ancestorPath - Schema path from root to current level (exclusive).
86
- * @param valueFlags - Lookup of flags valid at this level, used so a
87
- * space-separated value-flag's value (`--region eu`) is not mistaken for a
88
- * command name. Empty by default (arity-unaware, legacy behaviour).
89
- * @returns Discriminated dispatch result.
90
- *
91
- * @internal
92
- */
93
- function dispatch(
94
- argv: readonly string[],
95
- commands: ReadonlyMap<string, ErasedCommand>,
96
- ancestorPath: readonly CommandSchema[] = [],
97
- valueFlags: ValueFlagLookup = new Map(),
98
- ): DispatchResult {
99
- // Find first non-flag token (potential command name).
100
- // Flags may appear before the command name (e.g. `--verbose db migrate`).
101
- // `--` terminates flag scanning — the next token is treated as a command name.
102
- // Value-flags in space-separated form (`--region eu`, `-r eu`) consume the
103
- // following token as their value, so it must be skipped rather than matched
104
- // as a command name (see #25).
105
- let cmdIdx = -1;
106
- for (let i = 0; i < argv.length; i++) {
107
- const token = argv[i];
108
- if (token === undefined) continue;
109
- if (token === '--') {
110
- // End-of-flags marker: next token (if any) is the command name.
111
- if (i + 1 < argv.length) cmdIdx = i + 1;
112
- break;
113
- }
114
- if (token.startsWith('-')) {
115
- if (consumesFollowingToken(token, valueFlags)) i++; // skip the flag's value
116
- continue;
117
- }
118
- cmdIdx = i;
119
- break;
120
- }
121
-
122
- if (cmdIdx === -1) {
123
- // No command name token found — only flags or empty argv.
124
- return {
125
- kind: 'unknown',
126
- input: '',
127
- candidates: uniqueCommands(commands),
128
- parentPath: ancestorPath,
129
- };
130
- }
131
-
132
- const cmdName = argv[cmdIdx];
133
- if (cmdName === undefined) {
134
- return {
135
- kind: 'unknown',
136
- input: '',
137
- candidates: uniqueCommands(commands),
138
- parentPath: ancestorPath,
139
- };
140
- }
141
-
142
- const matched = commands.get(cmdName);
143
- if (matched === undefined) {
144
- return {
145
- kind: 'unknown',
146
- input: cmdName,
147
- candidates: uniqueCommands(commands),
148
- parentPath: ancestorPath,
149
- };
150
- }
151
-
152
- // Remove command name token from argv, preserving order.
153
- const remaining = [...argv.slice(0, cmdIdx), ...argv.slice(cmdIdx + 1)];
154
- const currentPath = [...ancestorPath, matched.schema];
155
-
156
- // If command has subcommands, try to descend. The matched command's own
157
- // flags govern arity at the child level (e.g. `db --config x migrate`).
158
- if (matched.subcommands.size > 0) {
159
- const subResult = dispatch(
160
- remaining,
161
- matched.subcommands,
162
- currentPath,
163
- buildFlagLookup(matched.schema.flags),
164
- );
165
-
166
- switch (subResult.kind) {
167
- case 'match':
168
- // Successfully found deeper target.
169
- return subResult;
170
-
171
- case 'unknown':
172
- if (subResult.input === '') {
173
- // No subcommand specified.
174
- if (matched.schema.hasAction) {
175
- return {
176
- kind: 'match',
177
- command: matched,
178
- commandPath: currentPath,
179
- remainingArgv: remaining,
180
- };
181
- }
182
- return { kind: 'needs-subcommand', command: matched, commandPath: currentPath };
183
- }
184
-
185
- // Unknown token — might be a positional arg for this command.
186
- if (matched.schema.hasAction) {
187
- return {
188
- kind: 'match',
189
- command: matched,
190
- commandPath: currentPath,
191
- remainingArgv: remaining,
192
- };
193
- }
194
- // No handler — propagate the unknown error.
195
- return subResult;
196
-
197
- case 'needs-subcommand':
198
- // Propagate from deeper level.
199
- return subResult;
200
-
201
- default: {
202
- // Exhaustiveness guard — ensures all DispatchResult variants are handled.
203
- const _exhaustive: never = subResult;
204
- return _exhaustive;
205
- }
206
- }
207
- }
208
-
209
- // Leaf command (no subcommands).
210
- return {
211
- kind: 'match',
212
- command: matched,
213
- commandPath: currentPath,
214
- remainingArgv: remaining,
215
- };
216
- }
217
-
218
- // --- Flag-arity awareness
219
-
220
- /**
221
- * Whether a flag token consumes the following argv token as its value.
222
- *
223
- * Mirrors the parser's value-consumption rules (`parseLongFlag` /
224
- * `parseShortFlags`) so the command-name scan skips exactly the tokens the
225
- * parser would treat as flag values:
226
- * - `--flag value` → consumes when `--flag` is a known value-flag.
227
- * - `--flag=value` → self-contained, consumes nothing.
228
- * - `-abc value` → consumes only when the trailing short flag is a value-flag;
229
- * an earlier value-flag char takes the rest of the group as an inline value.
230
- *
231
- * Unknown flags conservatively consume nothing (the parser reports them later).
232
- *
233
- * @internal
234
- */
235
- function consumesFollowingToken(token: string, valueFlags: ValueFlagLookup): boolean {
236
- if (token.startsWith('--')) {
237
- if (token.includes('=')) return false;
238
- const entry = valueFlags.get(token.slice(2));
239
- return entry !== undefined && flagExpectsValue(entry[1]);
240
- }
241
-
242
- // Short flag group (`-abc`). A value attaches to the trailing flag only; an
243
- // earlier value-flag char swallows the remaining chars as an inline value.
244
- const chars = token.slice(1);
245
- for (let i = 0; i < chars.length; i++) {
246
- const entry = valueFlags.get(chars.charAt(i));
247
- if (entry === undefined) return false;
248
- if (!flagExpectsValue(entry[1])) continue;
249
- return i === chars.length - 1;
250
- }
251
- return false;
252
- }
253
-
254
- // --- "Did you mean?" suggestion
255
-
256
- /**
257
- * Levenshtein distance between two strings.
258
- * @internal
259
- */
260
- function levenshtein(a: string, b: string): number {
261
- const m = a.length;
262
- const n = b.length;
263
-
264
- // Flat (m+1)×(n+1) matrix — fully initialised, no sparse access.
265
- // Two-row rolling buffer: only the previous and current rows are needed.
266
- const w = n + 1;
267
- let prev = new Uint16Array(w);
268
- let curr = new Uint16Array(w);
269
-
270
- // Base row: distance from empty string to b[0..j]
271
- for (let j = 0; j <= n; j++) prev[j] = j;
272
-
273
- for (let i = 1; i <= m; i++) {
274
- curr[0] = i;
275
- for (let j = 1; j <= n; j++) {
276
- const cost = a.charAt(i - 1) === b.charAt(j - 1) ? 0 : 1;
277
- // All indices are in-bounds: j ∈ [1,n], j-1 ∈ [0,n-1], both < w.
278
- // prev/curr are dense Uint16Arrays of length w — no holes.
279
- const del = prev[j] ?? 0;
280
- const ins = curr[j - 1] ?? 0;
281
- const sub = prev[j - 1] ?? 0;
282
- curr[j] = Math.min(del + 1, ins + 1, sub + cost);
283
- }
284
- [prev, curr] = [curr, prev];
285
- }
286
-
287
- // After the loop, `prev` holds the last computed row (swapped at loop end).
288
- return prev[n] ?? Math.max(m, n);
289
- }
290
-
291
- /**
292
- * Find the closest command name match for a "did you mean?" suggestion.
293
- *
294
- * Searches command names and aliases. Returns `undefined` if no
295
- * sufficiently close match exists (threshold: 3).
296
- *
297
- * @internal
298
- */
299
- function findClosestCommand(input: string, commands: readonly ErasedCommand[]): string | undefined {
300
- const MAX_DISTANCE = 3;
301
- let bestName: string | undefined;
302
- let bestDist = MAX_DISTANCE + 1;
303
-
304
- for (const cmd of commands) {
305
- // Check main name
306
- const nameDist = levenshtein(input, cmd.schema.name);
307
- if (nameDist < bestDist) {
308
- bestDist = nameDist;
309
- bestName = cmd.schema.name;
310
- }
311
- // Check aliases
312
- for (const alias of cmd.schema.aliases) {
313
- const aliasDist = levenshtein(input, alias);
314
- if (aliasDist < bestDist) {
315
- bestDist = aliasDist;
316
- bestName = cmd.schema.name; // suggest canonical name
317
- }
318
- }
319
- }
320
-
321
- return bestDist <= MAX_DISTANCE ? bestName : undefined;
322
- }
323
-
324
- // --- Helpers
325
-
326
- /**
327
- * Deduplicate commands from a name+alias map.
328
- *
329
- * A map keyed by name *and* alias contains duplicate entries for aliased
330
- * commands. This returns unique commands (by schema identity).
331
- *
332
- * @internal
333
- */
334
- function uniqueCommands(commands: ReadonlyMap<string, ErasedCommand>): readonly ErasedCommand[] {
335
- const seen = new Set<ErasedCommand>();
336
- const result: ErasedCommand[] = [];
337
- for (const cmd of commands.values()) {
338
- if (!seen.has(cmd)) {
339
- seen.add(cmd);
340
- result.push(cmd);
341
- }
342
- }
343
- return result;
344
- }
345
-
346
- // --- Exports
347
-
348
- export type { DispatchMatch, DispatchNeedsSubcommand, DispatchResult, DispatchUnknown, ValueFlagLookup };
349
- export { consumesFollowingToken, dispatch, findClosestCommand, levenshtein, uniqueCommands };
@@ -1,82 +0,0 @@
1
- /**
2
- * Root-help header hyperlink resolution.
3
- *
4
- * Stores explicit `.links()` URLs and derives missing ones from package.json
5
- * metadata (`repository` / `homepage`) once that data is available — during
6
- * runtime preflight for `.run()`, or directly from pre-loaded
7
- * `.packageJson(data)` for `.execute()`.
8
- *
9
- * @module dreamcli/core/cli/help-links
10
- * @internal
11
- */
12
-
13
- import type { PackageJsonData } from '#internals/core/config/package-json.ts';
14
- import { packageRepositoryUrl } from '#internals/core/config/package-json.ts';
15
-
16
- /**
17
- * OSC 8 hyperlink targets for the root-help header.
18
- *
19
- * Set via `CLIBuilder.links()`; fields left `undefined` are derived from
20
- * package.json metadata when `.packageJson()` is active.
21
- */
22
- interface HelpLinks {
23
- /** URL the program name links to (e.g. the repository or homepage). */
24
- readonly name: string | undefined;
25
- /** URL the version links to (e.g. the release tag). */
26
- readonly version: string | undefined;
27
- }
28
-
29
- /**
30
- * Build the release-tag URL for a version on a known forge.
31
- *
32
- * GitHub and GitLab have stable release-tag routes; other hosts return
33
- * `undefined` rather than guessing.
34
- *
35
- * @internal
36
- */
37
- function releaseTagUrl(repoUrl: string, version: string): string | undefined {
38
- let hostname: string;
39
- try {
40
- hostname = new URL(repoUrl).hostname;
41
- } catch {
42
- return undefined;
43
- }
44
- if (hostname === 'github.com') return `${repoUrl}/releases/tag/v${version}`;
45
- if (hostname === 'gitlab.com') return `${repoUrl}/-/releases/v${version}`;
46
- return undefined;
47
- }
48
-
49
- /**
50
- * Fill `undefined` link fields from package.json metadata.
51
- *
52
- * Explicit URLs always win. The name falls back to the normalized
53
- * `repository` URL, then `homepage`; the version falls back to the forge
54
- * release-tag URL when both a repository and a version are known.
55
- * Idempotent — already-resolved fields pass through unchanged.
56
- *
57
- * @internal
58
- */
59
- function deriveHelpLinks(
60
- links: HelpLinks | undefined,
61
- pkg: PackageJsonData | undefined,
62
- version: string | undefined,
63
- ): HelpLinks | undefined {
64
- if (links === undefined) return undefined;
65
- if (pkg === undefined || (links.name !== undefined && links.version !== undefined)) {
66
- return links;
67
- }
68
- const repoUrl = packageRepositoryUrl(pkg);
69
- return {
70
- name: links.name ?? repoUrl ?? pkg.homepage,
71
- version:
72
- links.version ??
73
- (repoUrl !== undefined && version !== undefined
74
- ? releaseTagUrl(repoUrl, version)
75
- : undefined),
76
- };
77
- }
78
-
79
- // --- Exports
80
-
81
- export type { HelpLinks };
82
- export { deriveHelpLinks };