@topogram/cli 0.3.62 → 0.3.64

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 (121) hide show
  1. package/package.json +1 -1
  2. package/src/adoption/plan.d.ts +6 -0
  3. package/src/adoption/reporting.d.ts +10 -0
  4. package/src/adoption/review-groups.d.ts +6 -0
  5. package/src/agent-brief.d.ts +3 -0
  6. package/src/agent-brief.js +495 -0
  7. package/src/agent-ops/query-builders.d.ts +26 -0
  8. package/src/archive/archive.d.ts +2 -0
  9. package/src/archive/compact.d.ts +1 -0
  10. package/src/archive/unarchive.d.ts +1 -0
  11. package/src/catalog.d.ts +10 -0
  12. package/src/catalog.js +62 -66
  13. package/src/cli/catalog-alias.d.ts +1 -0
  14. package/src/cli/command-parser.js +38 -0
  15. package/src/cli/command-parsers/core.js +102 -0
  16. package/src/cli/command-parsers/generator.js +39 -0
  17. package/src/cli/command-parsers/import.js +44 -0
  18. package/src/cli/command-parsers/legacy-workflow.js +21 -0
  19. package/src/cli/command-parsers/project.js +47 -0
  20. package/src/cli/command-parsers/sdlc.js +47 -0
  21. package/src/cli/command-parsers/shared.js +51 -0
  22. package/src/cli/command-parsers/template.js +48 -0
  23. package/src/cli/commands/agent.js +47 -0
  24. package/src/cli/commands/catalog.js +617 -0
  25. package/src/cli/commands/check.js +268 -0
  26. package/src/cli/commands/doctor.js +268 -0
  27. package/src/cli/commands/emit.js +149 -0
  28. package/src/cli/commands/generate.js +96 -0
  29. package/src/cli/commands/generator-policy.js +785 -0
  30. package/src/cli/commands/generator.js +443 -0
  31. package/src/cli/commands/import-runner.js +157 -0
  32. package/src/cli/commands/import.js +1734 -0
  33. package/src/cli/commands/inspect.js +55 -0
  34. package/src/cli/commands/new.js +94 -0
  35. package/src/cli/commands/package.js +815 -0
  36. package/src/cli/commands/query.js +1302 -0
  37. package/src/cli/commands/release-rollout.js +257 -0
  38. package/src/cli/commands/release-shared.js +528 -0
  39. package/src/cli/commands/release-status.js +429 -0
  40. package/src/cli/commands/release.js +107 -0
  41. package/src/cli/commands/sdlc.js +168 -0
  42. package/src/cli/commands/setup.js +76 -0
  43. package/src/cli/commands/source.js +291 -0
  44. package/src/cli/commands/template-runner.js +198 -0
  45. package/src/cli/commands/template.js +2145 -0
  46. package/src/cli/commands/trust.js +219 -0
  47. package/src/cli/commands/version.js +40 -0
  48. package/src/cli/commands/widget.js +168 -0
  49. package/src/cli/commands/workflow.js +63 -0
  50. package/src/cli/dispatcher.js +392 -0
  51. package/src/cli/help-dispatch.js +188 -0
  52. package/src/cli/help.js +296 -0
  53. package/src/cli/migration-guidance.js +59 -0
  54. package/src/cli/options.js +96 -0
  55. package/src/cli/output-safety.js +107 -0
  56. package/src/cli/path-normalization.js +29 -0
  57. package/src/cli.js +47 -11711
  58. package/src/example-implementation.d.ts +2 -0
  59. package/src/format.d.ts +1 -0
  60. package/src/generator/check.d.ts +1 -0
  61. package/src/generator/context/bundle.d.ts +1 -0
  62. package/src/generator/context/shared.d.ts +2 -0
  63. package/src/generator/native/parity-bundle.js +2 -1
  64. package/src/generator/surfaces/web/html-escape.js +22 -0
  65. package/src/generator/surfaces/web/react.js +10 -8
  66. package/src/generator/surfaces/web/sveltekit.js +7 -5
  67. package/src/generator/surfaces/web/vanilla.js +8 -4
  68. package/src/generator.d.ts +2 -0
  69. package/src/github-client.js +520 -0
  70. package/src/import/core/shared.js +20 -62
  71. package/src/import/extractors/api/flutter-dio.js +4 -8
  72. package/src/import/extractors/api/react-native-repository.js +4 -8
  73. package/src/import/index.d.ts +4 -0
  74. package/src/import/provenance.d.ts +4 -0
  75. package/src/new-project.js +100 -11
  76. package/src/npm-safety.js +79 -0
  77. package/src/parser.d.ts +1 -0
  78. package/src/path-helpers.d.ts +1 -0
  79. package/src/path-helpers.js +20 -0
  80. package/src/project-config.js +1 -0
  81. package/src/reconcile/docs.d.ts +8 -0
  82. package/src/reconcile/journeys.d.ts +1 -0
  83. package/src/resolver.d.ts +1 -0
  84. package/src/runtime-support.js +29 -0
  85. package/src/sdlc/adopt.d.ts +1 -0
  86. package/src/sdlc/check.d.ts +1 -0
  87. package/src/sdlc/explain.d.ts +1 -0
  88. package/src/sdlc/release.d.ts +1 -0
  89. package/src/sdlc/scaffold.d.ts +1 -0
  90. package/src/sdlc/transition.d.ts +1 -0
  91. package/src/text-helpers.d.ts +6 -0
  92. package/src/text-helpers.js +245 -0
  93. package/src/topogram-config.js +306 -0
  94. package/src/validator.d.ts +2 -0
  95. package/src/workflows/adoption/index.js +26 -0
  96. package/src/workflows/docs-generate.js +262 -0
  97. package/src/workflows/docs-scan.js +703 -0
  98. package/src/workflows/docs.js +15 -0
  99. package/src/workflows/import-app/api.js +799 -0
  100. package/src/workflows/import-app/db.js +538 -0
  101. package/src/workflows/import-app/index.js +30 -0
  102. package/src/workflows/import-app/shared.js +218 -0
  103. package/src/workflows/import-app/ui.js +443 -0
  104. package/src/workflows/import-app/workflow.js +159 -0
  105. package/src/workflows/reconcile/adoption-plan.js +742 -0
  106. package/src/workflows/reconcile/auth.js +692 -0
  107. package/src/workflows/reconcile/bundle-core.js +600 -0
  108. package/src/workflows/reconcile/bundle-shared.js +75 -0
  109. package/src/workflows/reconcile/candidate-model.js +477 -0
  110. package/src/workflows/reconcile/canonical-surface.js +264 -0
  111. package/src/workflows/reconcile/gap-report.js +333 -0
  112. package/src/workflows/reconcile/ids.js +6 -0
  113. package/src/workflows/reconcile/impacts.js +625 -0
  114. package/src/workflows/reconcile/index.js +7 -0
  115. package/src/workflows/reconcile/renderers.js +461 -0
  116. package/src/workflows/reconcile/summary.js +90 -0
  117. package/src/workflows/reconcile/workflow.js +309 -0
  118. package/src/workflows/shared.js +189 -0
  119. package/src/workflows/types.d.ts +93 -0
  120. package/src/workflows.d.ts +1 -0
  121. package/src/workflows.js +10 -7652
@@ -0,0 +1,617 @@
1
+ // @ts-check
2
+
3
+ import childProcess from "node:child_process";
4
+ import path from "node:path";
5
+
6
+ import { stableStringify } from "../../format.js";
7
+ import {
8
+ catalogEntryPackageSpec,
9
+ catalogSourceOrDefault,
10
+ checkCatalogSource,
11
+ copyCatalogTopogramEntry,
12
+ findCatalogEntry,
13
+ loadCatalog,
14
+ TOPOGRAM_SOURCE_FILE
15
+ } from "../../catalog.js";
16
+ import { githubAuthStatus } from "../../github-client.js";
17
+ import { assertSafeNpmSpec, localNpmrcEnv } from "../../npm-safety.js";
18
+
19
+ /**
20
+ * @returns {void}
21
+ */
22
+ export function printCatalogHelp() {
23
+ console.log("Usage: topogram catalog list [--json] [--catalog <path-or-source>]");
24
+ console.log(" or: topogram catalog show <id> [--json] [--catalog <path-or-source>]");
25
+ console.log(" or: topogram catalog doctor [--json] [--catalog <path-or-source>]");
26
+ console.log(" or: topogram catalog check <path-or-url> [--json]");
27
+ console.log(" or: topogram catalog copy <id> <target> [--version <version>] [--json] [--catalog <path-or-source>]");
28
+ console.log("");
29
+ console.log("Catalog commands inspect the shared Topogram index. The catalog is an index; templates and topograms resolve to versioned packages.");
30
+ console.log("");
31
+ console.log("Examples:");
32
+ console.log(" topogram catalog list");
33
+ console.log(" topogram catalog show hello-web");
34
+ console.log(" topogram catalog doctor");
35
+ console.log(" topogram catalog check topograms.catalog.json");
36
+ console.log(" topogram catalog copy hello ./hello-topogram");
37
+ }
38
+
39
+ /**
40
+ * @param {unknown} error
41
+ * @returns {string}
42
+ */
43
+ function messageFromError(error) {
44
+ return error instanceof Error ? error.message : String(error);
45
+ }
46
+
47
+ /**
48
+ * @param {string} value
49
+ * @returns {string}
50
+ */
51
+ export function shellCommandArg(value) {
52
+ return /^[A-Za-z0-9_./:@=-]+$/.test(value) ? value : JSON.stringify(value);
53
+ }
54
+
55
+ /**
56
+ * @param {string|null} source
57
+ * @returns {{ ok: boolean, source: string, catalog: any, entries: any[], diagnostics: any[], errors: string[] }}
58
+ */
59
+ export function buildCatalogListPayload(source) {
60
+ const loaded = loadCatalog(source || null);
61
+ return {
62
+ ok: true,
63
+ source: loaded.source,
64
+ catalog: {
65
+ version: loaded.catalog.version,
66
+ entries: loaded.catalog.entries.length
67
+ },
68
+ entries: loaded.catalog.entries,
69
+ diagnostics: loaded.diagnostics,
70
+ errors: []
71
+ };
72
+ }
73
+
74
+ /**
75
+ * @param {ReturnType<typeof buildCatalogListPayload>} payload
76
+ * @returns {void}
77
+ */
78
+ export function printCatalogList(payload) {
79
+ console.log("Catalog entries:");
80
+ console.log("Template entries create starters with `topogram new`; topogram entries copy editable Topogram source.");
81
+ console.log(`Catalog: ${payload.source}`);
82
+ console.log(`Version: ${payload.catalog.version}`);
83
+ const catalogOption = payload.source === catalogSourceOrDefault(null)
84
+ ? ""
85
+ : ` --catalog ${shellCommandArg(payload.source)}`;
86
+ for (const entry of payload.entries) {
87
+ console.log(`- ${entry.id} (${entry.kind})`);
88
+ console.log(` Package: ${entry.package}@${entry.defaultVersion}`);
89
+ console.log(` Description: ${entry.description}`);
90
+ console.log(` Trust scope: ${entry.trust.scope}`);
91
+ console.log(` Executable implementation: ${entry.trust.includesExecutableImplementation ? "yes" : "no"}`);
92
+ if (entry.kind === "template") {
93
+ console.log(` New: topogram new ./my-app --template ${shellCommandArg(entry.id)}${catalogOption}`);
94
+ } else {
95
+ console.log(` Copy: topogram catalog copy ${shellCommandArg(entry.id)} ./${entry.id}-topogram${catalogOption}`);
96
+ }
97
+ }
98
+ }
99
+
100
+ /**
101
+ * @param {string} id
102
+ * @param {string|null} source
103
+ * @returns {{ ok: boolean, source: string, catalog: { version: string }, entry: any|null, packageSpec: string|null, commands: { primary: string|null, followUp: string[] }, diagnostics: any[], errors: string[] }}
104
+ */
105
+ export function buildCatalogShowPayload(id, source) {
106
+ if (!id || id.startsWith("-")) {
107
+ throw new Error("topogram catalog show requires <id>.");
108
+ }
109
+ const loaded = loadCatalog(source || null);
110
+ const entry = findCatalogEntry(loaded.catalog, id, null);
111
+ if (!entry) {
112
+ const diagnostic = {
113
+ code: "catalog_entry_not_found",
114
+ severity: "error",
115
+ message: `Catalog entry '${id}' was not found in ${loaded.source}.`,
116
+ path: loaded.source,
117
+ suggestedFix: "Run `topogram catalog list` to see available entries."
118
+ };
119
+ return {
120
+ ok: false,
121
+ source: loaded.source,
122
+ catalog: { version: loaded.catalog.version },
123
+ entry: null,
124
+ packageSpec: null,
125
+ commands: { primary: null, followUp: [] },
126
+ diagnostics: [diagnostic],
127
+ errors: [diagnostic.message]
128
+ };
129
+ }
130
+ return {
131
+ ok: true,
132
+ source: loaded.source,
133
+ catalog: { version: loaded.catalog.version },
134
+ entry,
135
+ packageSpec: catalogEntryPackageSpec(entry),
136
+ commands: catalogShowCommands(entry, loaded.source),
137
+ diagnostics: loaded.diagnostics,
138
+ errors: []
139
+ };
140
+ }
141
+
142
+ /**
143
+ * @param {any} entry
144
+ * @param {string} source
145
+ * @returns {{ primary: string, followUp: string[] }}
146
+ */
147
+ export function catalogShowCommands(entry, source) {
148
+ const catalogOption = source === catalogSourceOrDefault(null)
149
+ ? ""
150
+ : ` --catalog ${shellCommandArg(source)}`;
151
+ if (entry.kind === "template") {
152
+ const target = "./my-app";
153
+ return {
154
+ primary: `topogram new ${target} --template ${shellCommandArg(entry.id)}${catalogOption}`,
155
+ followUp: [
156
+ `cd ${target}`,
157
+ "npm install",
158
+ "npm run check",
159
+ "npm run generate"
160
+ ]
161
+ };
162
+ }
163
+ const target = `./${entry.id}-topogram`;
164
+ return {
165
+ primary: `topogram catalog copy ${shellCommandArg(entry.id)} ${target}${catalogOption}`,
166
+ followUp: [
167
+ `cd ${target}`,
168
+ "topogram source status --local",
169
+ "topogram check",
170
+ "topogram generate"
171
+ ]
172
+ };
173
+ }
174
+
175
+ /**
176
+ * @param {ReturnType<typeof buildCatalogShowPayload>} payload
177
+ * @returns {void}
178
+ */
179
+ export function printCatalogShow(payload) {
180
+ if (!payload.ok || !payload.entry) {
181
+ console.log("Catalog entry not found.");
182
+ console.log(`Catalog: ${payload.source}`);
183
+ for (const diagnostic of payload.diagnostics) {
184
+ const label = diagnostic.severity === "warning" ? "Warning" : "Error";
185
+ console.log(`${label}: ${diagnostic.message}`);
186
+ }
187
+ return;
188
+ }
189
+ const { entry } = payload;
190
+ console.log(`Catalog entry: ${entry.id}`);
191
+ console.log(`Kind: ${entry.kind}`);
192
+ if (entry.kind === "template") {
193
+ console.log("Action: creates a starter app workspace with `topogram new`.");
194
+ } else {
195
+ console.log("Action: copies editable Topogram source with `topogram catalog copy`.");
196
+ console.log("Executable implementation: no (topogram entries cannot include implementation/ in v1).");
197
+ }
198
+ console.log(`Catalog: ${payload.source}`);
199
+ console.log(`Package: ${payload.packageSpec}`);
200
+ console.log(`Description: ${entry.description}`);
201
+ console.log(`Tags: ${entry.tags.join(", ") || "none"}`);
202
+ console.log(`Trust scope: ${entry.trust.scope}`);
203
+ if (entry.kind === "template") {
204
+ console.log(`Executable implementation: ${entry.trust.includesExecutableImplementation ? "yes" : "no"}`);
205
+ }
206
+ if (entry.trust.notes) {
207
+ console.log(`Trust notes: ${entry.trust.notes}`);
208
+ }
209
+ console.log("");
210
+ console.log("Recommended command:");
211
+ console.log(` ${payload.commands.primary}`);
212
+ if (payload.commands.followUp.length > 0) {
213
+ console.log("Follow-up:");
214
+ for (const command of payload.commands.followUp) {
215
+ console.log(` ${command}`);
216
+ }
217
+ }
218
+ if (entry.kind === "topogram") {
219
+ console.log("");
220
+ console.log(`${TOPOGRAM_SOURCE_FILE} will record copy provenance only. Local edits are allowed.`);
221
+ }
222
+ }
223
+
224
+ /**
225
+ * @param {string|null} source
226
+ * @returns {{ ok: boolean, source: string, auth: { githubTokenEnv: boolean, ghTokenEnv: boolean, ghCli: { checked: boolean, available: boolean, authenticated: boolean, reason: string|null } }, catalog: { reachable: boolean, version: string|null, entries: number }, packages: Array<{ id: string, kind: string, package: string, version: string, packageSpec: string, ok: boolean, checkedVersion: string|null, diagnostics: any[] }>, diagnostics: any[], errors: string[] }}
227
+ */
228
+ export function buildCatalogDoctorPayload(source) {
229
+ const resolvedSource = catalogSourceOrDefault(source || null);
230
+ const auth = buildCatalogDoctorAuth(resolvedSource);
231
+ const diagnostics = [];
232
+ /** @type {Array<{ id: string, kind: string, package: string, version: string, packageSpec: string, ok: boolean, checkedVersion: string|null, diagnostics: any[] }>} */
233
+ const packages = [];
234
+ let loaded = null;
235
+ try {
236
+ loaded = loadCatalog(source || null);
237
+ } catch (error) {
238
+ const diagnostic = {
239
+ code: "catalog_unreachable",
240
+ severity: "error",
241
+ message: messageFromError(error),
242
+ path: resolvedSource,
243
+ suggestedFix: catalogDoctorSourceFix(resolvedSource)
244
+ };
245
+ return {
246
+ ok: false,
247
+ source: resolvedSource,
248
+ auth,
249
+ catalog: {
250
+ reachable: false,
251
+ version: null,
252
+ entries: 0
253
+ },
254
+ packages,
255
+ diagnostics: [diagnostic],
256
+ errors: [diagnostic.message]
257
+ };
258
+ }
259
+ diagnostics.push(...loaded.diagnostics);
260
+ for (const entry of loaded.catalog.entries) {
261
+ packages.push(checkCatalogDoctorPackage(entry));
262
+ }
263
+ const packageDiagnostics = packages.flatMap((entry) => entry.diagnostics);
264
+ const allDiagnostics = [...diagnostics, ...packageDiagnostics];
265
+ const errors = allDiagnostics
266
+ .filter((diagnostic) => diagnostic.severity === "error")
267
+ .map((diagnostic) => diagnostic.message);
268
+ return {
269
+ ok: errors.length === 0,
270
+ source: loaded.source,
271
+ auth,
272
+ catalog: {
273
+ reachable: true,
274
+ version: loaded.catalog.version,
275
+ entries: loaded.catalog.entries.length
276
+ },
277
+ packages,
278
+ diagnostics: allDiagnostics,
279
+ errors
280
+ };
281
+ }
282
+
283
+ /**
284
+ * @param {string} source
285
+ * @returns {{ githubTokenEnv: boolean, ghTokenEnv: boolean, ghCli: { checked: boolean, available: boolean, authenticated: boolean, reason: string|null } }}
286
+ */
287
+ export function buildCatalogDoctorAuth(source) {
288
+ const shouldCheckGh = source.startsWith("github:");
289
+ return githubAuthStatus({
290
+ checkGh: shouldCheckGh && !process.env.GITHUB_TOKEN && !process.env.GH_TOKEN
291
+ });
292
+ }
293
+
294
+ /**
295
+ * @param {any} entry
296
+ * @returns {{ id: string, kind: string, package: string, version: string, packageSpec: string, ok: boolean, checkedVersion: string|null, diagnostics: any[] }}
297
+ */
298
+ function checkCatalogDoctorPackage(entry) {
299
+ const packageSpec = catalogEntryPackageSpec(entry);
300
+ const result = runNpmViewPackageSpec(packageSpec);
301
+ if (result.status === 0) {
302
+ const checkedVersion = String(result.stdout || "").trim().replace(/^"|"$/g, "");
303
+ return {
304
+ id: entry.id,
305
+ kind: entry.kind,
306
+ package: entry.package,
307
+ version: entry.defaultVersion,
308
+ packageSpec,
309
+ ok: checkedVersion === entry.defaultVersion,
310
+ checkedVersion: checkedVersion || null,
311
+ diagnostics: checkedVersion === entry.defaultVersion ? [] : [{
312
+ code: "catalog_package_version_mismatch",
313
+ severity: "error",
314
+ message: `Catalog entry '${entry.id}' expected ${packageSpec}, but npm returned '${checkedVersion || "(empty)"}'.`,
315
+ path: entry.id,
316
+ suggestedFix: "Check defaultVersion in the catalog, or publish the referenced package version."
317
+ }]
318
+ };
319
+ }
320
+ const diagnostic = catalogDoctorPackageDiagnostic(entry, packageSpec, result);
321
+ return {
322
+ id: entry.id,
323
+ kind: entry.kind,
324
+ package: entry.package,
325
+ version: entry.defaultVersion,
326
+ packageSpec,
327
+ ok: false,
328
+ checkedVersion: null,
329
+ diagnostics: [diagnostic]
330
+ };
331
+ }
332
+
333
+ /**
334
+ * @param {string} packageSpec
335
+ * @returns {{ status: number|null, stdout: string, stderr: string, error?: Error }}
336
+ */
337
+ export function runNpmViewPackageSpec(packageSpec) {
338
+ assertSafeNpmSpec(packageSpec);
339
+ const npmBin = process.platform === "win32" ? "npm.cmd" : "npm";
340
+ return childProcess.spawnSync(npmBin, ["view", "--json", "--", packageSpec, "version"], {
341
+ encoding: "utf8",
342
+ env: {
343
+ ...process.env,
344
+ ...localNpmrcEnv(process.cwd()),
345
+ PATH: process.env.PATH || ""
346
+ }
347
+ });
348
+ }
349
+
350
+ /**
351
+ * @param {any} entry
352
+ * @param {string} packageSpec
353
+ * @param {{ stdout?: string, stderr?: string, error?: Error }} result
354
+ * @returns {any}
355
+ */
356
+ export function catalogDoctorPackageDiagnostic(entry, packageSpec, result) {
357
+ const output = [result.error?.message, result.stderr, result.stdout].filter(Boolean).join("\n").trim();
358
+ const normalized = output.toLowerCase();
359
+ if (result.error?.message && result.error.message.includes("ENOENT")) {
360
+ return {
361
+ code: "npm_not_found",
362
+ severity: "error",
363
+ message: "npm is required to check catalog package access.",
364
+ path: entry.id,
365
+ suggestedFix: "Install npm with Node.js, then rerun `topogram catalog doctor`."
366
+ };
367
+ }
368
+ if (/\b(401|e401|authentication|auth token|login)\b/.test(normalized)) {
369
+ return {
370
+ code: "catalog_package_auth_required",
371
+ severity: "error",
372
+ message: `Authentication is required to inspect package '${packageSpec}'.`,
373
+ path: entry.id,
374
+ suggestedFix: "Configure registry-specific npm auth when using private packages."
375
+ };
376
+ }
377
+ if (/\b(403|e403|forbidden|permission|denied)\b/.test(normalized)) {
378
+ return {
379
+ code: "catalog_package_access_denied",
380
+ severity: "error",
381
+ message: `Package access was denied for '${packageSpec}'.`,
382
+ path: entry.id,
383
+ suggestedFix: "Check package visibility and registry-specific npm auth for the consumer environment."
384
+ };
385
+ }
386
+ if (/\b(404|e404|not found)\b/.test(normalized)) {
387
+ return {
388
+ code: "catalog_package_not_found",
389
+ severity: "error",
390
+ message: `Package '${packageSpec}' was not found, or the current npm token cannot see it.`,
391
+ path: entry.id,
392
+ suggestedFix: "Check the package name/version and npm package registry access."
393
+ };
394
+ }
395
+ return {
396
+ code: "catalog_package_check_failed",
397
+ severity: "error",
398
+ message: `Failed to inspect package '${packageSpec}'.${output ? `\n${output}` : ""}`,
399
+ path: entry.id,
400
+ suggestedFix: "Run `npm view <package>@<version> version --json` with the same npm auth configuration to debug package access."
401
+ };
402
+ }
403
+
404
+ /**
405
+ * @param {string} source
406
+ * @returns {string}
407
+ */
408
+ function catalogDoctorSourceFix(source) {
409
+ if (source.startsWith("github:")) {
410
+ return "Set GITHUB_TOKEN or GH_TOKEN with repository read access, use `gh auth login` only as a local no-token fallback, or pass --catalog ./topograms.catalog.json.";
411
+ }
412
+ if (source.startsWith("http://") || source.startsWith("https://")) {
413
+ return "Check the catalog URL and token access, or pass --catalog ./topograms.catalog.json.";
414
+ }
415
+ return "Check the local catalog path and run `topogram catalog check <path>`.";
416
+ }
417
+
418
+ /**
419
+ * @param {ReturnType<typeof buildCatalogDoctorPayload>} payload
420
+ * @returns {void}
421
+ */
422
+ export function printCatalogDoctor(payload) {
423
+ console.log(payload.ok ? "Catalog doctor passed." : "Catalog doctor found issues.");
424
+ console.log(`Source: ${payload.source}`);
425
+ if (payload.source.startsWith("github:")) {
426
+ const tokenStatus = payload.auth.githubTokenEnv || payload.auth.ghTokenEnv ? "yes" : "no";
427
+ const ghStatus = payload.auth.ghCli.checked
428
+ ? `${payload.auth.ghCli.authenticated ? "authenticated" : "not authenticated"}${payload.auth.ghCli.reason ? ` (${payload.auth.ghCli.reason})` : ""}`
429
+ : "not checked";
430
+ console.log(`GitHub token env: ${tokenStatus}`);
431
+ console.log(`gh auth: ${ghStatus}`);
432
+ }
433
+ console.log(`Catalog reachable: ${payload.catalog.reachable ? "yes" : "no"}`);
434
+ if (payload.catalog.reachable) {
435
+ console.log(`Version: ${payload.catalog.version}`);
436
+ console.log(`Entries: ${payload.catalog.entries}`);
437
+ }
438
+ if (payload.packages.length > 0) {
439
+ console.log("Packages:");
440
+ for (const item of payload.packages) {
441
+ console.log(`- ${item.id} (${item.kind}): ${item.packageSpec} ${item.ok ? "ok" : "failed"}`);
442
+ for (const diagnostic of item.diagnostics) {
443
+ console.log(` Error: ${diagnostic.message}`);
444
+ if (diagnostic.suggestedFix) {
445
+ console.log(` Fix: ${diagnostic.suggestedFix}`);
446
+ }
447
+ }
448
+ }
449
+ }
450
+ const packageIds = new Set(payload.packages.map((item) => item.id));
451
+ for (const diagnostic of payload.diagnostics.filter((item) => !item.path || !packageIds.has(item.path))) {
452
+ const label = diagnostic.severity === "warning" ? "Warning" : "Error";
453
+ console.log(`${label}: ${diagnostic.message}`);
454
+ if (diagnostic.suggestedFix) {
455
+ console.log(`Fix: ${diagnostic.suggestedFix}`);
456
+ }
457
+ }
458
+ }
459
+
460
+ /**
461
+ * @param {string} source
462
+ * @returns {ReturnType<typeof checkCatalogSource>}
463
+ */
464
+ export function buildCatalogCheckPayload(source) {
465
+ if (!source) {
466
+ throw new Error("topogram catalog check requires <path-or-url>.");
467
+ }
468
+ return checkCatalogSource(source);
469
+ }
470
+
471
+ /**
472
+ * @param {ReturnType<typeof checkCatalogSource>} payload
473
+ * @returns {void}
474
+ */
475
+ export function printCatalogCheck(payload) {
476
+ console.log(payload.ok ? "Catalog check passed." : "Catalog check failed.");
477
+ console.log(`Source: ${payload.source}`);
478
+ if (payload.catalog) {
479
+ console.log(`Version: ${payload.catalog.version}`);
480
+ console.log(`Entries: ${payload.catalog.entries.length}`);
481
+ }
482
+ for (const diagnostic of payload.diagnostics) {
483
+ const label = diagnostic.severity === "warning" ? "Warning" : "Error";
484
+ console.log(`${label}: ${diagnostic.message}`);
485
+ }
486
+ }
487
+
488
+ /**
489
+ * @param {string} id
490
+ * @param {string} targetPath
491
+ * @param {{ source?: string|null, version?: string|null }} options
492
+ * @returns {{ ok: boolean, source: string, id: string, kind: "topogram", packageSpec: string, targetPath: string, provenancePath: string, files: string[], diagnostics: any[], errors: string[] }}
493
+ */
494
+ export function buildCatalogCopyPayload(id, targetPath, options) {
495
+ if (!id || id.startsWith("-")) {
496
+ throw new Error("topogram catalog copy requires <id>.");
497
+ }
498
+ if (!targetPath || targetPath.startsWith("-")) {
499
+ throw new Error("topogram catalog copy requires <target>.");
500
+ }
501
+ const loaded = loadCatalog(options.source || null);
502
+ const entry = findCatalogEntry(loaded.catalog, id, "topogram");
503
+ if (!entry) {
504
+ throw new Error(`Catalog topogram entry '${id}' was not found in ${loaded.source}.`);
505
+ }
506
+ const copied = copyCatalogTopogramEntry(entry, targetPath, {
507
+ catalogSource: loaded.source,
508
+ version: options.version || null
509
+ });
510
+ return {
511
+ source: loaded.source,
512
+ ...copied,
513
+ diagnostics: [],
514
+ errors: []
515
+ };
516
+ }
517
+
518
+ /**
519
+ * @param {ReturnType<typeof buildCatalogCopyPayload>} payload
520
+ * @returns {void}
521
+ */
522
+ export function printCatalogCopy(payload) {
523
+ console.log(`Copied catalog topogram '${payload.id}' to ${payload.targetPath}.`);
524
+ console.log(`Package: ${payload.packageSpec}`);
525
+ console.log(`Source provenance: ${payload.provenancePath}`);
526
+ console.log(`Files: ${payload.files.length}`);
527
+ console.log(`${TOPOGRAM_SOURCE_FILE} records catalog-copy provenance only. Local edits are allowed.`);
528
+ console.log("");
529
+ console.log("Next steps:");
530
+ console.log(` cd ${shellCommandArg(path.relative(process.cwd(), payload.targetPath) || ".")}`);
531
+ console.log(" topogram source status --local");
532
+ console.log(" topogram check");
533
+ console.log(" topogram generate");
534
+ }
535
+
536
+ /**
537
+ * @param {{
538
+ * commandArgs: Record<string, any>,
539
+ * inputPath: string|null|undefined,
540
+ * catalogSource: string|null,
541
+ * requestedVersion: string|null,
542
+ * json: boolean
543
+ * }} context
544
+ * @returns {number}
545
+ */
546
+ export function runCatalogCommand(context) {
547
+ const { commandArgs, inputPath, catalogSource, requestedVersion, json } = context;
548
+ if (commandArgs.catalogCommand === "list") {
549
+ const payload = buildCatalogListPayload(catalogSource || inputPath || null);
550
+ if (json) {
551
+ console.log(stableStringify(payload));
552
+ } else {
553
+ printCatalogList(payload);
554
+ }
555
+ return 0;
556
+ }
557
+
558
+ if (commandArgs.catalogCommand === "show") {
559
+ if (!inputPath) {
560
+ console.error("Missing required <id>.");
561
+ printCatalogHelp();
562
+ return 1;
563
+ }
564
+ const payload = buildCatalogShowPayload(inputPath, catalogSource);
565
+ if (json) {
566
+ console.log(stableStringify(payload));
567
+ } else {
568
+ printCatalogShow(payload);
569
+ }
570
+ return payload.ok ? 0 : 1;
571
+ }
572
+
573
+ if (commandArgs.catalogCommand === "doctor") {
574
+ const payload = buildCatalogDoctorPayload(catalogSource || inputPath || null);
575
+ if (json) {
576
+ console.log(stableStringify(payload));
577
+ } else {
578
+ printCatalogDoctor(payload);
579
+ }
580
+ return payload.ok ? 0 : 1;
581
+ }
582
+
583
+ if (commandArgs.catalogCommand === "check") {
584
+ if (!inputPath) {
585
+ console.error("Missing required <path-or-url>.");
586
+ printCatalogHelp();
587
+ return 1;
588
+ }
589
+ const payload = buildCatalogCheckPayload(inputPath);
590
+ if (json) {
591
+ console.log(stableStringify(payload));
592
+ } else {
593
+ printCatalogCheck(payload);
594
+ }
595
+ return payload.ok ? 0 : 1;
596
+ }
597
+
598
+ if (commandArgs.catalogCommand === "copy") {
599
+ if (!commandArgs.catalogId || !inputPath) {
600
+ console.error("Missing required <id> or <target>.");
601
+ printCatalogHelp();
602
+ return 1;
603
+ }
604
+ const payload = buildCatalogCopyPayload(commandArgs.catalogId, inputPath, {
605
+ source: catalogSource,
606
+ version: requestedVersion
607
+ });
608
+ if (json) {
609
+ console.log(stableStringify(payload));
610
+ } else {
611
+ printCatalogCopy(payload);
612
+ }
613
+ return 0;
614
+ }
615
+
616
+ throw new Error(`Unknown catalog command '${commandArgs.catalogCommand}'`);
617
+ }