@eide/foir-cli 0.1.33 → 0.1.35

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.
@@ -0,0 +1,259 @@
1
+ import { readFileSync } from 'fs';
2
+ import { resolve, dirname } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { COMMANDS, createSchemaEngine, } from '@eide/command-registry';
5
+ import { withErrorHandler } from '../lib/errors.js';
6
+ import { createClient } from '../lib/client.js';
7
+ import { formatOutput, formatList, timeAgo, success, } from '../lib/output.js';
8
+ import { parseInputData, confirmAction, isUUID } from '../lib/input.js';
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = dirname(__filename);
11
+ function loadSchemaSDL() {
12
+ // Try monorepo path first (development)
13
+ const monorepoPath = resolve(__dirname, '../../../../graphql-core/schema.graphql');
14
+ try {
15
+ return readFileSync(monorepoPath, 'utf-8');
16
+ }
17
+ catch {
18
+ // Fall back to relative path from dist (might differ)
19
+ const altPath = resolve(__dirname, '../../../graphql-core/schema.graphql');
20
+ try {
21
+ return readFileSync(altPath, 'utf-8');
22
+ }
23
+ catch {
24
+ throw new Error('Could not find schema.graphql. Ensure you are running from within the EIDE monorepo.');
25
+ }
26
+ }
27
+ }
28
+ /**
29
+ * Extract the useful data from a GraphQL result, unwrapping list wrappers.
30
+ */
31
+ function extractResult(result, operationName) {
32
+ const raw = result[operationName];
33
+ if (!raw || typeof raw !== 'object')
34
+ return { data: raw };
35
+ const obj = raw;
36
+ // Check for list wrappers: { items: [...], total } or { entityName: [...], total }
37
+ if ('total' in obj) {
38
+ for (const [key, val] of Object.entries(obj)) {
39
+ if (key !== 'total' && key !== '__typename' && Array.isArray(val)) {
40
+ return { data: val, total: obj.total };
41
+ }
42
+ }
43
+ }
44
+ return { data: raw };
45
+ }
46
+ /**
47
+ * Convert registry ColumnDef (with string format) to CLI ColumnDef (with format function).
48
+ */
49
+ function toCliColumns(columns) {
50
+ if (!columns)
51
+ return undefined;
52
+ return columns.map((col) => {
53
+ const cliCol = {
54
+ key: col.key,
55
+ header: col.header,
56
+ width: col.width,
57
+ };
58
+ switch (col.format) {
59
+ case 'timeAgo':
60
+ cliCol.format = (v) => timeAgo(v);
61
+ break;
62
+ case 'boolean':
63
+ cliCol.format = (v) => (v ? 'yes' : '');
64
+ break;
65
+ case 'truncate':
66
+ cliCol.format = (v) => {
67
+ const str = String(v ?? '');
68
+ return str.length > 40 ? str.slice(0, 39) + '\u2026' : str;
69
+ };
70
+ break;
71
+ case 'join':
72
+ cliCol.format = (v) => Array.isArray(v) ? v.join(', ') : String(v ?? '');
73
+ break;
74
+ case 'bytes':
75
+ cliCol.format = (v) => {
76
+ const num = Number(v);
77
+ if (num < 1024)
78
+ return `${num} B`;
79
+ if (num < 1024 * 1024)
80
+ return `${(num / 1024).toFixed(1)} KB`;
81
+ return `${(num / (1024 * 1024)).toFixed(1)} MB`;
82
+ };
83
+ break;
84
+ }
85
+ return cliCol;
86
+ });
87
+ }
88
+ /**
89
+ * Register all declarative commands from the command registry.
90
+ */
91
+ export function registerDynamicCommands(program, globalOpts) {
92
+ let engine;
93
+ try {
94
+ const sdl = loadSchemaSDL();
95
+ engine = createSchemaEngine(sdl);
96
+ }
97
+ catch (err) {
98
+ // If schema can't be loaded, skip dynamic command registration
99
+ // (special commands still work)
100
+ console.error(`Warning: Could not load schema for dynamic commands: ${err instanceof Error ? err.message : String(err)}`);
101
+ return;
102
+ }
103
+ // Group commands by group name
104
+ const groups = new Map();
105
+ for (const cmd of COMMANDS) {
106
+ if (!groups.has(cmd.group))
107
+ groups.set(cmd.group, []);
108
+ groups.get(cmd.group).push(cmd);
109
+ }
110
+ for (const [groupName, entries] of groups) {
111
+ const group = program.command(groupName).description(`Manage ${groupName}`);
112
+ for (const entry of entries) {
113
+ let cmd = group.command(entry.name).description(entry.description);
114
+ // Add positional args
115
+ for (const pos of entry.positionalArgs ?? []) {
116
+ cmd = cmd.argument(`<${pos.name}>`, pos.description ?? pos.name);
117
+ }
118
+ // Add flags from schema args (excluding positional args and input args)
119
+ const schemaArgs = engine.getOperationArgs(entry.operation, entry.operationType);
120
+ for (const arg of schemaArgs) {
121
+ // Skip if already a positional arg
122
+ if (entry.positionalArgs?.some((p) => p.graphqlArg === arg.name))
123
+ continue;
124
+ // Skip if it's the input arg
125
+ if (entry.acceptsInput && arg.name === (entry.inputArgName ?? 'input'))
126
+ continue;
127
+ const reqStr = arg.required ? ' (required)' : '';
128
+ cmd = cmd.option(`--${arg.name} <value>`, `${arg.name}${reqStr}`);
129
+ }
130
+ // Add --data/--file for input commands
131
+ if (entry.acceptsInput) {
132
+ cmd = cmd.option('-d, --data <json>', 'Data as JSON');
133
+ cmd = cmd.option('-f, --file <path>', 'Read data from file');
134
+ }
135
+ // Add --confirm for destructive commands
136
+ if (entry.requiresConfirmation) {
137
+ cmd = cmd.option('--confirm', 'Skip confirmation prompt');
138
+ }
139
+ // Action handler
140
+ cmd.action(withErrorHandler(globalOpts, async (...actionArgs) => {
141
+ const opts = globalOpts();
142
+ const client = await createClient(opts);
143
+ // Build variables from positional args + flags
144
+ const variables = {};
145
+ // Map positional args
146
+ const positionals = entry.positionalArgs ?? [];
147
+ for (let i = 0; i < positionals.length; i++) {
148
+ const posDef = positionals[i];
149
+ const value = actionArgs[i];
150
+ if (value) {
151
+ variables[posDef.graphqlArg] = value;
152
+ }
153
+ }
154
+ // The flags/options object is the last argument from Commander
155
+ const flags = (actionArgs[positionals.length] ?? {});
156
+ // Map named flags to variables (coerce types)
157
+ const rawFlags = {};
158
+ for (const [key, val] of Object.entries(flags)) {
159
+ if (key === 'data' || key === 'file' || key === 'confirm')
160
+ continue;
161
+ rawFlags[key] = String(val);
162
+ }
163
+ const coerced = engine.coerceArgs(entry.operation, entry.operationType, rawFlags);
164
+ Object.assign(variables, coerced);
165
+ // Handle input data (--data, --file, stdin)
166
+ if (entry.acceptsInput && (flags.data || flags.file)) {
167
+ const inputData = await parseInputData({
168
+ data: flags.data,
169
+ file: flags.file,
170
+ });
171
+ const argName = entry.inputArgName ?? 'input';
172
+ variables[argName] = inputData;
173
+ }
174
+ // Handle alternate get (by key/code instead of UUID)
175
+ if (entry.alternateGet &&
176
+ positionals.length > 0 &&
177
+ variables[positionals[0].graphqlArg]) {
178
+ const firstArgValue = String(variables[positionals[0].graphqlArg]);
179
+ if (!isUUID(firstArgValue)) {
180
+ // Use alternate operation
181
+ const altEntry = {
182
+ ...entry,
183
+ operation: entry.alternateGet.operation,
184
+ positionalArgs: [
185
+ {
186
+ name: positionals[0].name,
187
+ graphqlArg: entry.alternateGet.argName,
188
+ },
189
+ ],
190
+ };
191
+ // Remap the variable
192
+ delete variables[positionals[0].graphqlArg];
193
+ variables[entry.alternateGet.argName] = firstArgValue;
194
+ const queryStr = engine.buildQuery(altEntry, variables);
195
+ const result = (await client.request(queryStr, variables));
196
+ const { data } = extractResult(result, altEntry.operation);
197
+ formatOutput(data, opts);
198
+ return;
199
+ }
200
+ }
201
+ // Confirmation prompt for destructive actions
202
+ if (entry.requiresConfirmation) {
203
+ const confirmed = await confirmAction(`${entry.description}?`, {
204
+ confirm: !!flags.confirm,
205
+ });
206
+ if (!confirmed) {
207
+ console.log('Aborted.');
208
+ return;
209
+ }
210
+ }
211
+ // Build and execute query
212
+ const queryStr = engine.buildQuery(entry, variables);
213
+ const result = (await client.request(queryStr, variables));
214
+ const { data, total } = extractResult(result, entry.operation);
215
+ // Format output
216
+ if (entry.scalarResult) {
217
+ if (!(opts.json || opts.jsonl || opts.quiet)) {
218
+ if (entry.successMessage) {
219
+ success(entry.successMessage);
220
+ }
221
+ }
222
+ else {
223
+ formatOutput(data, opts);
224
+ }
225
+ }
226
+ else if (Array.isArray(data)) {
227
+ const cliColumns = toCliColumns(entry.columns);
228
+ formatList(data, opts, {
229
+ columns: cliColumns ?? autoColumns(data),
230
+ total,
231
+ });
232
+ }
233
+ else {
234
+ formatOutput(data, opts);
235
+ if (entry.successMessage &&
236
+ !(opts.json || opts.jsonl || opts.quiet)) {
237
+ success(entry.successMessage);
238
+ }
239
+ }
240
+ }));
241
+ }
242
+ }
243
+ }
244
+ /**
245
+ * Auto-generate column definitions from the first item in an array.
246
+ */
247
+ function autoColumns(items) {
248
+ if (items.length === 0)
249
+ return [];
250
+ const first = items[0];
251
+ return Object.keys(first)
252
+ .filter((k) => k !== '__typename')
253
+ .slice(0, 6)
254
+ .map((key) => ({
255
+ key,
256
+ header: key,
257
+ width: 20,
258
+ }));
259
+ }
@@ -38,8 +38,8 @@ async function fetchSessionContext(apiUrl, accessToken) {
38
38
  return data.sessionContext;
39
39
  }
40
40
  async function fetchApiKeys(apiUrl, accessToken, projectId, tenantId) {
41
- const data = await gqlRequest(apiUrl, accessToken, `query { listApiKeys(includeInactive: false, limit: 100) { apiKeys { id name isActive } } }`, undefined, { 'x-tenant-id': tenantId, 'x-project-id': projectId });
42
- return data.listApiKeys?.apiKeys ?? [];
41
+ const data = await gqlRequest(apiUrl, accessToken, `query { listApiKeys(includeInactive: false, limit: 100) { items { id name isActive } } }`, undefined, { 'x-tenant-id': tenantId, 'x-project-id': projectId });
42
+ return data.listApiKeys?.items ?? [];
43
43
  }
44
44
  async function createApiKey(apiUrl, accessToken, projectId, tenantId) {
45
45
  const data = await gqlRequest(apiUrl, accessToken, `mutation($input: CreateApiKeyInput!) { createApiKey(input: $input) { apiKey { id name isActive } plainKey } }`, {