@xano/cli 1.0.6-beta.1 → 1.0.6-beta.11
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/README.md +138 -4
- package/dist/commands/branch/list/index.js +5 -1
- package/dist/commands/ephemeral/impersonate/index.d.ts +1 -0
- package/dist/commands/ephemeral/impersonate/index.js +11 -3
- package/dist/commands/ephemeral/list/index.js +2 -1
- package/dist/commands/ephemeral/pull/index.d.ts +0 -6
- package/dist/commands/ephemeral/pull/index.js +10 -95
- package/dist/commands/ephemeral/push/index.d.ts +10 -0
- package/dist/commands/ephemeral/push/index.js +77 -0
- package/dist/commands/ephemeral/static_host/build/list/index.d.ts +2 -3
- package/dist/commands/ephemeral/static_host/build/list/index.js +19 -40
- package/dist/commands/ephemeral/static_host/list/index.d.ts +2 -3
- package/dist/commands/ephemeral/static_host/list/index.js +20 -40
- package/dist/commands/flatten/index.d.ts +34 -0
- package/dist/commands/flatten/index.js +140 -0
- package/dist/commands/function/list/index.d.ts +4 -4
- package/dist/commands/function/list/index.js +30 -38
- package/dist/commands/knowledge/list/index.js +2 -1
- package/dist/commands/platform/list/index.js +13 -14
- package/dist/commands/release/list/index.js +13 -14
- package/dist/commands/release/pull/index.d.ts +0 -6
- package/dist/commands/release/pull/index.js +10 -95
- package/dist/commands/sandbox/env/list/index.js +4 -0
- package/dist/commands/sandbox/pull/index.d.ts +0 -1
- package/dist/commands/sandbox/pull/index.js +10 -77
- package/dist/commands/sandbox/unit_test/list/index.d.ts +2 -0
- package/dist/commands/sandbox/unit_test/list/index.js +9 -14
- package/dist/commands/sandbox/unit_test/run/index.js +17 -11
- package/dist/commands/sandbox/unit_test/run_all/index.d.ts +1 -0
- package/dist/commands/sandbox/unit_test/run_all/index.js +76 -38
- package/dist/commands/sandbox/workflow_test/list/index.d.ts +2 -0
- package/dist/commands/sandbox/workflow_test/list/index.js +9 -14
- package/dist/commands/sandbox/workflow_test/run/index.js +12 -9
- package/dist/commands/sandbox/workflow_test/run_all/index.d.ts +1 -0
- package/dist/commands/sandbox/workflow_test/run_all/index.js +74 -36
- package/dist/commands/static_host/build/list/index.d.ts +2 -3
- package/dist/commands/static_host/build/list/index.js +19 -40
- package/dist/commands/static_host/build/pull/index.js +12 -9
- package/dist/commands/static_host/list/index.d.ts +2 -3
- package/dist/commands/static_host/list/index.js +20 -40
- package/dist/commands/static_host/migrate/index.js +14 -10
- package/dist/commands/tenant/backup/list/index.js +13 -14
- package/dist/commands/tenant/cluster/list/index.js +9 -10
- package/dist/commands/tenant/env/list/index.js +4 -0
- package/dist/commands/tenant/list/index.js +19 -20
- package/dist/commands/tenant/pull/index.d.ts +0 -6
- package/dist/commands/tenant/pull/index.js +10 -95
- package/dist/commands/tenant/snapshot/list/index.js +5 -1
- package/dist/commands/tenant/unit_test/list/index.d.ts +4 -2
- package/dist/commands/tenant/unit_test/list/index.js +9 -14
- package/dist/commands/tenant/unit_test/run/index.js +19 -11
- package/dist/commands/tenant/unit_test/run_all/index.d.ts +1 -0
- package/dist/commands/tenant/unit_test/run_all/index.js +80 -38
- package/dist/commands/tenant/workflow_test/list/index.d.ts +4 -2
- package/dist/commands/tenant/workflow_test/list/index.js +9 -14
- package/dist/commands/tenant/workflow_test/run/index.js +14 -9
- package/dist/commands/tenant/workflow_test/run_all/index.d.ts +1 -0
- package/dist/commands/tenant/workflow_test/run_all/index.js +78 -36
- package/dist/commands/unit_test/list/index.d.ts +3 -1
- package/dist/commands/unit_test/list/index.js +9 -14
- package/dist/commands/unit_test/run/index.js +19 -11
- package/dist/commands/unit_test/run_all/index.d.ts +1 -0
- package/dist/commands/unit_test/run_all/index.js +81 -41
- package/dist/commands/workflow_test/list/index.d.ts +3 -1
- package/dist/commands/workflow_test/list/index.js +10 -13
- package/dist/commands/workflow_test/run/index.js +15 -10
- package/dist/commands/workflow_test/run_all/index.d.ts +1 -0
- package/dist/commands/workflow_test/run_all/index.js +82 -39
- package/dist/commands/workspace/git/pull/index.js +53 -3
- package/dist/commands/workspace/list/index.js +7 -13
- package/dist/commands/workspace/pull/index.d.ts +0 -6
- package/dist/commands/workspace/pull/index.js +10 -95
- package/dist/utils/concurrency.d.ts +29 -0
- package/dist/utils/concurrency.js +54 -0
- package/dist/utils/document-parser.d.ts +116 -0
- package/dist/utils/document-parser.js +274 -1
- package/dist/utils/flatten.d.ts +36 -0
- package/dist/utils/flatten.js +68 -0
- package/dist/utils/knowledge-sync.d.ts +1 -0
- package/dist/utils/knowledge-sync.js +11 -5
- package/dist/utils/microservice-wait.d.ts +53 -0
- package/dist/utils/microservice-wait.js +83 -0
- package/dist/utils/multidoc-push.d.ts +12 -0
- package/dist/utils/multidoc-push.js +84 -0
- package/dist/utils/paging.d.ts +145 -0
- package/dist/utils/paging.js +199 -0
- package/dist/utils/reference-checker.js +13 -0
- package/oclif.manifest.json +3420 -3208
- package/package.json +1 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Poll a tenant's microservice deployment status until every auto-deployed
|
|
3
|
+
* microservice settles (ok/error) or a timeout elapses. Backs `ephemeral push
|
|
4
|
+
* --wait`: after a multidoc push auto-deploys microservices (tenant_deploy=
|
|
5
|
+
* "auto"), the CLI waits here for them to become Ready and reports the outcome.
|
|
6
|
+
*
|
|
7
|
+
* The backend route (GET .../tenant/{name}/microservice, statement
|
|
8
|
+
* mvp:microservice_status_all) returns one entry per microservice with a live
|
|
9
|
+
* k8s readiness read. `manual`/`disabled` microservices are reported but not
|
|
10
|
+
* waited on (they were never deployed by the push).
|
|
11
|
+
*/
|
|
12
|
+
/** Statuses that mean an awaited microservice has settled (no longer in flight). */
|
|
13
|
+
const SETTLED = new Set(['ok', 'error']);
|
|
14
|
+
/** Statuses that mean we don't wait on this microservice at all. */
|
|
15
|
+
const NOT_AWAITED = new Set(['skipped', 'disabled']);
|
|
16
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
17
|
+
/** An awaited microservice is one that was auto-deployed and is not skipped/disabled. */
|
|
18
|
+
export function isAwaited(entry) {
|
|
19
|
+
return !NOT_AWAITED.has(entry.status) && (entry.tenant_deploy ?? 'auto') !== 'manual';
|
|
20
|
+
}
|
|
21
|
+
/** True once every awaited microservice has settled (ok/error). */
|
|
22
|
+
function allSettled(entries) {
|
|
23
|
+
const awaited = entries.filter((e) => isAwaited(e));
|
|
24
|
+
// No auto microservices at all → nothing to wait for.
|
|
25
|
+
if (awaited.length === 0)
|
|
26
|
+
return true;
|
|
27
|
+
return awaited.every((e) => SETTLED.has(e.status));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fetch the status route once. Throws on a non-2xx so the caller can decide
|
|
31
|
+
* whether a transient failure is fatal (it isn't — we keep polling).
|
|
32
|
+
*/
|
|
33
|
+
async function fetchStatus(opts) {
|
|
34
|
+
const res = await opts.verboseFetch(opts.statusUrl, { headers: { Authorization: `Bearer ${opts.accessToken}` }, method: 'GET' }, opts.verbose ?? false, opts.accessToken);
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
throw new Error(`status ${res.status} ${res.statusText}`);
|
|
37
|
+
}
|
|
38
|
+
const body = (await res.json());
|
|
39
|
+
if (!Array.isArray(body)) {
|
|
40
|
+
throw new Error('unexpected status response shape');
|
|
41
|
+
}
|
|
42
|
+
return body;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Poll until all awaited microservices settle or the timeout elapses. Transient
|
|
46
|
+
* poll failures (network blips, a brief 5xx while a pod restarts) are tolerated —
|
|
47
|
+
* they don't abort the wait; only the overall timeout does.
|
|
48
|
+
*/
|
|
49
|
+
export async function waitForMicroservices(opts) {
|
|
50
|
+
const pollIntervalMs = opts.pollIntervalMs ?? 2000;
|
|
51
|
+
const timeoutMs = opts.timeoutMs ?? 300000;
|
|
52
|
+
const start = Date.now();
|
|
53
|
+
let entries = [];
|
|
54
|
+
let consecutiveErrors = 0;
|
|
55
|
+
// Poll loop. We always do at least one fetch so `--wait` reports even when the
|
|
56
|
+
// deployment already settled between the push returning and the first poll.
|
|
57
|
+
for (;;) {
|
|
58
|
+
const elapsed = Date.now() - start;
|
|
59
|
+
try {
|
|
60
|
+
entries = await fetchStatus(opts);
|
|
61
|
+
consecutiveErrors = 0;
|
|
62
|
+
opts.onPoll?.(entries, elapsed);
|
|
63
|
+
if (allSettled(entries)) {
|
|
64
|
+
return { entries, hadError: entries.some((e) => isAwaited(e) && e.status === 'error'), timedOut: false };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// A transient status read failure is non-fatal — keep polling until the
|
|
69
|
+
// overall timeout. (Mirrors the frontend poller's tolerate-with-cap.) We
|
|
70
|
+
// don't surface each blip; the timeout is the real failure signal.
|
|
71
|
+
consecutiveErrors++;
|
|
72
|
+
}
|
|
73
|
+
if (Date.now() - start + pollIntervalMs > timeoutMs) {
|
|
74
|
+
// Out of budget. Report whatever we last saw as timed out.
|
|
75
|
+
return {
|
|
76
|
+
entries,
|
|
77
|
+
hadError: entries.some((e) => isAwaited(e) && e.status === 'error'),
|
|
78
|
+
timedOut: true,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
await sleep(pollIntervalMs);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -97,6 +97,18 @@ export declare function readDocuments(files: string[]): Array<{
|
|
|
97
97
|
content: string;
|
|
98
98
|
filePath: string;
|
|
99
99
|
}>;
|
|
100
|
+
/**
|
|
101
|
+
* Return the entries whose file holds more than one document (a `---`
|
|
102
|
+
* separator). Used to refuse multi-doc files before the push pipeline's
|
|
103
|
+
* single-doc-per-file assumptions can drop or corrupt them.
|
|
104
|
+
*/
|
|
105
|
+
export declare function findMultiDocEntries(entries: Array<{
|
|
106
|
+
content: string;
|
|
107
|
+
filePath: string;
|
|
108
|
+
}>): Array<{
|
|
109
|
+
count: number;
|
|
110
|
+
filePath: string;
|
|
111
|
+
}>;
|
|
100
112
|
export declare function renderBadReferences(badRefs: BadReference[], log: (msg: string) => void): void;
|
|
101
113
|
export declare function renderBadIndexes(badIndexes: BadIndex[], log: (msg: string) => void): void;
|
|
102
114
|
export declare function confirm(message: string): Promise<boolean>;
|
|
@@ -3,6 +3,7 @@ import { minimatch } from 'minimatch';
|
|
|
3
3
|
import * as fs from 'node:fs';
|
|
4
4
|
import { join, relative } from 'node:path';
|
|
5
5
|
import { buildDocumentKey, findFilesWithGuid, parseDocument } from './document-parser.js';
|
|
6
|
+
import { flattenBundleFile } from './flatten.js';
|
|
6
7
|
import { collectKnowledgeObjects, fetchKnowledge, knowledgePreview, pushKnowledge, syncGuidToFrontmatter, toPushItems, } from './knowledge-sync.js';
|
|
7
8
|
import { checkReferences, checkTableIndexes } from './reference-checker.js';
|
|
8
9
|
/**
|
|
@@ -102,6 +103,31 @@ export function readDocuments(files) {
|
|
|
102
103
|
}
|
|
103
104
|
return entries;
|
|
104
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* A `.xs` document separator: a line that is exactly `---`. The whole push
|
|
108
|
+
* pipeline (partial-diff filtering, the document→file map, and GUID writeback)
|
|
109
|
+
* assumes ONE document per file — it only ever parses the first document in a
|
|
110
|
+
* file and rewrites the first `guid =` line. A multi-document file therefore
|
|
111
|
+
* silently drops on a partial push and corrupts GUIDs on a full one, so it is
|
|
112
|
+
* refused up front (with a pointer to `xano flatten`).
|
|
113
|
+
*/
|
|
114
|
+
const DOC_SEPARATOR = /^---$/m;
|
|
115
|
+
/**
|
|
116
|
+
* Return the entries whose file holds more than one document (a `---`
|
|
117
|
+
* separator). Used to refuse multi-doc files before the push pipeline's
|
|
118
|
+
* single-doc-per-file assumptions can drop or corrupt them.
|
|
119
|
+
*/
|
|
120
|
+
export function findMultiDocEntries(entries) {
|
|
121
|
+
const offenders = [];
|
|
122
|
+
for (const entry of entries) {
|
|
123
|
+
if (DOC_SEPARATOR.test(entry.content)) {
|
|
124
|
+
// Count documents = separators + 1 (trailing/empty fragments don't change the "multi" verdict).
|
|
125
|
+
const count = entry.content.split('\n').filter((l) => l.trim() === '---').length + 1;
|
|
126
|
+
offenders.push({ count, filePath: entry.filePath });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return offenders;
|
|
130
|
+
}
|
|
105
131
|
// ── Validation Rendering ────────────────────────────────────────────────────
|
|
106
132
|
export function renderBadReferences(badRefs, log) {
|
|
107
133
|
log(ux.colorize('yellow', ux.colorize('bold', '=== Unresolved References ===')));
|
|
@@ -387,6 +413,64 @@ export async function executePush(ctx, target, flags) {
|
|
|
387
413
|
if (documentEntries.length === 0) {
|
|
388
414
|
command.error(`All .xs files in ${inputDir} are empty`);
|
|
389
415
|
}
|
|
416
|
+
// ── Handle multi-document files ───────────────────────────────────────
|
|
417
|
+
// The push pipeline assumes one document per file: the partial-diff filter
|
|
418
|
+
// and GUID-writeback both parse only the first document in a file, so a
|
|
419
|
+
// bundle like a single hand-authored multidoc.xs silently pushes nothing
|
|
420
|
+
// (partial) or clobbers a single guid line across every document (full).
|
|
421
|
+
// Rather than fail, offer to flatten each bundle in place into the
|
|
422
|
+
// per-document layout `pull` produces, then re-read and continue the push.
|
|
423
|
+
const multiDocFiles = findMultiDocEntries(documentEntries);
|
|
424
|
+
if (multiDocFiles.length > 0) {
|
|
425
|
+
const list = multiDocFiles
|
|
426
|
+
.map((f) => ` ${relative(inputDir, f.filePath) || f.filePath} (${f.count} documents)`)
|
|
427
|
+
.join('\n');
|
|
428
|
+
log('');
|
|
429
|
+
log(ux.colorize('yellow', ux.colorize('bold', '=== Multi-document file(s) detected ===')));
|
|
430
|
+
log('');
|
|
431
|
+
log(ux.colorize('yellow', 'These .xs files hold more than one document:'));
|
|
432
|
+
log(list);
|
|
433
|
+
log('');
|
|
434
|
+
log(ux.colorize('dim', 'Push needs one document per file (partial diff + GUID writeback operate per file),\n' +
|
|
435
|
+
'so a multi-doc bundle silently pushes nothing or corrupts GUIDs. I can split each\n' +
|
|
436
|
+
'into the standard per-document layout (the same tree `pull` produces) and continue.'));
|
|
437
|
+
log('');
|
|
438
|
+
// Flattening DELETES the source bundle, so never do it unattended without an
|
|
439
|
+
// explicit opt-in. --force means "just do it"; an interactive TTY gets a
|
|
440
|
+
// prompt; a bare non-interactive run refuses (rather than silently rewriting
|
|
441
|
+
// and deleting the user's files in CI).
|
|
442
|
+
if (flags.force) {
|
|
443
|
+
log(ux.colorize('dim', 'Flattening (--force)…'));
|
|
444
|
+
}
|
|
445
|
+
else if (process.stdin.isTTY) {
|
|
446
|
+
const doFlatten = await confirm('Flatten these file(s) in place and continue the push?');
|
|
447
|
+
if (!doFlatten) {
|
|
448
|
+
log('Push cancelled. Run `xano flatten <file>` yourself, or re-run to be prompted again.');
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
command.error('Multi-document .xs file(s) cannot be pushed. Run `xano flatten <file>` to split them ' +
|
|
454
|
+
'into the standard per-document layout first, or re-run with --force to flatten automatically.');
|
|
455
|
+
}
|
|
456
|
+
for (const offender of multiDocFiles) {
|
|
457
|
+
try {
|
|
458
|
+
const result = flattenBundleFile(offender.filePath, { log: (m) => flags.verbose && log(ux.colorize('dim', m)) });
|
|
459
|
+
log(ux.colorize('dim', ` flattened ${relative(inputDir, offender.filePath) || offender.filePath} → ${result.written.length} files`));
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
command.error(`Failed to flatten ${offender.filePath}: ${error.message}`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
log('');
|
|
466
|
+
// Re-collect and re-read: the bundle files are gone, replaced by the split
|
|
467
|
+
// per-document tree, so the rest of the push sees a clean one-doc-per-file set.
|
|
468
|
+
const refreshed = readDocuments(applyFilters(collectFiles(inputDir), inputDir, flags.include, flags.exclude, () => { }));
|
|
469
|
+
documentEntries = refreshed;
|
|
470
|
+
if (documentEntries.length === 0) {
|
|
471
|
+
command.error('After flattening, no .xs documents remain to push.');
|
|
472
|
+
}
|
|
473
|
+
}
|
|
390
474
|
multidoc = documentEntries.map((d) => d.content).join('\n---\n');
|
|
391
475
|
// ── Build document key → file path map (for GUID writeback) ─────────
|
|
392
476
|
for (const entry of documentEntries) {
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How much paging information the backing Metadata API endpoint gives us.
|
|
3
|
+
*
|
|
4
|
+
* The Metadata API pages unevenly, and what the CLI can honestly tell the user
|
|
5
|
+
* depends entirely on what the response carries:
|
|
6
|
+
*
|
|
7
|
+
* - `envelope` accepts page + per_page and returns
|
|
8
|
+
* {curPage, nextPage, prevPage, items[]}. `nextPage` is
|
|
9
|
+
* server-computed and null on the last page, so it is the
|
|
10
|
+
* one place a "there is more" hint is guaranteed correct.
|
|
11
|
+
* - `page-only-envelope` accepts page only (per_page is hardcoded server-side)
|
|
12
|
+
* and returns an object carrying `itemsTotal`. A true
|
|
13
|
+
* total, so we can say where the user sits — but we offer
|
|
14
|
+
* no next-page affordance.
|
|
15
|
+
* - `none` no server-side paging at all. The full set comes back,
|
|
16
|
+
* so the item count IS the total.
|
|
17
|
+
*
|
|
18
|
+
* There is deliberately no tier for endpoints that page but return a bare array
|
|
19
|
+
* with no metadata (tenants, releases, platforms, clusters, ephemerals, tenant
|
|
20
|
+
* backups). Those commands are left alone: the only has-more signal available
|
|
21
|
+
* would be "the page came back full", which is a guaranteed false positive when
|
|
22
|
+
* the result set is an exact multiple of the page size — sending the user to an
|
|
23
|
+
* empty page. Better to offer nothing than to offer a lie.
|
|
24
|
+
*/
|
|
25
|
+
export type PagingTier = 'envelope' | 'none' | 'page-only-envelope';
|
|
26
|
+
export interface PagingFlagValues {
|
|
27
|
+
page?: number;
|
|
28
|
+
per_page?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface NormalizedList<T> {
|
|
31
|
+
curPage?: number;
|
|
32
|
+
items: T[];
|
|
33
|
+
itemsTotal?: number;
|
|
34
|
+
nextPage?: number;
|
|
35
|
+
prevPage?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface PagingFlagOptions {
|
|
38
|
+
/** Page size the endpoint hardcodes server-side; surfaced in the page flag description. */
|
|
39
|
+
fixedPerPage?: number;
|
|
40
|
+
/** Maximum per_page the endpoint accepts; surfaced in the per_page flag description. */
|
|
41
|
+
maxPerPage?: number;
|
|
42
|
+
}
|
|
43
|
+
declare function makePageFlag(description: string): import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
|
|
44
|
+
declare function makePerPageFlag(defaultValue: number, description: string): import("@oclif/core/interfaces").OptionFlag<number, import("@oclif/core/interfaces").CustomOptions>;
|
|
45
|
+
type PageFlag = ReturnType<typeof makePageFlag>;
|
|
46
|
+
type PerPageFlag = ReturnType<typeof makePerPageFlag>;
|
|
47
|
+
/**
|
|
48
|
+
* Build the oclif flag definitions appropriate to an endpoint's paging tier.
|
|
49
|
+
* Spread the result into a command's `static flags`.
|
|
50
|
+
*
|
|
51
|
+
* A flag the server ignores is worse than no flag, so `per_page` is only
|
|
52
|
+
* produced for tiers whose endpoint actually accepts it. The overloads keep
|
|
53
|
+
* oclif's flag-type inference intact so `flags.page` stays typed at call sites.
|
|
54
|
+
*/
|
|
55
|
+
export declare function pagingFlags(tier: 'envelope', options?: PagingFlagOptions): {
|
|
56
|
+
page: PageFlag;
|
|
57
|
+
per_page: PerPageFlag;
|
|
58
|
+
};
|
|
59
|
+
export declare function pagingFlags(tier: 'page-only-envelope', options?: PagingFlagOptions): {
|
|
60
|
+
page: PageFlag;
|
|
61
|
+
};
|
|
62
|
+
export declare function pagingFlags(tier: 'none', options?: PagingFlagOptions): Record<string, never>;
|
|
63
|
+
export declare function pagingFlags(tier: PagingTier, options?: PagingFlagOptions): Record<string, unknown>;
|
|
64
|
+
/**
|
|
65
|
+
* Translate parsed paging flags into query-string entries, emitting only the
|
|
66
|
+
* params the endpoint actually declares.
|
|
67
|
+
*/
|
|
68
|
+
export declare function buildPagingParams(flags: PagingFlagValues | Record<string, unknown>, tier: PagingTier): Array<[string, string]>;
|
|
69
|
+
/**
|
|
70
|
+
* Absorb the response shapes the Metadata API returns for list endpoints:
|
|
71
|
+
* a bare array, a paging envelope keyed on `items`, or an object keyed on the
|
|
72
|
+
* resource name (e.g. `{workspaces: [...]}`).
|
|
73
|
+
*
|
|
74
|
+
* `resourceKeys` names any resource-keyed fallbacks to check, in order.
|
|
75
|
+
*/
|
|
76
|
+
export declare function normalizeListResponse<T>(data: unknown, resourceKeys?: string[]): NormalizedList<T>;
|
|
77
|
+
/** Page size used when the CLI pages an endpoint on the user's behalf. */
|
|
78
|
+
export declare const INTERNAL_PAGE_SIZE = 100;
|
|
79
|
+
/**
|
|
80
|
+
* Walk an envelope-tier endpoint page by page and collect every item.
|
|
81
|
+
*
|
|
82
|
+
* Termination is the server's own `nextPage`, never "the page came back full" —
|
|
83
|
+
* the same rule the footer follows. An empty page also stops the walk, which
|
|
84
|
+
* covers an endpoint that omits `nextPage` entirely rather than looping forever.
|
|
85
|
+
*
|
|
86
|
+
* `maxPages` is a runaway backstop, not a limit users are expected to hit; when
|
|
87
|
+
* it trips, `onTruncate` fires so the caller can say so out loud instead of
|
|
88
|
+
* silently processing a subset.
|
|
89
|
+
*/
|
|
90
|
+
export declare function collectAllPages<T>(fetchPage: (page: number) => Promise<NormalizedList<T>>, options?: {
|
|
91
|
+
maxPages?: number;
|
|
92
|
+
onTruncate?: (collected: number) => void;
|
|
93
|
+
}): Promise<T[]>;
|
|
94
|
+
export interface PagingJson<T> {
|
|
95
|
+
curPage?: number;
|
|
96
|
+
items: T[];
|
|
97
|
+
itemsTotal?: number;
|
|
98
|
+
nextPage?: number;
|
|
99
|
+
perPage?: number;
|
|
100
|
+
prevPage?: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Build the `--output json` payload for a list command.
|
|
104
|
+
*
|
|
105
|
+
* Field names mirror the Metadata API envelope exactly — `curPage`, `nextPage`,
|
|
106
|
+
* `prevPage`, `itemsTotal`, `items` — so a script can move between the CLI and
|
|
107
|
+
* the raw API without a translation layer. `perPage` is the one field the CLI
|
|
108
|
+
* injects: the API never echoes it back, and it is otherwise invisible when the
|
|
109
|
+
* user leaves the flag at its default.
|
|
110
|
+
*
|
|
111
|
+
* Emits only fields the response actually proves, mirroring the footer's
|
|
112
|
+
* honesty rule: `itemsTotal` appears only when the server sent it, `nextPage`
|
|
113
|
+
* only when the server computed it.
|
|
114
|
+
*
|
|
115
|
+
* Without this envelope, a script driving `--output json` sees a bare array and
|
|
116
|
+
* its only available stop condition is `length < perPage` — precisely the
|
|
117
|
+
* page-fullness inference this module refuses to make in the footer.
|
|
118
|
+
*/
|
|
119
|
+
export declare function buildPagingJson<T>(list: NormalizedList<T>, options: {
|
|
120
|
+
page?: number;
|
|
121
|
+
perPage?: number;
|
|
122
|
+
tier: PagingTier;
|
|
123
|
+
}): PagingJson<T>;
|
|
124
|
+
export interface PagingFooterOptions {
|
|
125
|
+
/** Singular noun for the listed resource, used by the `none` tier. */
|
|
126
|
+
noun: string;
|
|
127
|
+
/** Plural form; defaults to `noun + 's'`. */
|
|
128
|
+
nounPlural?: string;
|
|
129
|
+
/** Page the user requested, used when the response omits `curPage`. */
|
|
130
|
+
page?: number;
|
|
131
|
+
/** Page size the user requested. Recorded for context; never used to infer a next page. */
|
|
132
|
+
perPage?: number;
|
|
133
|
+
tier: PagingTier;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Render the paging footer for a list command, reporting only facts the response
|
|
137
|
+
* actually proves. Returns null when there is nothing worth saying — an empty
|
|
138
|
+
* result set already prints its own "No X found" line.
|
|
139
|
+
*
|
|
140
|
+
* Deliberately asymmetric across tiers: `envelope` shows a next-page hint but no
|
|
141
|
+
* total, `page-only-envelope` shows a total but no next-page hint, and `none`
|
|
142
|
+
* shows a bare count. See PagingTier for why.
|
|
143
|
+
*/
|
|
144
|
+
export declare function formatPagingFooter<T>(list: NormalizedList<T>, options: PagingFooterOptions): null | string;
|
|
145
|
+
export {};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import { Flags } from '@oclif/core';
|
|
3
|
+
function makePageFlag(description) {
|
|
4
|
+
return Flags.integer({ default: 1, description, required: false });
|
|
5
|
+
}
|
|
6
|
+
function makePerPageFlag(defaultValue, description) {
|
|
7
|
+
return Flags.integer({ default: defaultValue, description, required: false });
|
|
8
|
+
}
|
|
9
|
+
export function pagingFlags(tier, options = {}) {
|
|
10
|
+
if (tier === 'none') {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
if (tier === 'page-only-envelope') {
|
|
14
|
+
const fixed = options.fixedPerPage;
|
|
15
|
+
return {
|
|
16
|
+
page: makePageFlag(fixed
|
|
17
|
+
? `Page number for pagination (page size is fixed at ${fixed} by the API)`
|
|
18
|
+
: 'Page number for pagination'),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
page: makePageFlag('Page number for pagination'),
|
|
23
|
+
per_page: makePerPageFlag(50, options.maxPerPage
|
|
24
|
+
? `Number of results per page (max ${options.maxPerPage})`
|
|
25
|
+
: 'Number of results per page'),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Translate parsed paging flags into query-string entries, emitting only the
|
|
30
|
+
* params the endpoint actually declares.
|
|
31
|
+
*/
|
|
32
|
+
export function buildPagingParams(flags, tier) {
|
|
33
|
+
if (tier === 'none') {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const { page, per_page: perPage } = flags;
|
|
37
|
+
const params = [];
|
|
38
|
+
if (page !== undefined) {
|
|
39
|
+
params.push(['page', String(page)]);
|
|
40
|
+
}
|
|
41
|
+
if (tier === 'envelope' && perPage !== undefined) {
|
|
42
|
+
params.push(['per_page', String(perPage)]);
|
|
43
|
+
}
|
|
44
|
+
return params;
|
|
45
|
+
}
|
|
46
|
+
/** Coerce a possibly-null numeric paging field to a number or undefined. */
|
|
47
|
+
function optionalNumber(value) {
|
|
48
|
+
return typeof value === 'number' ? value : undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Absorb the response shapes the Metadata API returns for list endpoints:
|
|
52
|
+
* a bare array, a paging envelope keyed on `items`, or an object keyed on the
|
|
53
|
+
* resource name (e.g. `{workspaces: [...]}`).
|
|
54
|
+
*
|
|
55
|
+
* `resourceKeys` names any resource-keyed fallbacks to check, in order.
|
|
56
|
+
*/
|
|
57
|
+
export function normalizeListResponse(data, resourceKeys = []) {
|
|
58
|
+
if (Array.isArray(data)) {
|
|
59
|
+
return { items: data };
|
|
60
|
+
}
|
|
61
|
+
if (data && typeof data === 'object') {
|
|
62
|
+
const record = data;
|
|
63
|
+
if (Array.isArray(record.items)) {
|
|
64
|
+
return {
|
|
65
|
+
curPage: optionalNumber(record.curPage),
|
|
66
|
+
items: record.items,
|
|
67
|
+
itemsTotal: optionalNumber(record.itemsTotal),
|
|
68
|
+
nextPage: optionalNumber(record.nextPage),
|
|
69
|
+
prevPage: optionalNumber(record.prevPage),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
for (const key of resourceKeys) {
|
|
73
|
+
if (Array.isArray(record[key])) {
|
|
74
|
+
return {
|
|
75
|
+
curPage: optionalNumber(record.curPage),
|
|
76
|
+
items: record[key],
|
|
77
|
+
itemsTotal: optionalNumber(record.itemsTotal),
|
|
78
|
+
nextPage: optionalNumber(record.nextPage),
|
|
79
|
+
prevPage: optionalNumber(record.prevPage),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const accepted = ['an array', 'an object with an items array', ...resourceKeys.map((k) => `an object with a ${k} array`)];
|
|
85
|
+
throw new Error(`Unexpected API response format. Expected ${accepted.join(', ')}.`);
|
|
86
|
+
}
|
|
87
|
+
/** Page size used when the CLI pages an endpoint on the user's behalf. */
|
|
88
|
+
export const INTERNAL_PAGE_SIZE = 100;
|
|
89
|
+
/**
|
|
90
|
+
* Walk an envelope-tier endpoint page by page and collect every item.
|
|
91
|
+
*
|
|
92
|
+
* Termination is the server's own `nextPage`, never "the page came back full" —
|
|
93
|
+
* the same rule the footer follows. An empty page also stops the walk, which
|
|
94
|
+
* covers an endpoint that omits `nextPage` entirely rather than looping forever.
|
|
95
|
+
*
|
|
96
|
+
* `maxPages` is a runaway backstop, not a limit users are expected to hit; when
|
|
97
|
+
* it trips, `onTruncate` fires so the caller can say so out loud instead of
|
|
98
|
+
* silently processing a subset.
|
|
99
|
+
*/
|
|
100
|
+
export async function collectAllPages(fetchPage, options = {}) {
|
|
101
|
+
const maxPages = options.maxPages ?? 100;
|
|
102
|
+
const items = [];
|
|
103
|
+
for (let page = 1; page <= maxPages; page++) {
|
|
104
|
+
// eslint-disable-next-line no-await-in-loop
|
|
105
|
+
const list = await fetchPage(page);
|
|
106
|
+
items.push(...list.items);
|
|
107
|
+
if (list.items.length === 0 || list.nextPage === undefined) {
|
|
108
|
+
return items;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
options.onTruncate?.(items.length);
|
|
112
|
+
return items;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Build the `--output json` payload for a list command.
|
|
116
|
+
*
|
|
117
|
+
* Field names mirror the Metadata API envelope exactly — `curPage`, `nextPage`,
|
|
118
|
+
* `prevPage`, `itemsTotal`, `items` — so a script can move between the CLI and
|
|
119
|
+
* the raw API without a translation layer. `perPage` is the one field the CLI
|
|
120
|
+
* injects: the API never echoes it back, and it is otherwise invisible when the
|
|
121
|
+
* user leaves the flag at its default.
|
|
122
|
+
*
|
|
123
|
+
* Emits only fields the response actually proves, mirroring the footer's
|
|
124
|
+
* honesty rule: `itemsTotal` appears only when the server sent it, `nextPage`
|
|
125
|
+
* only when the server computed it.
|
|
126
|
+
*
|
|
127
|
+
* Without this envelope, a script driving `--output json` sees a bare array and
|
|
128
|
+
* its only available stop condition is `length < perPage` — precisely the
|
|
129
|
+
* page-fullness inference this module refuses to make in the footer.
|
|
130
|
+
*/
|
|
131
|
+
export function buildPagingJson(list, options) {
|
|
132
|
+
const payload = { items: list.items };
|
|
133
|
+
if (options.tier === 'none') {
|
|
134
|
+
return payload;
|
|
135
|
+
}
|
|
136
|
+
const hasPositionEvidence = list.curPage !== undefined ||
|
|
137
|
+
list.nextPage !== undefined ||
|
|
138
|
+
list.prevPage !== undefined ||
|
|
139
|
+
list.itemsTotal !== undefined;
|
|
140
|
+
// Same rule as the footer: with no metadata we cannot assert a position.
|
|
141
|
+
if (!hasPositionEvidence) {
|
|
142
|
+
return payload;
|
|
143
|
+
}
|
|
144
|
+
const curPage = list.curPage ?? options.page;
|
|
145
|
+
if (curPage !== undefined)
|
|
146
|
+
payload.curPage = curPage;
|
|
147
|
+
if (options.tier === 'envelope' && options.perPage !== undefined)
|
|
148
|
+
payload.perPage = options.perPage;
|
|
149
|
+
if (list.nextPage !== undefined)
|
|
150
|
+
payload.nextPage = list.nextPage;
|
|
151
|
+
if (list.prevPage !== undefined)
|
|
152
|
+
payload.prevPage = list.prevPage;
|
|
153
|
+
if (list.itemsTotal !== undefined)
|
|
154
|
+
payload.itemsTotal = list.itemsTotal;
|
|
155
|
+
return payload;
|
|
156
|
+
}
|
|
157
|
+
const SEPARATOR = ' · ';
|
|
158
|
+
/**
|
|
159
|
+
* Render the paging footer for a list command, reporting only facts the response
|
|
160
|
+
* actually proves. Returns null when there is nothing worth saying — an empty
|
|
161
|
+
* result set already prints its own "No X found" line.
|
|
162
|
+
*
|
|
163
|
+
* Deliberately asymmetric across tiers: `envelope` shows a next-page hint but no
|
|
164
|
+
* total, `page-only-envelope` shows a total but no next-page hint, and `none`
|
|
165
|
+
* shows a bare count. See PagingTier for why.
|
|
166
|
+
*/
|
|
167
|
+
export function formatPagingFooter(list, options) {
|
|
168
|
+
const count = list.items.length;
|
|
169
|
+
const plural = options.nounPlural ?? `${options.noun}s`;
|
|
170
|
+
const countPhrase = `${count} ${count === 1 ? options.noun : plural}`;
|
|
171
|
+
// A paged tier whose response carried no metadata at all tells us nothing
|
|
172
|
+
// about position — the endpoint may have ignored `page` entirely. Reporting
|
|
173
|
+
// the requested page here would state the user's wish as a server fact, so
|
|
174
|
+
// fall back to the bare count.
|
|
175
|
+
const hasPositionEvidence = list.curPage !== undefined ||
|
|
176
|
+
list.nextPage !== undefined ||
|
|
177
|
+
list.prevPage !== undefined ||
|
|
178
|
+
list.itemsTotal !== undefined;
|
|
179
|
+
if (options.tier === 'none' || !hasPositionEvidence) {
|
|
180
|
+
return count === 0 ? null : countPhrase;
|
|
181
|
+
}
|
|
182
|
+
// An empty page past the end is worth reporting when we know the true total —
|
|
183
|
+
// otherwise `--page 99` on a populated workspace is indistinguishable from an
|
|
184
|
+
// empty one.
|
|
185
|
+
if (count === 0 && list.itemsTotal === undefined) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const page = list.curPage ?? options.page ?? 1;
|
|
189
|
+
const parts = [`Page ${page}`, `${count} shown`];
|
|
190
|
+
if (options.tier === 'envelope' && list.nextPage !== undefined) {
|
|
191
|
+
// Server-computed. Null/absent on the last page, so following it can never
|
|
192
|
+
// land on an empty result. Page fullness is never consulted here.
|
|
193
|
+
parts.push(`next: --page ${list.nextPage}`);
|
|
194
|
+
}
|
|
195
|
+
if (options.tier === 'page-only-envelope' && list.itemsTotal !== undefined) {
|
|
196
|
+
parts.push(`${list.itemsTotal} total`);
|
|
197
|
+
}
|
|
198
|
+
return parts.join(SEPARATOR);
|
|
199
|
+
}
|
|
@@ -28,6 +28,16 @@ const REFERENCE_PATTERNS = [
|
|
|
28
28
|
},
|
|
29
29
|
// Schema foreign key references: table = "name" inside field definitions
|
|
30
30
|
{ keyword: 'table (FK)', regex: /\btable\s*=\s*"([^"]*)"/gm, targetType: 'table' },
|
|
31
|
+
// Realtime v2: a channel references its owning realtime_server by name via a
|
|
32
|
+
// top-level `realtime_server = "..."` (renamed from a bare `server` to match
|
|
33
|
+
// the api_group/mcp_server convention). The full keyword is unambiguous, so no
|
|
34
|
+
// false-match against another object type's `server` field.
|
|
35
|
+
{
|
|
36
|
+
keyword: 'channel (realtime_server)',
|
|
37
|
+
regex: /^\s*realtime_server\s*=\s*"([^"]*)"/gm,
|
|
38
|
+
sourceType: 'channel',
|
|
39
|
+
targetType: 'realtime_server',
|
|
40
|
+
},
|
|
31
41
|
];
|
|
32
42
|
/**
|
|
33
43
|
* Strip surrounding quotes from a name if present.
|
|
@@ -119,6 +129,9 @@ export function checkReferences(documents, serverOperations) {
|
|
|
119
129
|
if (!parsed)
|
|
120
130
|
continue;
|
|
121
131
|
for (const pattern of REFERENCE_PATTERNS) {
|
|
132
|
+
// Skip patterns scoped to a different source document type.
|
|
133
|
+
if (pattern.sourceType && pattern.sourceType !== parsed.type)
|
|
134
|
+
continue;
|
|
122
135
|
// Reset regex state for each document
|
|
123
136
|
pattern.regex.lastIndex = 0;
|
|
124
137
|
let match;
|