@nocobase/app-cli-client 0.0.0-stage → 0.1.0-beta.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.
Files changed (178) hide show
  1. package/README.md +24 -2
  2. package/dist/brand.d.ts +52 -0
  3. package/dist/brand.d.ts.map +1 -0
  4. package/dist/brand.js +61 -0
  5. package/dist/brand.js.map +1 -0
  6. package/dist/commands.d.ts +9 -0
  7. package/dist/commands.d.ts.map +1 -0
  8. package/dist/commands.js +30 -0
  9. package/dist/commands.js.map +1 -0
  10. package/dist/completion.d.ts +13 -0
  11. package/dist/completion.d.ts.map +1 -0
  12. package/dist/completion.js +157 -0
  13. package/dist/completion.js.map +1 -0
  14. package/dist/config.d.ts +100 -0
  15. package/dist/config.d.ts.map +1 -0
  16. package/dist/config.js +43 -0
  17. package/dist/config.js.map +1 -0
  18. package/dist/docs.d.ts +9 -0
  19. package/dist/docs.d.ts.map +1 -0
  20. package/dist/docs.js +146 -0
  21. package/dist/docs.js.map +1 -0
  22. package/dist/dynamic/execute.d.ts +24 -0
  23. package/dist/dynamic/execute.d.ts.map +1 -0
  24. package/dist/dynamic/execute.js +125 -0
  25. package/dist/dynamic/execute.js.map +1 -0
  26. package/dist/dynamic/files.d.ts +21 -0
  27. package/dist/dynamic/files.d.ts.map +1 -0
  28. package/dist/dynamic/files.js +79 -0
  29. package/dist/dynamic/files.js.map +1 -0
  30. package/dist/dynamic/help.d.ts +6 -0
  31. package/dist/dynamic/help.d.ts.map +1 -0
  32. package/dist/dynamic/help.js +42 -0
  33. package/dist/dynamic/help.js.map +1 -0
  34. package/dist/dynamic/index.d.ts +34 -0
  35. package/dist/dynamic/index.d.ts.map +1 -0
  36. package/dist/dynamic/index.js +197 -0
  37. package/dist/dynamic/index.js.map +1 -0
  38. package/dist/dynamic/manifest.d.ts +21 -0
  39. package/dist/dynamic/manifest.d.ts.map +1 -0
  40. package/dist/dynamic/manifest.js +175 -0
  41. package/dist/dynamic/manifest.js.map +1 -0
  42. package/dist/dynamic/output.d.ts +14 -0
  43. package/dist/dynamic/output.d.ts.map +1 -0
  44. package/dist/dynamic/output.js +103 -0
  45. package/dist/dynamic/output.js.map +1 -0
  46. package/dist/dynamic/parse.d.ts +3 -0
  47. package/dist/dynamic/parse.d.ts.map +1 -0
  48. package/dist/dynamic/parse.js +13 -0
  49. package/dist/dynamic/parse.js.map +1 -0
  50. package/dist/dynamic/rest.d.ts +29 -0
  51. package/dist/dynamic/rest.d.ts.map +1 -0
  52. package/dist/dynamic/rest.js +212 -0
  53. package/dist/dynamic/rest.js.map +1 -0
  54. package/dist/dynamic/session.d.ts +20 -0
  55. package/dist/dynamic/session.d.ts.map +1 -0
  56. package/dist/dynamic/session.js +70 -0
  57. package/dist/dynamic/session.js.map +1 -0
  58. package/dist/dynamic/ticket.d.ts +13 -0
  59. package/dist/dynamic/ticket.d.ts.map +1 -0
  60. package/dist/dynamic/ticket.js +40 -0
  61. package/dist/dynamic/ticket.js.map +1 -0
  62. package/dist/hooks/command-not-found.d.ts +4 -0
  63. package/dist/hooks/command-not-found.d.ts.map +1 -0
  64. package/dist/hooks/command-not-found.js +6 -0
  65. package/dist/hooks/command-not-found.js.map +1 -0
  66. package/dist/index.d.ts +8 -0
  67. package/dist/index.d.ts.map +1 -0
  68. package/dist/index.js +14 -0
  69. package/dist/index.js.map +1 -0
  70. package/dist/install.d.ts +72 -0
  71. package/dist/install.d.ts.map +1 -0
  72. package/dist/install.js +148 -0
  73. package/dist/install.js.map +1 -0
  74. package/dist/lib/command.d.ts +35 -0
  75. package/dist/lib/command.d.ts.map +1 -0
  76. package/dist/lib/command.js +97 -0
  77. package/dist/lib/command.js.map +1 -0
  78. package/dist/lib/credentials.d.ts +110 -0
  79. package/dist/lib/credentials.d.ts.map +1 -0
  80. package/dist/lib/credentials.js +330 -0
  81. package/dist/lib/credentials.js.map +1 -0
  82. package/dist/lib/device.d.ts +48 -0
  83. package/dist/lib/device.d.ts.map +1 -0
  84. package/dist/lib/device.js +189 -0
  85. package/dist/lib/device.js.map +1 -0
  86. package/dist/lib/envelope.d.ts +21 -0
  87. package/dist/lib/envelope.d.ts.map +1 -0
  88. package/dist/lib/envelope.js +87 -0
  89. package/dist/lib/envelope.js.map +1 -0
  90. package/dist/lib/files.d.ts +4 -0
  91. package/dist/lib/files.d.ts.map +1 -0
  92. package/dist/lib/files.js +23 -0
  93. package/dist/lib/files.js.map +1 -0
  94. package/dist/lib/globals.d.ts +25 -0
  95. package/dist/lib/globals.d.ts.map +1 -0
  96. package/dist/lib/globals.js +77 -0
  97. package/dist/lib/globals.js.map +1 -0
  98. package/dist/lib/http.d.ts +45 -0
  99. package/dist/lib/http.d.ts.map +1 -0
  100. package/dist/lib/http.js +141 -0
  101. package/dist/lib/http.js.map +1 -0
  102. package/dist/lib/mime.d.ts +3 -0
  103. package/dist/lib/mime.d.ts.map +1 -0
  104. package/dist/lib/mime.js +38 -0
  105. package/dist/lib/mime.js.map +1 -0
  106. package/dist/lib/secrets/index.d.ts +18 -0
  107. package/dist/lib/secrets/index.d.ts.map +1 -0
  108. package/dist/lib/secrets/index.js +55 -0
  109. package/dist/lib/secrets/index.js.map +1 -0
  110. package/dist/lib/secrets/macos.d.ts +13 -0
  111. package/dist/lib/secrets/macos.d.ts.map +1 -0
  112. package/dist/lib/secrets/macos.js +61 -0
  113. package/dist/lib/secrets/macos.js.map +1 -0
  114. package/dist/lib/secrets/secret-service.d.ts +17 -0
  115. package/dist/lib/secrets/secret-service.d.ts.map +1 -0
  116. package/dist/lib/secrets/secret-service.js +81 -0
  117. package/dist/lib/secrets/secret-service.js.map +1 -0
  118. package/dist/lib/secrets/store.d.ts +28 -0
  119. package/dist/lib/secrets/store.d.ts.map +1 -0
  120. package/dist/lib/secrets/store.js +41 -0
  121. package/dist/lib/secrets/store.js.map +1 -0
  122. package/dist/lib/secrets/windows.d.ts +20 -0
  123. package/dist/lib/secrets/windows.d.ts.map +1 -0
  124. package/dist/lib/secrets/windows.js +155 -0
  125. package/dist/lib/secrets/windows.js.map +1 -0
  126. package/dist/lib/update-hint.d.ts +5 -0
  127. package/dist/lib/update-hint.d.ts.map +1 -0
  128. package/dist/lib/update-hint.js +26 -0
  129. package/dist/lib/update-hint.js.map +1 -0
  130. package/dist/login.d.ts +15 -0
  131. package/dist/login.d.ts.map +1 -0
  132. package/dist/login.js +232 -0
  133. package/dist/login.js.map +1 -0
  134. package/dist/logout.d.ts +13 -0
  135. package/dist/logout.d.ts.map +1 -0
  136. package/dist/logout.js +56 -0
  137. package/dist/logout.js.map +1 -0
  138. package/dist/parse/arguments.d.ts +84 -0
  139. package/dist/parse/arguments.d.ts.map +1 -0
  140. package/dist/parse/arguments.js +460 -0
  141. package/dist/parse/arguments.js.map +1 -0
  142. package/dist/parse/exit.d.ts +7 -0
  143. package/dist/parse/exit.d.ts.map +1 -0
  144. package/dist/parse/exit.js +24 -0
  145. package/dist/parse/exit.js.map +1 -0
  146. package/dist/parse/help.d.ts +22 -0
  147. package/dist/parse/help.d.ts.map +1 -0
  148. package/dist/parse/help.js +151 -0
  149. package/dist/parse/help.js.map +1 -0
  150. package/dist/parse/index.d.ts +5 -0
  151. package/dist/parse/index.d.ts.map +1 -0
  152. package/dist/parse/index.js +7 -0
  153. package/dist/parse/index.js.map +1 -0
  154. package/dist/parse/manifest.d.ts +97 -0
  155. package/dist/parse/manifest.d.ts.map +1 -0
  156. package/dist/parse/manifest.js +4 -0
  157. package/dist/parse/manifest.js.map +1 -0
  158. package/dist/profile.d.ts +4 -0
  159. package/dist/profile.d.ts.map +1 -0
  160. package/dist/profile.js +80 -0
  161. package/dist/profile.js.map +1 -0
  162. package/dist/request.d.ts +25 -0
  163. package/dist/request.d.ts.map +1 -0
  164. package/dist/request.js +35 -0
  165. package/dist/request.js.map +1 -0
  166. package/dist/run.d.ts +17 -0
  167. package/dist/run.d.ts.map +1 -0
  168. package/dist/run.js +88 -0
  169. package/dist/run.js.map +1 -0
  170. package/dist/update.d.ts +45 -0
  171. package/dist/update.d.ts.map +1 -0
  172. package/dist/update.js +177 -0
  173. package/dist/update.js.map +1 -0
  174. package/dist/whoami.d.ts +31 -0
  175. package/dist/whoami.d.ts.map +1 -0
  176. package/dist/whoami.js +118 -0
  177. package/dist/whoami.js.map +1 -0
  178. package/package.json +67 -3
@@ -0,0 +1,148 @@
1
+ // Where an application's CLI is installed when it came from the application's install script
2
+ // (`/api/agents/dist/installScript`), and how a newer version replaces it:
3
+ //
4
+ // <prefix>/versions/<version>/ one unpacked standalone tarball per version (bin/<bin>, bin/node, dist/, …)
5
+ // <prefix>/current -> versions/<version>
6
+ // <prefix>/install.json what the script set up: the command link (`binLink`), and `mode`, `cli` for the
7
+ // CLI alone or `runner` once the script ran with `--runner` (absent: `runner`)
8
+ // <bin-dir>/<bin> -> <prefix>/current/bin/<bin>
9
+ //
10
+ // A service runs `<prefix>/current/bin/<bin>`, so switching `current` and restarting is an update. A CLI started from
11
+ // a source checkout, or installed any other way, has no installation: it neither updates nor uninstalls itself.
12
+ //
13
+ // This module reads no CLI configuration, so the CLI's `update` and a runner carried by the CLI share it.
14
+ import { execFile } from 'node:child_process';
15
+ import { createHash } from 'node:crypto';
16
+ import { existsSync, readFileSync } from 'node:fs';
17
+ import { mkdir, readdir, readlink, rename, rm, symlink, writeFile, } from 'node:fs/promises';
18
+ import path from 'node:path';
19
+ import { promisify } from 'node:util';
20
+ import { compareVersions, currentTarget, DIST_ROUTES, DistArtifactSchema, routePath, } from '@nocobase/agent-protocol';
21
+ const execFileAsync = promisify(execFile);
22
+ /** The installation whose version directory is `root`, the root of the installed package; undefined for any other. */
23
+ export function detectInstallation(root) {
24
+ const versionsDir = path.dirname(root);
25
+ if (path.basename(versionsDir) !== 'versions')
26
+ return undefined;
27
+ const prefix = path.dirname(versionsDir);
28
+ const current = path.join(prefix, 'current');
29
+ if (!existsSync(current))
30
+ return undefined;
31
+ let binLink;
32
+ let mode = 'runner';
33
+ try {
34
+ const info = JSON.parse(readFileSync(path.join(prefix, 'install.json'), 'utf8'));
35
+ if (typeof info.binLink === 'string' && info.binLink !== '')
36
+ binLink = info.binLink;
37
+ if (info.mode === 'cli')
38
+ mode = 'cli';
39
+ }
40
+ catch {
41
+ binLink = undefined;
42
+ }
43
+ return {
44
+ prefix,
45
+ versionDir: root,
46
+ version: path.basename(root),
47
+ current,
48
+ ...(binLink === undefined ? {} : { binLink }),
49
+ mode,
50
+ };
51
+ }
52
+ /** The command that starts whichever version is current. */
53
+ export function launcherOf(installation, bin) {
54
+ return path.join(installation.current, 'bin', bin);
55
+ }
56
+ export class ChecksumError extends Error {
57
+ name = 'ChecksumError';
58
+ }
59
+ export function sha256Of(bytes) {
60
+ return createHash('sha256').update(bytes).digest('hex');
61
+ }
62
+ /**
63
+ * Checks `bytes` against `sha256` and unpacks the standalone tarball into `dest` (its top directory stripped), through
64
+ * a sibling `.partial` directory so `dest` is either complete or absent.
65
+ */
66
+ export async function unpackTarball(bytes, sha256, dest) {
67
+ const actual = sha256Of(bytes);
68
+ if (actual !== sha256)
69
+ throw new ChecksumError(`The download does not match its SHA-256 (expected ${sha256}, got ${actual}).`);
70
+ const partial = `${dest}.partial`;
71
+ await rm(partial, { recursive: true, force: true });
72
+ await mkdir(partial, { recursive: true, mode: 0o755 });
73
+ const archive = `${partial}.tar.gz`;
74
+ try {
75
+ await writeFile(archive, bytes, { mode: 0o600 });
76
+ await execFileAsync('tar', [
77
+ '-xzf',
78
+ archive,
79
+ '-C',
80
+ partial,
81
+ '--strip-components=1',
82
+ ]);
83
+ await rm(dest, { recursive: true, force: true });
84
+ await rename(partial, dest);
85
+ }
86
+ finally {
87
+ await rm(archive, { force: true });
88
+ await rm(partial, { recursive: true, force: true });
89
+ }
90
+ }
91
+ /** Points `<prefix>/current` at `version`, replacing the link in one rename. */
92
+ export async function activateVersion(installation, version) {
93
+ const temporary = path.join(installation.prefix, `.current-${process.pid}-${Date.now()}`);
94
+ await symlink(path.join('versions', version), temporary);
95
+ await rename(temporary, installation.current);
96
+ }
97
+ /** The version `current` points at. */
98
+ export async function currentVersion(installation) {
99
+ try {
100
+ return path.basename(await readlink(installation.current));
101
+ }
102
+ catch {
103
+ return undefined;
104
+ }
105
+ }
106
+ /** Removes every version but `keep`. */
107
+ export async function pruneVersions(installation, keep) {
108
+ const versionsDir = path.join(installation.prefix, 'versions');
109
+ const removed = [];
110
+ for (const entry of await readdir(versionsDir).catch(() => [])) {
111
+ if (keep.includes(entry))
112
+ continue;
113
+ await rm(path.join(versionsDir, entry), { recursive: true, force: true });
114
+ removed.push(entry);
115
+ }
116
+ return removed;
117
+ }
118
+ /** The newest version of `product` the application serves for `target`. */
119
+ export async function latestArtifact(client, product, target = currentTarget()) {
120
+ return client.get(routePath(DIST_ROUTES.resolve, { product, target }), DistArtifactSchema);
121
+ }
122
+ export function isNewer(candidate, running) {
123
+ return compareVersions(candidate, running) > 0;
124
+ }
125
+ /**
126
+ * Installs `update` beside the running version and makes it current; the previous version stays until the next
127
+ * update, so a bad one can be switched back by hand. The process has to restart to run it.
128
+ */
129
+ export async function applyUpdate(options) {
130
+ const { installation, client, update, bin } = options;
131
+ if (!/^[0-9A-Za-z.+-]+$/u.test(update.version))
132
+ throw new Error(`Not a version: ${update.version}`);
133
+ const previous = await currentVersion(installation);
134
+ const dest = path.join(installation.prefix, 'versions', update.version);
135
+ if (!existsSync(path.join(dest, 'bin', bin))) {
136
+ options.log?.(`update: downloading ${bin} ${update.version}`);
137
+ const bytes = await client.download(update.url);
138
+ await unpackTarball(bytes, update.sha256, dest);
139
+ }
140
+ await activateVersion(installation, update.version);
141
+ options.log?.(`update: ${bin} ${update.version} is current (was ${previous ?? 'none'})`);
142
+ const removed = await pruneVersions(installation, [
143
+ update.version,
144
+ ...(previous === undefined ? [] : [previous]),
145
+ ]);
146
+ return { previous, removed };
147
+ }
148
+ //# sourceMappingURL=install.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.js","sourceRoot":"","sources":["../src/install.ts"],"names":[],"mappings":"AAAA,6FAA6F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AAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;KAC9C,CAAC,CAAC;IACH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { Command } from '@oclif/core';
2
+ import { type ExitCode } from '@nocobase/agent-protocol';
3
+ import { type AppCliPaths } from '../config.ts';
4
+ export declare class UsageError extends Error {
5
+ readonly exit: ExitCode;
6
+ constructor(message: string, exit?: ExitCode);
7
+ }
8
+ /** A failure as oclif reports it, with the CLI's exit code. */
9
+ export declare function asCliError(error: unknown): Error;
10
+ export declare abstract class AppCommand extends Command {
11
+ static enableJsonFlag: boolean;
12
+ /** Whether the command honours the global `--dry-run`; one that does not refuses it. */
13
+ static supportsDryRun: boolean;
14
+ /** The failure as the command raised it, before it became oclif's error. */
15
+ private failure;
16
+ /** The command as typed, such as `whoami`. */
17
+ protected get commandName(): string;
18
+ protected toSuccessJson(result: unknown): unknown;
19
+ /** Whether `--dry-run` was given: the command then says what it would do and changes nothing. */
20
+ protected get dryRun(): boolean;
21
+ /** Whether `--yes` was given. */
22
+ protected get yes(): boolean;
23
+ init(): Promise<void>;
24
+ /** Under `--quiet` nothing but `--json`'s document is printed on success. */
25
+ log(message?: string, ...args: unknown[]): void;
26
+ warn(input: string | Error): string | Error;
27
+ protected toErrorJson(error: unknown): unknown;
28
+ protected get paths(): AppCliPaths;
29
+ /** The CLI's command name, for messages. */
30
+ protected get bin(): string;
31
+ protected catch(error: Error & {
32
+ exitCode?: number;
33
+ }): Promise<unknown>;
34
+ }
35
+ //# sourceMappingURL=command.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../../src/lib/command.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAU,MAAM,aAAa,CAAC;AAG9C,OAAO,EAAc,KAAK,QAAQ,EAAE,MAAM,0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(`envelope.ts`), the global
2
+ // flags (`globals.ts`), and failures reported with the CLI's exit codes (0 ok, 1 general, 2 network, 3 auth, 4 not found, 5 validation,
3
+ // 6 conflict, 7 a plan is required).
4
+ import { Command, Errors } from '@oclif/core';
5
+ import { ZodError } from 'zod';
6
+ import { EXIT_CODES } from '@nocobase/agent-protocol';
7
+ import { appCliConfig, appCliPaths } from "../config.js";
8
+ import { CliCommandError, failureEnvelope, successEnvelope, } from "./envelope.js";
9
+ import { globalFlags } from "./globals.js";
10
+ import { AppApiError } from "./http.js";
11
+ export class UsageError extends Error {
12
+ exit;
13
+ constructor(message, exit = EXIT_CODES.validation) {
14
+ super(message);
15
+ this.name = 'UsageError';
16
+ this.exit = exit;
17
+ }
18
+ }
19
+ /** A failure as oclif reports it, with the CLI's exit code. */
20
+ export function asCliError(error) {
21
+ if (error instanceof AppApiError) {
22
+ return new Errors.CLIError(`${error.reason}: ${error.message}`, {
23
+ exit: error.exitCode,
24
+ });
25
+ }
26
+ if (error instanceof UsageError)
27
+ return new Errors.CLIError(error.message, { exit: error.exit });
28
+ if (error instanceof CliCommandError)
29
+ return new Errors.CLIError([
30
+ error.message,
31
+ ...error.suggestions.map((suggestion) => suggestion.run
32
+ ? `${suggestion.message} (${[suggestion.run.command, ...suggestion.run.args].join(' ')})`
33
+ : suggestion.message),
34
+ ].join('\n'), { exit: error.exit });
35
+ if (error instanceof ZodError)
36
+ return new Errors.CLIError(`The server answered in an unexpected shape: ${error.issues[0]?.message ?? 'invalid'}`, { exit: EXIT_CODES.general });
37
+ return error instanceof Error ? error : new Error(String(error));
38
+ }
39
+ export class AppCommand extends Command {
40
+ static enableJsonFlag = true;
41
+ /** Whether the command honours the global `--dry-run`; one that does not refuses it. */
42
+ static supportsDryRun = false;
43
+ /** The failure as the command raised it, before it became oclif's error. */
44
+ failure;
45
+ /** The command as typed, such as `whoami`. */
46
+ get commandName() {
47
+ return (this.id ?? '').split(':').join(' ');
48
+ }
49
+ toSuccessJson(result) {
50
+ return successEnvelope(this.commandName, result, [], globalFlags().dryRun ? 'success-noop' : 'success');
51
+ }
52
+ /** Whether `--dry-run` was given: the command then says what it would do and changes nothing. */
53
+ get dryRun() {
54
+ return globalFlags().dryRun;
55
+ }
56
+ /** Whether `--yes` was given. */
57
+ get yes() {
58
+ return globalFlags().yes;
59
+ }
60
+ async init() {
61
+ await super.init();
62
+ const own = this.constructor;
63
+ if (globalFlags().dryRun && !own.supportsDryRun)
64
+ throw new CliCommandError('DRY_RUN_UNSUPPORTED', `\`${this.bin} ${this.commandName}\` has no dry run; run it without --dry-run.`, { exit: EXIT_CODES.validation });
65
+ }
66
+ /** Under `--quiet` nothing but `--json`'s document is printed on success. */
67
+ log(message, ...args) {
68
+ if (globalFlags().quiet && !this.jsonEnabled())
69
+ return;
70
+ super.log(message, ...args);
71
+ }
72
+ warn(input) {
73
+ if (globalFlags().quiet)
74
+ return input;
75
+ return super.warn(input);
76
+ }
77
+ toErrorJson(error) {
78
+ return failureEnvelope(this.commandName, this.failure ?? error, [], appCliConfig().bin);
79
+ }
80
+ get paths() {
81
+ return appCliPaths();
82
+ }
83
+ /** The CLI's command name, for messages. */
84
+ get bin() {
85
+ return appCliConfig().bin;
86
+ }
87
+ async catch(error) {
88
+ this.failure = error;
89
+ if (error instanceof AppApiError ||
90
+ error instanceof UsageError ||
91
+ error instanceof CliCommandError ||
92
+ error instanceof ZodError)
93
+ return super.catch(asCliError(error));
94
+ return super.catch(error);
95
+ }
96
+ }
97
+ //# sourceMappingURL=command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.js","sourceRoot":"","sources":["../../src/lib/command.ts"],"names":[],"mappings":"AAAA,uHAAuH;AACvH,wIAAwI;AACxI,qCAAqC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAE/B,OAAO,EAAE,UAAU,EAAiB,MAAM,0BAA0B,CAAC;AAErE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K,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC"}
@@ -0,0 +1,110 @@
1
+ import { type RunCliCredential } from '@nocobase/agent-protocol';
2
+ import { type AppCliConfig, type AppCliPaths } from '../config.ts';
3
+ import { ApiClient } from './http.ts';
4
+ import { type KeyStorage, type SecretStore } from './secrets/index.ts';
5
+ /** What a profile's credential is: a session token from a sign-in through the browser, or an API key. */
6
+ export type CredentialKind = 'session' | 'apiKey';
7
+ /** The profile a sign-in goes to when none is named. */
8
+ export declare const DEFAULT_PROFILE = "default";
9
+ /** A credential the CLI holds, what it is, and where it came from. */
10
+ export interface UserConfig {
11
+ readonly server: string;
12
+ /** The session token or the API key. */
13
+ readonly key: string;
14
+ /** A session token is sent as `Authorization: Bearer`, an API key as `x-api-key`. */
15
+ readonly kind: CredentialKind;
16
+ readonly storage: KeyStorage | 'env';
17
+ /** The profile, or undefined for a key from the environment. */
18
+ readonly profile?: string | undefined;
19
+ readonly keyId?: string | undefined;
20
+ readonly keyName?: string | undefined;
21
+ }
22
+ /** A profile as `profile list` shows it: never its key. */
23
+ export interface ProfileSummary {
24
+ readonly name: string;
25
+ readonly server: string;
26
+ readonly storage: KeyStorage;
27
+ readonly kind: CredentialKind;
28
+ readonly keyName?: string | undefined;
29
+ readonly current: boolean;
30
+ }
31
+ export interface UserConfigOptions {
32
+ paths?: AppCliPaths;
33
+ config?: AppCliConfig;
34
+ /** The keychain; the platform's own by default, undefined for none. */
35
+ store?: SecretStore | undefined;
36
+ env?: NodeJS.ProcessEnv;
37
+ /** The profile to use; `--profile`, else `<PREFIX>_PROFILE`, else the current one. */
38
+ profile?: string | undefined;
39
+ }
40
+ /** `<PREFIX>_<name>`, such as `ACME_API_KEY`; undefined when the CLI has no prefix or the variable is empty. */
41
+ export declare function envValue(config: AppCliConfig, env: NodeJS.ProcessEnv, name: 'SERVER' | 'API_KEY' | 'PROFILE'): string | undefined;
42
+ /** The keychain account of a profile: the state directory, and the profile's name after `#` beside the default. */
43
+ export declare function profileAccount(home: string, profile: string): string;
44
+ /** The profile a command acts as: `--profile`, `<PREFIX>_PROFILE`, the current one, or `default`. */
45
+ export declare function selectedProfile(stored: {
46
+ readonly current?: string | undefined;
47
+ }, options: Pick<UserConfigOptions, 'profile'>, config: AppCliConfig, env: NodeJS.ProcessEnv): string;
48
+ /** Refuses a name a profile cannot have. */
49
+ export declare function checkProfileName(name: string): string;
50
+ /**
51
+ * The key the CLI acts with, or undefined when nobody is signed in: `<PREFIX>_API_KEY` (with `<PREFIX>_SERVER`, or the
52
+ * profile's server), else the selected profile's. Throws a `UsageError` when the key is in a keychain that cannot be
53
+ * read or no longer has it, and inside an agent run, which never acts as the person.
54
+ */
55
+ export declare function readUserConfig(options?: UserConfigOptions): Promise<UserConfig | undefined>;
56
+ /**
57
+ * The server and profile a command would act as, read without its key (no keychain is touched): what completion needs
58
+ * to find the cached manifest. Undefined when nobody is signed in.
59
+ */
60
+ export declare function peekUserConfig(options?: UserConfigOptions): Promise<{
61
+ readonly server: string;
62
+ readonly profile?: string;
63
+ } | undefined>;
64
+ export interface SavedUserConfig {
65
+ storage: KeyStorage;
66
+ profile: string;
67
+ /** Set when the key had to go into `config.json`. */
68
+ warning?: string;
69
+ }
70
+ /** What the credential is, and what the server said of a key, kept beside the profile. */
71
+ export interface SavedKeyInfo {
72
+ /** `apiKey` when left out. */
73
+ readonly kind?: CredentialKind | undefined;
74
+ readonly keyId?: string | undefined;
75
+ readonly keyName?: string | undefined;
76
+ }
77
+ /**
78
+ * Stores the credential of `server` as a profile (the selected one, `default` at first) and makes it current: in the
79
+ * keychain, or in `config.json` (0600) with a warning when there is no usable keychain.
80
+ */
81
+ export declare function saveUserConfig(server: string, key: string, options?: UserConfigOptions & SavedKeyInfo): Promise<SavedUserConfig>;
82
+ /** The profiles, current first, without their keys. */
83
+ export declare function listProfiles(options?: UserConfigOptions): Promise<ProfileSummary[]>;
84
+ /** Makes `name` the current profile. */
85
+ export declare function useProfile(name: string, options?: UserConfigOptions): Promise<void>;
86
+ /**
87
+ * Forgets a profile: its key leaves the keychain and `config.json`. The current profile becomes the first one left.
88
+ * Answers what was forgotten, or undefined when there was no such profile.
89
+ */
90
+ export declare function removeProfile(name: string, options?: UserConfigOptions): Promise<{
91
+ readonly server: string;
92
+ } | undefined>;
93
+ /** The headers a credential authenticates with: `Authorization: Bearer` for a session token, `x-api-key` for a key. */
94
+ export declare function credentialHeaders(credential: Pick<UserConfig, 'key'> & {
95
+ readonly kind?: CredentialKind;
96
+ }): Record<string, string>;
97
+ export declare function userClient(config: Pick<UserConfig, 'server' | 'key'> & {
98
+ readonly kind?: CredentialKind;
99
+ }): ApiClient;
100
+ export declare function runClient(credential: RunCliCredential): ApiClient;
101
+ /** Accepts `https://host/path` and `host:port`; drops a trailing slash. */
102
+ export declare function normalizeServer(server: string): string;
103
+ /**
104
+ * The run's credentials file: the one the runner names for this process (`AGENT_RUN_CREDENTIALS`, set only for the CLI
105
+ * by the command the runner puts on the agent's PATH, so it works from a directory outside the run's own), else the
106
+ * nearest one at or above `from`.
107
+ */
108
+ export declare function findRunCredentials(from: string, config?: AppCliConfig, env?: NodeJS.ProcessEnv): string | undefined;
109
+ export declare function readRunCredentials(file: string): Promise<RunCliCredential>;
110
+ //# sourceMappingURL=credentials.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../../src/lib/credentials.ts"],"names":[],"mappings":"AAeA,OAAO,EAKL,KAAK,gBAAgB,EACtB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,WAAW,EACjB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAOL,KAAK,UAAU,EACf,KAAK,WAAW,EACjB,MAAM,oBAAoB,CAAC;AAE5B,yGAAyG;AACzG,MAAM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