@dereekb/dbx-cli 13.11.0
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/LICENSE +21 -0
- package/firebase-api-manifest/src/generate-api-manifest/bind-validators.d.ts +44 -0
- package/firebase-api-manifest/src/generate-api-manifest/emit.d.ts +29 -0
- package/firebase-api-manifest/src/generate-api-manifest/extract-crud.d.ts +25 -0
- package/firebase-api-manifest/src/generate-api-manifest/find-api-files.d.ts +16 -0
- package/firebase-api-manifest/src/generate-api-manifest/main.d.ts +46 -0
- package/firebase-api-manifest/src/generate-api-manifest/parse-functions.d.ts +18 -0
- package/firebase-api-manifest/src/generate-api-manifest/resolve-package.d.ts +59 -0
- package/firebase-api-manifest/src/generate-api-manifest/types.d.ts +44 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +8162 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -0
- package/index.esm.js +8084 -0
- package/package.json +27 -0
- package/src/index.d.ts +1 -0
- package/src/lib/api/call-model.client.d.ts +37 -0
- package/src/lib/api/call-model.command.factory.d.ts +42 -0
- package/src/lib/api/call.passthrough.command.d.ts +8 -0
- package/src/lib/api/index.d.ts +3 -0
- package/src/lib/auth/auth.command.factory.d.ts +29 -0
- package/src/lib/auth/index.d.ts +3 -0
- package/src/lib/auth/oidc.client.d.ts +125 -0
- package/src/lib/auth/oidc.flow.d.ts +79 -0
- package/src/lib/config/cli.config.d.ts +112 -0
- package/src/lib/config/env.d.ts +183 -0
- package/src/lib/config/index.d.ts +4 -0
- package/src/lib/config/paths.d.ts +33 -0
- package/src/lib/config/token.cache.d.ts +51 -0
- package/src/lib/context/cli.context.d.ts +41 -0
- package/src/lib/context/index.d.ts +1 -0
- package/src/lib/doctor/doctor.command.factory.d.ts +48 -0
- package/src/lib/doctor/index.d.ts +1 -0
- package/src/lib/env/env.command.factory.d.ts +22 -0
- package/src/lib/env/index.d.ts +1 -0
- package/src/lib/index.d.ts +11 -0
- package/src/lib/manifest/build-manifest-commands.d.ts +77 -0
- package/src/lib/manifest/index.d.ts +2 -0
- package/src/lib/manifest/types.d.ts +20 -0
- package/src/lib/middleware/auth.middleware.d.ts +29 -0
- package/src/lib/middleware/index.d.ts +2 -0
- package/src/lib/middleware/output.middleware.d.ts +54 -0
- package/src/lib/output/index.d.ts +1 -0
- package/src/lib/output/output.command.factory.d.ts +55 -0
- package/src/lib/runner/index.d.ts +1 -0
- package/src/lib/runner/run.d.ts +71 -0
- package/src/lib/util/args.d.ts +78 -0
- package/src/lib/util/context.slot.d.ts +50 -0
- package/src/lib/util/handler.d.ts +13 -0
- package/src/lib/util/index.d.ts +6 -0
- package/src/lib/util/interactive.d.ts +26 -0
- package/src/lib/util/output.d.ts +152 -0
- package/src/lib/util/pagination.d.ts +91 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path configuration for a CLI's on-disk config and token cache files.
|
|
3
|
+
*/
|
|
4
|
+
export interface CliPaths {
|
|
5
|
+
readonly configDir: string;
|
|
6
|
+
readonly configFilePath: string;
|
|
7
|
+
readonly tokenCachePath: string;
|
|
8
|
+
}
|
|
9
|
+
export interface CliPathsConfig {
|
|
10
|
+
/**
|
|
11
|
+
* The CLI name. Used as the directory suffix (e.g. `demo-cli` → `~/.demo-cli`).
|
|
12
|
+
*/
|
|
13
|
+
readonly cliName: string;
|
|
14
|
+
/**
|
|
15
|
+
* Optional override for the config directory. When set, this is used verbatim instead of `~/.<cliName>`.
|
|
16
|
+
*
|
|
17
|
+
* Useful for tests that want to point at a temp dir.
|
|
18
|
+
*/
|
|
19
|
+
readonly configDirOverride?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Builds the on-disk path layout for a CLI's config directory.
|
|
23
|
+
*
|
|
24
|
+
* Layout:
|
|
25
|
+
* - `<configDir>/config.json` — the persistent CLI config (envs, output settings)
|
|
26
|
+
* - `<configDir>/.tokens.json` — per-env access/refresh token cache (mode 0600)
|
|
27
|
+
*
|
|
28
|
+
* @param config - The path-building inputs.
|
|
29
|
+
* @param config.cliName - The CLI's binary name; the default config dir is `~/.<cliName>`.
|
|
30
|
+
* @param config.configDirOverride - Optional override that replaces the default config directory verbatim (used by tests).
|
|
31
|
+
* @returns The {@link CliPaths} pointing at `configDir`, the config file, and the token cache file.
|
|
32
|
+
*/
|
|
33
|
+
export declare function buildCliPaths(config: CliPathsConfig): CliPaths;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type AsyncKeyedValueCache, type Maybe } from '@dereekb/util';
|
|
2
|
+
/**
|
|
3
|
+
* A cached token entry for a single env.
|
|
4
|
+
*/
|
|
5
|
+
export interface CliTokenEntry {
|
|
6
|
+
readonly accessToken: string;
|
|
7
|
+
readonly refreshToken?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Unix epoch milliseconds at which the access token expires.
|
|
10
|
+
*/
|
|
11
|
+
readonly expiresAt: number;
|
|
12
|
+
readonly tokenType?: string;
|
|
13
|
+
readonly scope?: string;
|
|
14
|
+
readonly idToken?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Token cache shape on disk — keyed by env name.
|
|
18
|
+
*/
|
|
19
|
+
export type CliTokenCache = Record<string, CliTokenEntry>;
|
|
20
|
+
/**
|
|
21
|
+
* Token cache store keyed by env name.
|
|
22
|
+
*
|
|
23
|
+
* Backed by a single JSON file with per-process in-memory memoization. See
|
|
24
|
+
* {@link createMemoizedJsonFileAsyncKeyedValueCache} for the underlying file/memo behavior.
|
|
25
|
+
*/
|
|
26
|
+
export type CliTokenCacheStore = AsyncKeyedValueCache<CliTokenEntry>;
|
|
27
|
+
export interface CreateCliTokenCacheStoreInput {
|
|
28
|
+
readonly tokenCachePath: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a per-env token cache store backed by a single JSON file.
|
|
32
|
+
*
|
|
33
|
+
* Entries are written with mode 0o600. Reads are memoized per-process to avoid hitting disk
|
|
34
|
+
* on every access.
|
|
35
|
+
*
|
|
36
|
+
* @param input - The cache store inputs.
|
|
37
|
+
* @param input.tokenCachePath - Absolute path to the JSON file backing the cache.
|
|
38
|
+
* @returns A {@link CliTokenCacheStore} keyed by env name.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createCliTokenCacheStore(input: CreateCliTokenCacheStoreInput): CliTokenCacheStore;
|
|
41
|
+
/**
|
|
42
|
+
* Returns true when the token entry's access token is at or near expiry.
|
|
43
|
+
*
|
|
44
|
+
* Defaults to a 60-second buffer to allow for clock skew and request latency.
|
|
45
|
+
*
|
|
46
|
+
* @param entry - The token cache entry to check (`null`/`undefined` is treated as expired).
|
|
47
|
+
* @param nowMs - The current time in unix epoch milliseconds. Defaults to `Date.now()`.
|
|
48
|
+
* @param bufferMs - Skew/latency buffer in milliseconds; the token is treated as expired this far ahead of `expiresAt`.
|
|
49
|
+
* @returns `true` when the token is at or within `bufferMs` of expiry, otherwise `false`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function isTokenExpired(entry: Maybe<CliTokenEntry>, nowMs?: number, bufferMs?: number): boolean;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { OnCallTypedModelParams } from '@dereekb/firebase';
|
|
2
|
+
import { type CliEnvConfig } from '../config/env';
|
|
3
|
+
/**
|
|
4
|
+
* The CLI context attached to argv by the auth middleware.
|
|
5
|
+
*
|
|
6
|
+
* Holds the active env, the current access token, and a `callModel` helper that performs
|
|
7
|
+
* the HTTP call against `<env.apiBaseUrl>/model/call`.
|
|
8
|
+
*/
|
|
9
|
+
export interface CliContext {
|
|
10
|
+
readonly cliName: string;
|
|
11
|
+
readonly envName: string;
|
|
12
|
+
readonly env: CliEnvConfig;
|
|
13
|
+
readonly accessToken: string;
|
|
14
|
+
readonly callModel: <TParams = unknown, TResult = unknown>(params: OnCallTypedModelParams<TParams>) => Promise<TResult>;
|
|
15
|
+
}
|
|
16
|
+
export declare const setCliContext: (value: import("@dereekb/util").Maybe<CliContext>) => void;
|
|
17
|
+
export declare const getCliContext: () => import("@dereekb/util").Maybe<CliContext>;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the current {@link CliContext} or throws — for use in command handlers that require auth.
|
|
20
|
+
*/
|
|
21
|
+
export declare const requireCliContext: () => CliContext;
|
|
22
|
+
export interface CreateCliContextInput {
|
|
23
|
+
readonly cliName: string;
|
|
24
|
+
readonly envName: string;
|
|
25
|
+
readonly env: CliEnvConfig;
|
|
26
|
+
readonly accessToken: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Builds a {@link CliContext} for the current invocation.
|
|
30
|
+
*
|
|
31
|
+
* Bundles the env config and access token alongside a `callModel` helper that POSTs to
|
|
32
|
+
* `<env.apiBaseUrl>/model/call` with the cached Bearer token.
|
|
33
|
+
*
|
|
34
|
+
* @param input - The context inputs.
|
|
35
|
+
* @param input.cliName - The CLI's binary name.
|
|
36
|
+
* @param input.envName - The active env name.
|
|
37
|
+
* @param input.env - The resolved {@link CliEnvConfig} for the active env.
|
|
38
|
+
* @param input.accessToken - The Bearer access token to include on outgoing API calls.
|
|
39
|
+
* @returns The constructed {@link CliContext}.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createCliContext(input: CreateCliContextInput): CliContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cli.context';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CommandModule } from 'yargs';
|
|
2
|
+
import { type Maybe } from '@dereekb/util';
|
|
3
|
+
import { type CliConfig } from '../config/cli.config';
|
|
4
|
+
import { type CliEnvConfig, type CliEnvDefault } from '../config/env';
|
|
5
|
+
export interface DoctorCheckInput {
|
|
6
|
+
readonly cliName: string;
|
|
7
|
+
readonly envName: Maybe<string>;
|
|
8
|
+
readonly env: Maybe<CliEnvConfig>;
|
|
9
|
+
readonly config: Maybe<CliConfig>;
|
|
10
|
+
}
|
|
11
|
+
export interface DoctorCheckResult {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly ok: boolean;
|
|
14
|
+
readonly detail?: unknown;
|
|
15
|
+
readonly suggestion?: string;
|
|
16
|
+
}
|
|
17
|
+
export type DoctorCheck = (input: DoctorCheckInput) => Promise<DoctorCheckResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Built-in checks: config file present, active env resolved, OIDC discovery, token refresh, API reachability.
|
|
20
|
+
*
|
|
21
|
+
* @returns The default {@link DoctorCheck} list, in execution order.
|
|
22
|
+
*/
|
|
23
|
+
export declare function defaultDoctorChecks(): DoctorCheck[];
|
|
24
|
+
export interface CreateDoctorCommandInput {
|
|
25
|
+
readonly cliName: string;
|
|
26
|
+
/**
|
|
27
|
+
* Additional checks to append after the default check list.
|
|
28
|
+
*/
|
|
29
|
+
readonly checks?: DoctorCheck[];
|
|
30
|
+
/**
|
|
31
|
+
* Built-in env presets. Resolved by env name and merged underneath the user's stored config so
|
|
32
|
+
* doctor can run against an env that only stores overrides on top of a registered default.
|
|
33
|
+
*/
|
|
34
|
+
readonly defaultEnvs?: readonly CliEnvDefault[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Composable doctor command.
|
|
38
|
+
*
|
|
39
|
+
* Runs all checks, prints a JSON summary, and exits 0 even when checks fail (the `ok` flag in the
|
|
40
|
+
* envelope is what callers script against).
|
|
41
|
+
*
|
|
42
|
+
* @param input - Factory configuration.
|
|
43
|
+
* @param input.cliName - The CLI's binary name.
|
|
44
|
+
* @param input.checks - Additional checks to append after the default check list.
|
|
45
|
+
* @param input.defaultEnvs - Built-in env presets merged underneath the user's stored env when names match.
|
|
46
|
+
* @returns A yargs `CommandModule` exposing the `doctor` command.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createDoctorCommand(input: CreateDoctorCommandInput): CommandModule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './doctor.command.factory';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CommandModule } from 'yargs';
|
|
2
|
+
import { type CliEnvDefault } from '../config/env';
|
|
3
|
+
export interface CreateEnvCommandInput {
|
|
4
|
+
readonly cliName: string;
|
|
5
|
+
/**
|
|
6
|
+
* Built-in env presets. Resolved by env name and merged underneath the user's stored config so
|
|
7
|
+
* `env list` / `env show` / `env add` reflect the effective config.
|
|
8
|
+
*/
|
|
9
|
+
readonly defaultEnvs?: readonly CliEnvDefault[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Factory for the built-in `env` command tree.
|
|
13
|
+
*
|
|
14
|
+
* Wires `list`, `use`, `add`, `show`, and `remove` subcommands that operate on the persisted env
|
|
15
|
+
* registry. Token-cache entries are removed when an env is deleted.
|
|
16
|
+
*
|
|
17
|
+
* @param input - Factory configuration.
|
|
18
|
+
* @param input.cliName - The CLI's binary name.
|
|
19
|
+
* @param input.defaultEnvs - Built-in env presets merged underneath the user's stored env when names match.
|
|
20
|
+
* @returns A yargs `CommandModule` exposing the full `env` subcommand surface.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createEnvCommand(input: CreateEnvCommandInput): CommandModule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './env.command.factory';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './api';
|
|
2
|
+
export * from './auth';
|
|
3
|
+
export * from './config';
|
|
4
|
+
export * from './context';
|
|
5
|
+
export * from './doctor';
|
|
6
|
+
export * from './env';
|
|
7
|
+
export * from './manifest';
|
|
8
|
+
export * from './middleware';
|
|
9
|
+
export * from './output';
|
|
10
|
+
export * from './runner';
|
|
11
|
+
export * from './util';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type CommandModule } from 'yargs';
|
|
2
|
+
import { type CliApiManifest } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Format used for the `Params Schema` section of a manifest command's `--help`
|
|
5
|
+
* epilogue.
|
|
6
|
+
*
|
|
7
|
+
* - `jsonschema` (default): emits a draft-2020-12 JSON Schema, the canonical
|
|
8
|
+
* shape `--data <json>` must match.
|
|
9
|
+
* - `arktype`: emits the arktype `expression` string — a compact, copy-pastable
|
|
10
|
+
* declaration that round-trips through `type(...)` to an equivalent validator.
|
|
11
|
+
* - `both`: emits the JSON Schema followed by the arktype expression.
|
|
12
|
+
*/
|
|
13
|
+
export type ManifestHelpDataFormat = 'jsonschema' | 'arktype' | 'both';
|
|
14
|
+
/**
|
|
15
|
+
* Default schema format when no override is supplied.
|
|
16
|
+
*/
|
|
17
|
+
export declare const DEFAULT_MANIFEST_HELP_DATA_FORMAT: ManifestHelpDataFormat;
|
|
18
|
+
/**
|
|
19
|
+
* Options accepted by {@link buildManifestCommands}.
|
|
20
|
+
*/
|
|
21
|
+
export interface BuildManifestCommandsOptions {
|
|
22
|
+
/**
|
|
23
|
+
* argv used to detect `--data-help`, `--all-help`, and similar flags.
|
|
24
|
+
* Defaults to `process.argv`. Tests pass an explicit value so detection
|
|
25
|
+
* does not leak from the surrounding process.
|
|
26
|
+
*/
|
|
27
|
+
readonly argv?: readonly string[];
|
|
28
|
+
/**
|
|
29
|
+
* Format used for the `Params Schema` section of `--help`.
|
|
30
|
+
*
|
|
31
|
+
* When omitted, the configured argv is scanned for `--data-help=<format>`
|
|
32
|
+
* (or `--data-help <format>`). Falls back to
|
|
33
|
+
* {@link DEFAULT_MANIFEST_HELP_DATA_FORMAT}.
|
|
34
|
+
*/
|
|
35
|
+
readonly dataHelpFormat?: ManifestHelpDataFormat;
|
|
36
|
+
/**
|
|
37
|
+
* Whether to hide unrelated global options (like `--verbose`, `--dump-dir`,
|
|
38
|
+
* `--pick`, …) from `--help` when the user passed `--data-help`. Defaults
|
|
39
|
+
* to `true` so the help output stays focused on the schema sections.
|
|
40
|
+
*
|
|
41
|
+
* Even when this is `true`, the user can pass `--all-help` to opt back in
|
|
42
|
+
* to the full options table.
|
|
43
|
+
*/
|
|
44
|
+
readonly focusHelpOnDataHelp?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Names of global options to hide when focus mode is in effect. Defaults
|
|
47
|
+
* to {@link STANDARD_GLOBAL_OPTION_NAMES} (the options registered by
|
|
48
|
+
* {@link createCli}).
|
|
49
|
+
*/
|
|
50
|
+
readonly hiddenWhenFocused?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Builds yargs `CommandModule[]` from a generated {@link CliApiManifest}.
|
|
54
|
+
*
|
|
55
|
+
* Groups entries by `model`, emitting one parent command per model and one child action per entry
|
|
56
|
+
* (named `<verb>` or `<verb>-<specifier>`). Each child accepts `--data <json>`, validates the
|
|
57
|
+
* payload against the entry's bound arktype validator (when present), and dispatches via the
|
|
58
|
+
* authenticated CLI context's `callModel` helper. Standalone entries are skipped because they
|
|
59
|
+
* are not dispatched through the `/model/call` endpoint.
|
|
60
|
+
*
|
|
61
|
+
* @param manifest - The generated manifest array.
|
|
62
|
+
* @param options - Optional overrides; see {@link BuildManifestCommandsOptions}.
|
|
63
|
+
* @returns The yargs `CommandModule[]` ready to be passed to `runCli({ apiCommands })`.
|
|
64
|
+
*/
|
|
65
|
+
export declare function buildManifestCommands(manifest: CliApiManifest, options?: BuildManifestCommandsOptions): CommandModule[];
|
|
66
|
+
/**
|
|
67
|
+
* Inspects an argv array for `--data-help=<format>` or `--data-help <format>`
|
|
68
|
+
* and returns the requested {@link ManifestHelpDataFormat}.
|
|
69
|
+
*
|
|
70
|
+
* Implemented as a raw argv scan (rather than going through yargs) because the
|
|
71
|
+
* value is needed when each command's builder runs — which is before yargs
|
|
72
|
+
* parses argv. Unrecognized values fall back to the default.
|
|
73
|
+
*
|
|
74
|
+
* @param argv - argv to inspect (defaults to `process.argv`).
|
|
75
|
+
* @returns The detected format, or {@link DEFAULT_MANIFEST_HELP_DATA_FORMAT}.
|
|
76
|
+
*/
|
|
77
|
+
export declare function detectDataHelpFormat(argv?: readonly string[]): ManifestHelpDataFormat;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Type } from 'arktype';
|
|
2
|
+
export type CliApiVerb = 'create' | 'read' | 'update' | 'delete' | 'query' | 'standalone';
|
|
3
|
+
export interface CliApiManifestField {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly typeText: string;
|
|
6
|
+
readonly description?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface CliApiManifestEntry {
|
|
9
|
+
readonly model: string;
|
|
10
|
+
readonly verb: CliApiVerb;
|
|
11
|
+
readonly specifier?: string;
|
|
12
|
+
readonly paramsTypeName?: string;
|
|
13
|
+
readonly paramsValidator?: Type<unknown>;
|
|
14
|
+
readonly resultTypeName?: string;
|
|
15
|
+
readonly groupName: string;
|
|
16
|
+
readonly sourceFile: string;
|
|
17
|
+
readonly description?: string;
|
|
18
|
+
readonly paramsFields?: readonly CliApiManifestField[];
|
|
19
|
+
}
|
|
20
|
+
export type CliApiManifest = readonly CliApiManifestEntry[];
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MiddlewareFunction } from 'yargs';
|
|
2
|
+
import { type CliEnvDefault } from '../config/env';
|
|
3
|
+
export interface CreateAuthMiddlewareInput {
|
|
4
|
+
readonly cliName: string;
|
|
5
|
+
/**
|
|
6
|
+
* Top-level command names that bypass authentication entirely.
|
|
7
|
+
*
|
|
8
|
+
* Conventionally: `auth`, `env`, `doctor`, plus any other config/utility commands.
|
|
9
|
+
*/
|
|
10
|
+
readonly skipCommands: ReadonlySet<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Built-in env presets. Merged underneath the user's stored env when the env name matches.
|
|
13
|
+
*/
|
|
14
|
+
readonly defaultEnvs?: readonly CliEnvDefault[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Yargs middleware that resolves the active env, loads/refreshes its access token, and attaches
|
|
18
|
+
* a {@link CliContext} via the module-level slot.
|
|
19
|
+
*
|
|
20
|
+
* Skips auth for the configured top-level commands so `auth login`, `env list`, etc. don't require
|
|
21
|
+
* a valid token.
|
|
22
|
+
*
|
|
23
|
+
* @param input - Middleware configuration.
|
|
24
|
+
* @param input.cliName - The CLI's binary name (used to derive the env-var prefix and config dir).
|
|
25
|
+
* @param input.skipCommands - Top-level command names that bypass authentication entirely.
|
|
26
|
+
* @param input.defaultEnvs - Built-in env presets merged underneath the user's stored env when names match.
|
|
27
|
+
* @returns A yargs middleware function suitable for `.middleware([..., true])`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createAuthMiddleware(input: CreateAuthMiddlewareInput): MiddlewareFunction;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Maybe } from '@dereekb/util';
|
|
2
|
+
import type { MiddlewareFunction } from 'yargs';
|
|
3
|
+
import { type CliCommandOutputConfig, type CliOutputConfig } from '../config/cli.config';
|
|
4
|
+
/**
|
|
5
|
+
* Returns the output config slice from wherever the consumer stores it.
|
|
6
|
+
*
|
|
7
|
+
* Default implementation reads from `<configDir>/config.json` at the dbx-cli location, but
|
|
8
|
+
* apps with their own on-disk config shape (e.g. zoho-cli) plug in their own loader.
|
|
9
|
+
*/
|
|
10
|
+
export type LoadOutputConfigFn = () => Promise<Maybe<CliOutputConfig>>;
|
|
11
|
+
/**
|
|
12
|
+
* Persists a per-command output override (called only when `--set-pick` / `--set-dump-dir` is used).
|
|
13
|
+
*
|
|
14
|
+
* Apps with their own config-file shape pass an implementation that merges into their store.
|
|
15
|
+
*/
|
|
16
|
+
export type SaveCommandOutputConfigFn = (commandKey: string, config: CliCommandOutputConfig) => Promise<void>;
|
|
17
|
+
export interface CreateOutputMiddlewareInput {
|
|
18
|
+
readonly cliName: string;
|
|
19
|
+
/**
|
|
20
|
+
* Top-level command names whose output should never be filtered by saved pick/dump-dir
|
|
21
|
+
* (typically the same set passed to {@link createAuthMiddleware}).
|
|
22
|
+
*/
|
|
23
|
+
readonly skipCommands: ReadonlySet<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Optional override for how the output config is read.
|
|
26
|
+
*
|
|
27
|
+
* When omitted, defaults to reading from `~/.config/<cliName>/config.json` via
|
|
28
|
+
* {@link loadCliConfig} + {@link buildCliPaths}.
|
|
29
|
+
*/
|
|
30
|
+
readonly loadOutputConfig?: LoadOutputConfigFn;
|
|
31
|
+
/**
|
|
32
|
+
* Optional override for how a per-command output config is persisted.
|
|
33
|
+
*
|
|
34
|
+
* When omitted, defaults to {@link mergeCliConfig} writing into `~/.config/<cliName>/config.json`.
|
|
35
|
+
*/
|
|
36
|
+
readonly saveCommandOutputConfig?: SaveCommandOutputConfigFn;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Yargs middleware that resolves output options (`--pick`, `--dump-dir`) by combining CLI flags,
|
|
40
|
+
* `--set-pick` / `--set-dump-dir` saves, per-command config, and global config — then writes them
|
|
41
|
+
* into the module-level output options slot.
|
|
42
|
+
*
|
|
43
|
+
* Consumers with a non-default config-file shape (e.g. zoho-cli's per-product layout) inject
|
|
44
|
+
* {@link CreateOutputMiddlewareInput.loadOutputConfig} and {@link CreateOutputMiddlewareInput.saveCommandOutputConfig}
|
|
45
|
+
* so the middleware orchestrates resolve/save without owning the storage format.
|
|
46
|
+
*
|
|
47
|
+
* @param input - Middleware configuration.
|
|
48
|
+
* @param input.cliName - The CLI's binary name (used to derive the default config path).
|
|
49
|
+
* @param input.skipCommands - Top-level command names whose output is never filtered by saved pick/dump-dir.
|
|
50
|
+
* @param input.loadOutputConfig - Optional override for how the output config is read.
|
|
51
|
+
* @param input.saveCommandOutputConfig - Optional override for how a per-command output config is persisted.
|
|
52
|
+
* @returns A yargs middleware function suitable for `.middleware([..., true])`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createOutputMiddleware(input: CreateOutputMiddlewareInput): MiddlewareFunction;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './output.command.factory';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { CommandModule } from 'yargs';
|
|
2
|
+
import { type CliOutputConfig } from '../config/cli.config';
|
|
3
|
+
import { type LoadOutputConfigFn } from '../middleware/output.middleware';
|
|
4
|
+
/**
|
|
5
|
+
* Persists a partial output config update (global + per-command overrides). Implementations are
|
|
6
|
+
* expected to merge with existing state — the same semantics used by {@link mergeCliConfig}.
|
|
7
|
+
*/
|
|
8
|
+
export type MergeOutputConfigFn = (update: CliOutputConfig) => Promise<unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Wipes the persisted output config entirely (global defaults + every per-command override).
|
|
11
|
+
*/
|
|
12
|
+
export type ClearOutputConfigFn = () => Promise<unknown>;
|
|
13
|
+
export interface CreateOutputCommandInput {
|
|
14
|
+
readonly cliName: string;
|
|
15
|
+
/**
|
|
16
|
+
* Reads the persisted output config. When omitted, defaults to dbx-cli's own
|
|
17
|
+
* `<configDir>/config.json` via {@link loadCliConfig}.
|
|
18
|
+
*/
|
|
19
|
+
readonly loadOutputConfig?: LoadOutputConfigFn;
|
|
20
|
+
/**
|
|
21
|
+
* Merges partial updates into the persisted output config. When omitted, defaults to
|
|
22
|
+
* {@link mergeCliConfig} writing to `<configDir>/config.json`.
|
|
23
|
+
*/
|
|
24
|
+
readonly mergeOutputConfig?: MergeOutputConfigFn;
|
|
25
|
+
/**
|
|
26
|
+
* Clears the persisted output config. When omitted, defaults to merging an empty/`undefined`
|
|
27
|
+
* output slice via {@link mergeCliConfig}.
|
|
28
|
+
*/
|
|
29
|
+
readonly clearOutputConfig?: ClearOutputConfigFn;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Factory for the `output` config command.
|
|
33
|
+
*
|
|
34
|
+
* Subcommands:
|
|
35
|
+
* - `output set [--command <key>] [--set-dump-dir <path>] [--set-pick <fields>]`
|
|
36
|
+
* - `output show`
|
|
37
|
+
* - `output clear [--command <key>]`
|
|
38
|
+
*
|
|
39
|
+
* The factory accepts callbacks for reading/writing the persisted output config so each CLI can
|
|
40
|
+
* plug in its own on-disk shape (e.g. dbx-cli's flat `<config>.output`, zoho-cli's
|
|
41
|
+
* shared/recruit/crm/desk wrapper). When omitted, the dbx-cli defaults persist to
|
|
42
|
+
* `<configDir>/config.json`.
|
|
43
|
+
*
|
|
44
|
+
* The `--set-dump-dir` / `--set-pick` flags on `output set` reuse the global flags registered by
|
|
45
|
+
* {@link createCli}, so the same syntax `<cli> output set --command foo.bar --set-pick a,b` works
|
|
46
|
+
* across CLIs.
|
|
47
|
+
*
|
|
48
|
+
* @param input - Factory configuration.
|
|
49
|
+
* @param input.cliName - The CLI's binary name (used to derive the default config path).
|
|
50
|
+
* @param input.loadOutputConfig - Optional override for reading the persisted output config.
|
|
51
|
+
* @param input.mergeOutputConfig - Optional override for persisting partial output-config updates.
|
|
52
|
+
* @param input.clearOutputConfig - Optional override for clearing the persisted output config entirely.
|
|
53
|
+
* @returns A yargs `CommandModule` exposing the full `output` subcommand surface.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createOutputCommand(input: CreateOutputCommandInput): CommandModule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './run';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Argv, type CommandModule } from 'yargs';
|
|
2
|
+
import { type CliEnvDefault } from '../config/env';
|
|
3
|
+
import { type DoctorCheck } from '../doctor/doctor.command.factory';
|
|
4
|
+
/**
|
|
5
|
+
* Names of the global options registered by {@link createCli} that are not
|
|
6
|
+
* specific to a single manifest command's payload. Manifest commands hide
|
|
7
|
+
* these from `--help` when the user passes `--data-help` so the help output
|
|
8
|
+
* focuses on the schema sections.
|
|
9
|
+
*/
|
|
10
|
+
export declare const STANDARD_GLOBAL_OPTION_NAMES: readonly string[];
|
|
11
|
+
export interface CreateCliInput {
|
|
12
|
+
readonly cliName: string;
|
|
13
|
+
/**
|
|
14
|
+
* App-specific config/utility commands appended after the built-in `auth`, `env`, and `doctor` commands.
|
|
15
|
+
*
|
|
16
|
+
* Commands listed here bypass authentication.
|
|
17
|
+
*/
|
|
18
|
+
readonly configCommands?: CommandModule[];
|
|
19
|
+
/**
|
|
20
|
+
* App-specific API commands appended after the built-in `call` passthrough.
|
|
21
|
+
*
|
|
22
|
+
* Commands listed here run after the auth middleware, so they have access to the {@link CliContext}.
|
|
23
|
+
*/
|
|
24
|
+
readonly apiCommands?: CommandModule[];
|
|
25
|
+
/**
|
|
26
|
+
* Extra checks appended to the doctor's default check list.
|
|
27
|
+
*/
|
|
28
|
+
readonly doctorChecks?: DoctorCheck[];
|
|
29
|
+
/**
|
|
30
|
+
* Built-in env presets shipped with the CLI. When the user runs commands against an env name
|
|
31
|
+
* that matches one of {@link CliEnvDefault.names}, the default values are merged underneath the
|
|
32
|
+
* stored config so users don't need to supply `apiBaseUrl`/`oidcIssuer` themselves.
|
|
33
|
+
*/
|
|
34
|
+
readonly defaultEnvs?: readonly CliEnvDefault[];
|
|
35
|
+
/**
|
|
36
|
+
* Argv to parse. Defaults to `hideBin(process.argv)`.
|
|
37
|
+
*/
|
|
38
|
+
readonly argv?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* Disable the built-in `call` passthrough — useful when an app prefers to expose only typed wrappers.
|
|
41
|
+
*/
|
|
42
|
+
readonly disableCallPassthrough?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Top-level CLI builder.
|
|
46
|
+
*
|
|
47
|
+
* Wires the standard yargs setup (global flags, auth/output middleware, the built-in `auth`, `env`,
|
|
48
|
+
* `doctor`, and `call` commands), and returns a yargs parser ready to be `.parse()`-d.
|
|
49
|
+
*
|
|
50
|
+
* App CLIs become a thin `index.ts` that imports `createCli`, registers any app-specific commands,
|
|
51
|
+
* and calls `.parse()`.
|
|
52
|
+
*
|
|
53
|
+
* @param input - Builder configuration.
|
|
54
|
+
* @param input.cliName - The CLI's binary name (used as `scriptName`, env-var prefix, and config dir).
|
|
55
|
+
* @param input.configCommands - App-specific config/utility commands appended after the built-ins; bypass auth.
|
|
56
|
+
* @param input.apiCommands - App-specific API commands appended after the built-in `call` passthrough; run after auth middleware.
|
|
57
|
+
* @param input.doctorChecks - Extra checks appended to the doctor's default check list.
|
|
58
|
+
* @param input.defaultEnvs - Built-in env presets shipped with the CLI.
|
|
59
|
+
* @param input.argv - Argv to parse. Defaults to `hideBin(process.argv)`.
|
|
60
|
+
* @param input.disableCallPassthrough - When `true`, omits the built-in `call` passthrough.
|
|
61
|
+
* @returns The configured yargs `Argv` ready to be `.parse()`-d.
|
|
62
|
+
*/
|
|
63
|
+
export declare function createCli(input: CreateCliInput): Argv;
|
|
64
|
+
/**
|
|
65
|
+
* Convenience helper for app `index.ts` entrypoints — wraps `createCli().parse()` in a try/catch
|
|
66
|
+
* that emits a structured error envelope and exits 1 on uncaught failures.
|
|
67
|
+
*
|
|
68
|
+
* @param input - The same builder configuration accepted by {@link createCli}.
|
|
69
|
+
* @returns Resolves once the parser has finished. Rejects only when `process.exit` is stubbed (e.g. in tests).
|
|
70
|
+
*/
|
|
71
|
+
export declare function runCli(input: CreateCliInput): Promise<void>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Argv } from 'yargs';
|
|
2
|
+
export interface EnvOptions {
|
|
3
|
+
readonly env?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Adds the standard `--env <name>` option used by every per-env command.
|
|
7
|
+
*
|
|
8
|
+
* @param yargs - The yargs builder to extend.
|
|
9
|
+
* @returns The same yargs builder with the `--env` option chained on.
|
|
10
|
+
*/
|
|
11
|
+
export declare function withEnv<T>(yargs: Argv<T>): Argv<T & EnvOptions>;
|
|
12
|
+
export interface OutputOptions {
|
|
13
|
+
readonly dumpDir?: string;
|
|
14
|
+
readonly pick?: string;
|
|
15
|
+
readonly pickAll?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Adds the per-command output flag set: `--dump-dir`, `--pick`, and `--pick-all`.
|
|
19
|
+
*
|
|
20
|
+
* @param yargs - The yargs builder to extend.
|
|
21
|
+
* @returns The same yargs builder with the output options chained on.
|
|
22
|
+
*/
|
|
23
|
+
export declare function withOutput<T>(yargs: Argv<T>): Argv<T & OutputOptions>;
|
|
24
|
+
/**
|
|
25
|
+
* Dump file format for {@link runPaginatedList} when `--dump-dir` is set.
|
|
26
|
+
*
|
|
27
|
+
* - `raw`: one full JSON page response (concatenated JSON when `--dump-merge=concat`, not standard JSON).
|
|
28
|
+
* - `page_by_line`: NDJSON with one page response per line.
|
|
29
|
+
* - `data_by_line`: NDJSON with one record per line.
|
|
30
|
+
*/
|
|
31
|
+
export declare const DUMP_OUTPUT_MODES: readonly ["raw", "page_by_line", "data_by_line"];
|
|
32
|
+
export type DumpOutputMode = (typeof DUMP_OUTPUT_MODES)[number];
|
|
33
|
+
/**
|
|
34
|
+
* Across-pages dump merge mode for {@link runPaginatedList}.
|
|
35
|
+
*
|
|
36
|
+
* - `replace`: truncate the file each iteration; only the last page survives.
|
|
37
|
+
* - `concat`: append each page to the file.
|
|
38
|
+
*/
|
|
39
|
+
export declare const DUMP_MERGE_MODES: readonly ["replace", "concat"];
|
|
40
|
+
export type DumpMergeMode = (typeof DUMP_MERGE_MODES)[number];
|
|
41
|
+
/**
|
|
42
|
+
* Stdout shape when `--multiple-pages > 1`.
|
|
43
|
+
*
|
|
44
|
+
* - `meta`: summary only (low memory).
|
|
45
|
+
* - `pages`: array of page responses (holds all pages in memory).
|
|
46
|
+
* - `merged_page`: concat all records into one array (holds all records in memory).
|
|
47
|
+
*/
|
|
48
|
+
export declare const MULTIPLE_PAGES_OUTPUT_MODES: readonly ["meta", "pages", "merged_page"];
|
|
49
|
+
export type MultiplePagesOutputMode = (typeof MULTIPLE_PAGES_OUTPUT_MODES)[number];
|
|
50
|
+
export interface MultiplePagesOptions {
|
|
51
|
+
readonly multiplePages: number;
|
|
52
|
+
readonly multiplePagesOutput: MultiplePagesOutputMode;
|
|
53
|
+
readonly dumpOutput: DumpOutputMode;
|
|
54
|
+
readonly dumpMerge: DumpMergeMode;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds the multi-page pagination flag set shared by every paginated list command.
|
|
58
|
+
*
|
|
59
|
+
* Compose from a CLI-specific page/offset builder (e.g. Zoho's `withPagination` adds
|
|
60
|
+
* `--page` / `--per-page`, then chains this for the multi-page controls).
|
|
61
|
+
*
|
|
62
|
+
* @param yargs - The yargs builder to extend.
|
|
63
|
+
* @returns The same yargs builder with the multi-page options chained on.
|
|
64
|
+
*/
|
|
65
|
+
export declare function withMultiplePages<T>(yargs: Argv<T>): Argv<T & MultiplePagesOptions>;
|
|
66
|
+
export interface CallModelArgs {
|
|
67
|
+
readonly model: string;
|
|
68
|
+
readonly verb: string;
|
|
69
|
+
readonly specifier?: string;
|
|
70
|
+
readonly data?: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Adds the standard generic-call positional + flag set: `<model> <verb> [specifier]` plus `--data <json>`.
|
|
74
|
+
*
|
|
75
|
+
* @param yargs - The yargs builder to extend.
|
|
76
|
+
* @returns The same yargs builder with the model-call positionals and `--data` option chained on.
|
|
77
|
+
*/
|
|
78
|
+
export declare function withCallModelArgs<T>(yargs: Argv<T>): Argv<T & CallModelArgs>;
|