appwrite-utils-cli 1.7.9 → 1.8.2

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 (70) hide show
  1. package/CHANGELOG.md +14 -199
  2. package/README.md +87 -30
  3. package/dist/adapters/AdapterFactory.js +5 -25
  4. package/dist/adapters/DatabaseAdapter.d.ts +17 -2
  5. package/dist/adapters/LegacyAdapter.d.ts +2 -1
  6. package/dist/adapters/LegacyAdapter.js +212 -16
  7. package/dist/adapters/TablesDBAdapter.d.ts +2 -12
  8. package/dist/adapters/TablesDBAdapter.js +261 -57
  9. package/dist/cli/commands/databaseCommands.js +4 -3
  10. package/dist/cli/commands/functionCommands.js +17 -8
  11. package/dist/collections/attributes.js +447 -125
  12. package/dist/collections/methods.js +197 -186
  13. package/dist/collections/tableOperations.d.ts +86 -0
  14. package/dist/collections/tableOperations.js +434 -0
  15. package/dist/collections/transferOperations.d.ts +3 -2
  16. package/dist/collections/transferOperations.js +93 -12
  17. package/dist/config/yamlConfig.d.ts +221 -88
  18. package/dist/examples/yamlTerminologyExample.d.ts +1 -1
  19. package/dist/examples/yamlTerminologyExample.js +6 -3
  20. package/dist/functions/fnConfigDiscovery.d.ts +3 -0
  21. package/dist/functions/fnConfigDiscovery.js +108 -0
  22. package/dist/interactiveCLI.js +18 -15
  23. package/dist/main.js +211 -73
  24. package/dist/migrations/appwriteToX.d.ts +88 -23
  25. package/dist/migrations/comprehensiveTransfer.d.ts +2 -0
  26. package/dist/migrations/comprehensiveTransfer.js +83 -6
  27. package/dist/migrations/dataLoader.d.ts +227 -69
  28. package/dist/migrations/dataLoader.js +3 -3
  29. package/dist/migrations/importController.js +3 -3
  30. package/dist/migrations/relationships.d.ts +8 -2
  31. package/dist/migrations/services/ImportOrchestrator.js +3 -3
  32. package/dist/migrations/transfer.js +159 -37
  33. package/dist/shared/attributeMapper.d.ts +20 -0
  34. package/dist/shared/attributeMapper.js +203 -0
  35. package/dist/shared/selectionDialogs.js +8 -4
  36. package/dist/storage/schemas.d.ts +354 -92
  37. package/dist/utils/configDiscovery.js +4 -3
  38. package/dist/utils/versionDetection.d.ts +0 -4
  39. package/dist/utils/versionDetection.js +41 -173
  40. package/dist/utils/yamlConverter.js +89 -16
  41. package/dist/utils/yamlLoader.d.ts +1 -1
  42. package/dist/utils/yamlLoader.js +6 -2
  43. package/dist/utilsController.js +56 -19
  44. package/package.json +4 -4
  45. package/src/adapters/AdapterFactory.ts +119 -143
  46. package/src/adapters/DatabaseAdapter.ts +18 -3
  47. package/src/adapters/LegacyAdapter.ts +236 -105
  48. package/src/adapters/TablesDBAdapter.ts +773 -643
  49. package/src/cli/commands/databaseCommands.ts +13 -12
  50. package/src/cli/commands/functionCommands.ts +23 -14
  51. package/src/collections/attributes.ts +2054 -1611
  52. package/src/collections/methods.ts +208 -293
  53. package/src/collections/tableOperations.ts +506 -0
  54. package/src/collections/transferOperations.ts +218 -144
  55. package/src/examples/yamlTerminologyExample.ts +10 -5
  56. package/src/functions/fnConfigDiscovery.ts +103 -0
  57. package/src/interactiveCLI.ts +25 -20
  58. package/src/main.ts +549 -194
  59. package/src/migrations/comprehensiveTransfer.ts +126 -50
  60. package/src/migrations/dataLoader.ts +3 -3
  61. package/src/migrations/importController.ts +3 -3
  62. package/src/migrations/services/ImportOrchestrator.ts +3 -3
  63. package/src/migrations/transfer.ts +148 -131
  64. package/src/shared/attributeMapper.ts +229 -0
  65. package/src/shared/selectionDialogs.ts +29 -25
  66. package/src/utils/configDiscovery.ts +9 -3
  67. package/src/utils/versionDetection.ts +74 -228
  68. package/src/utils/yamlConverter.ts +94 -17
  69. package/src/utils/yamlLoader.ts +11 -4
  70. package/src/utilsController.ts +80 -30
@@ -1,120 +1,177 @@
1
- import {
2
- Client,
3
- Databases,
4
- ID,
5
- Query,
6
- } from "node-appwrite";
7
- import { tryAwaitWithRetry, delay, calculateExponentialBackoff } from "../utils/helperFunctions.js";
8
- import { MessageFormatter } from "../shared/messageFormatter.js";
9
- import { chunk } from "es-toolkit";
1
+ import {
2
+ Client,
3
+ Databases,
4
+ ID,
5
+ Query,
6
+ } from "node-appwrite";
7
+ import { tryAwaitWithRetry, delay, calculateExponentialBackoff } from "../utils/helperFunctions.js";
8
+ import { MessageFormatter } from "../shared/messageFormatter.js";
9
+ import { chunk } from "es-toolkit";
10
+ import type { DatabaseAdapter } from "../adapters/DatabaseAdapter.js";
11
+ import { isLegacyDatabases } from "../utils/typeGuards.js";
12
+ import { getAdapter } from "../utils/getClientFromConfig.js";
10
13
 
11
14
  /**
12
15
  * Transfers all documents from one collection to another in a different database
13
16
  * within the same Appwrite Project
14
17
  */
15
- export const transferDocumentsBetweenDbsLocalToLocal = async (
16
- db: Databases,
17
- fromDbId: string,
18
- toDbId: string,
19
- fromCollId: string,
20
- toCollId: string
21
- ) => {
22
- let fromCollDocs = await tryAwaitWithRetry(async () =>
23
- db.listDocuments(fromDbId, fromCollId, [Query.limit(50)])
24
- );
25
- let totalDocumentsTransferred = 0;
26
-
27
- if (fromCollDocs.documents.length === 0) {
28
- MessageFormatter.info(`No documents found in collection ${fromCollId}`, { prefix: "Transfer" });
29
- return;
30
- } else if (fromCollDocs.documents.length < 50) {
31
- const batchedPromises = fromCollDocs.documents.map((doc) => {
32
- const toCreateObject: any = {
33
- ...doc,
34
- };
35
- delete toCreateObject.$databaseId;
36
- delete toCreateObject.$collectionId;
37
- delete toCreateObject.$createdAt;
38
- delete toCreateObject.$updatedAt;
39
- delete toCreateObject.$id;
40
- delete toCreateObject.$permissions;
41
- return tryAwaitWithRetry(
42
- async () =>
43
- await db.createDocument(
44
- toDbId,
45
- toCollId,
46
- doc.$id,
47
- toCreateObject,
48
- doc.$permissions
49
- )
50
- );
51
- });
52
- await Promise.all(batchedPromises);
53
- totalDocumentsTransferred += fromCollDocs.documents.length;
54
- } else {
55
- const batchedPromises = fromCollDocs.documents.map((doc) => {
56
- const toCreateObject: any = {
57
- ...doc,
58
- };
59
- delete toCreateObject.$databaseId;
60
- delete toCreateObject.$collectionId;
61
- delete toCreateObject.$createdAt;
62
- delete toCreateObject.$updatedAt;
63
- delete toCreateObject.$id;
64
- delete toCreateObject.$permissions;
65
- return tryAwaitWithRetry(async () =>
66
- db.createDocument(
67
- toDbId,
68
- toCollId,
69
- doc.$id,
70
- toCreateObject,
71
- doc.$permissions
72
- )
73
- );
74
- });
75
- await Promise.all(batchedPromises);
76
- totalDocumentsTransferred += fromCollDocs.documents.length;
77
- while (fromCollDocs.documents.length === 50) {
78
- fromCollDocs = await tryAwaitWithRetry(
79
- async () =>
80
- await db.listDocuments(fromDbId, fromCollId, [
81
- Query.limit(50),
82
- Query.cursorAfter(
83
- fromCollDocs.documents[fromCollDocs.documents.length - 1].$id
84
- ),
85
- ])
86
- );
87
- const batchedPromises = fromCollDocs.documents.map((doc) => {
88
- const toCreateObject: any = {
89
- ...doc,
90
- };
91
- delete toCreateObject.$databaseId;
92
- delete toCreateObject.$collectionId;
93
- delete toCreateObject.$createdAt;
94
- delete toCreateObject.$updatedAt;
95
- delete toCreateObject.$id;
96
- delete toCreateObject.$permissions;
97
- return tryAwaitWithRetry(
98
- async () =>
99
- await db.createDocument(
100
- toDbId,
101
- toCollId,
102
- doc.$id,
103
- toCreateObject,
104
- doc.$permissions
105
- )
106
- );
107
- });
108
- await Promise.all(batchedPromises);
109
- totalDocumentsTransferred += fromCollDocs.documents.length;
110
- }
111
- }
112
-
113
- MessageFormatter.success(
114
- `Transferred ${totalDocumentsTransferred} documents from database ${fromDbId} to database ${toDbId} -- collection ${fromCollId} to collection ${toCollId}`,
115
- { prefix: "Transfer" }
116
- );
117
- };
18
+ export const transferDocumentsBetweenDbsLocalToLocal = async (
19
+ db: Databases | DatabaseAdapter,
20
+ fromDbId: string,
21
+ toDbId: string,
22
+ fromCollId: string,
23
+ toCollId: string
24
+ ) => {
25
+ // Use adapter path when available for bulk operations
26
+ if (!isLegacyDatabases(db)) {
27
+ const adapter = db as DatabaseAdapter;
28
+
29
+ const pageSize = 1000;
30
+ let lastId: string | undefined;
31
+ let totalTransferred = 0;
32
+
33
+ while (true) {
34
+ const queries = [Query.limit(pageSize)];
35
+ if (lastId) queries.push(Query.cursorAfter(lastId));
36
+
37
+ const result = await adapter.listRows({ databaseId: fromDbId, tableId: fromCollId, queries });
38
+ const rows: any[] = (result as any).rows || (result as any).documents || [];
39
+ if (!rows.length) break;
40
+
41
+ // Prepare rows: strip system fields, keep $id and $permissions
42
+ const prepared = rows.map((doc) => {
43
+ const data: any = { ...doc };
44
+ delete data.$databaseId;
45
+ delete data.$collectionId;
46
+ delete data.$createdAt;
47
+ delete data.$updatedAt;
48
+ return data; // keep $id and $permissions for upsert
49
+ });
50
+
51
+ // Prefer bulk upsert, then bulk create, then individual
52
+ if (typeof (adapter as any).bulkUpsertRows === 'function' && adapter.supportsBulkOperations()) {
53
+ await (adapter as any).bulkUpsertRows({ databaseId: toDbId, tableId: toCollId, rows: prepared });
54
+ } else if (typeof (adapter as any).bulkCreateRows === 'function' && adapter.supportsBulkOperations()) {
55
+ await (adapter as any).bulkCreateRows({ databaseId: toDbId, tableId: toCollId, rows: prepared });
56
+ } else {
57
+ for (const row of prepared) {
58
+ const id = row.$id || ID.unique();
59
+ const permissions = row.$permissions || [];
60
+ const { $id, $permissions, ...data } = row;
61
+ await adapter.createRow({ databaseId: toDbId, tableId: toCollId, id, data, permissions });
62
+ }
63
+ }
64
+
65
+ totalTransferred += rows.length;
66
+ if (rows.length < pageSize) break;
67
+ lastId = rows[rows.length - 1].$id;
68
+ }
69
+
70
+ MessageFormatter.success(
71
+ `Transferred ${totalTransferred} rows from ${fromDbId}/${fromCollId} to ${toDbId}/${toCollId}`,
72
+ { prefix: "Transfer" }
73
+ );
74
+ return;
75
+ }
76
+
77
+ // Legacy path (Databases) – keep existing behavior
78
+ const legacyDb = db as Databases;
79
+ let fromCollDocs = await tryAwaitWithRetry(async () =>
80
+ legacyDb.listDocuments(fromDbId, fromCollId, [Query.limit(50)])
81
+ );
82
+ let totalDocumentsTransferred = 0;
83
+
84
+ if (fromCollDocs.documents.length === 0) {
85
+ MessageFormatter.info(`No documents found in collection ${fromCollId}`, { prefix: "Transfer" });
86
+ return;
87
+ } else if (fromCollDocs.documents.length < 50) {
88
+ const batchedPromises = fromCollDocs.documents.map((doc) => {
89
+ const toCreateObject: any = {
90
+ ...doc,
91
+ };
92
+ delete toCreateObject.$databaseId;
93
+ delete toCreateObject.$collectionId;
94
+ delete toCreateObject.$createdAt;
95
+ delete toCreateObject.$updatedAt;
96
+ delete toCreateObject.$id;
97
+ delete toCreateObject.$permissions;
98
+ return tryAwaitWithRetry(
99
+ async () =>
100
+ await legacyDb.createDocument(
101
+ toDbId,
102
+ toCollId,
103
+ doc.$id,
104
+ toCreateObject,
105
+ doc.$permissions
106
+ )
107
+ );
108
+ });
109
+ await Promise.all(batchedPromises);
110
+ totalDocumentsTransferred += fromCollDocs.documents.length;
111
+ } else {
112
+ const batchedPromises = fromCollDocs.documents.map((doc) => {
113
+ const toCreateObject: any = {
114
+ ...doc,
115
+ };
116
+ delete toCreateObject.$databaseId;
117
+ delete toCreateObject.$collectionId;
118
+ delete toCreateObject.$createdAt;
119
+ delete toCreateObject.$updatedAt;
120
+ delete toCreateObject.$id;
121
+ delete toCreateObject.$permissions;
122
+ return tryAwaitWithRetry(async () =>
123
+ legacyDb.createDocument(
124
+ toDbId,
125
+ toCollId,
126
+ doc.$id,
127
+ toCreateObject,
128
+ doc.$permissions
129
+ )
130
+ );
131
+ });
132
+ await Promise.all(batchedPromises);
133
+ totalDocumentsTransferred += fromCollDocs.documents.length;
134
+ while (fromCollDocs.documents.length === 50) {
135
+ fromCollDocs = await tryAwaitWithRetry(
136
+ async () =>
137
+ await legacyDb.listDocuments(fromDbId, fromCollId, [
138
+ Query.limit(50),
139
+ Query.cursorAfter(
140
+ fromCollDocs.documents[fromCollDocs.documents.length - 1].$id
141
+ ),
142
+ ])
143
+ );
144
+ const batchedPromises = fromCollDocs.documents.map((doc) => {
145
+ const toCreateObject: any = {
146
+ ...doc,
147
+ };
148
+ delete toCreateObject.$databaseId;
149
+ delete toCreateObject.$collectionId;
150
+ delete toCreateObject.$createdAt;
151
+ delete toCreateObject.$updatedAt;
152
+ delete toCreateObject.$id;
153
+ delete toCreateObject.$permissions;
154
+ return tryAwaitWithRetry(
155
+ async () =>
156
+ await legacyDb.createDocument(
157
+ toDbId,
158
+ toCollId,
159
+ doc.$id,
160
+ toCreateObject,
161
+ doc.$permissions
162
+ )
163
+ );
164
+ });
165
+ await Promise.all(batchedPromises);
166
+ totalDocumentsTransferred += fromCollDocs.documents.length;
167
+ }
168
+ }
169
+
170
+ MessageFormatter.success(
171
+ `Transferred ${totalDocumentsTransferred} documents from database ${fromDbId} to database ${toDbId} -- collection ${fromCollId} to collection ${toCollId}`,
172
+ { prefix: "Transfer" }
173
+ );
174
+ };
118
175
 
119
176
  /**
120
177
  * Enhanced document transfer with fault tolerance and exponential backoff
@@ -436,27 +493,24 @@ const transferDocumentBatchWithRetry = async (
436
493
  return await transferDocumentBatchWithRetryFallback(db, dbId, collectionId, documents, batchSize);
437
494
  };
438
495
 
439
- export const transferDocumentsBetweenDbsLocalToRemote = async (
440
- localDb: Databases,
441
- endpoint: string,
442
- projectId: string,
443
- apiKey: string,
444
- fromDbId: string,
445
- toDbId: string,
446
- fromCollId: string,
447
- toCollId: string
448
- ) => {
449
- MessageFormatter.info(`Starting enhanced document transfer from ${fromCollId} to ${toCollId}...`, { prefix: "Transfer" });
450
-
451
- const client = new Client()
452
- .setEndpoint(endpoint)
453
- .setProject(projectId)
454
- .setKey(apiKey);
455
-
456
- const remoteDb = new Databases(client);
457
- let totalDocumentsProcessed = 0;
458
- let totalSuccessful = 0;
459
- let totalFailed = 0;
496
+ export const transferDocumentsBetweenDbsLocalToRemote = async (
497
+ localDb: Databases | DatabaseAdapter,
498
+ endpoint: string,
499
+ projectId: string,
500
+ apiKey: string,
501
+ fromDbId: string,
502
+ toDbId: string,
503
+ fromCollId: string,
504
+ toCollId: string
505
+ ) => {
506
+ MessageFormatter.info(`Starting enhanced document transfer from ${fromCollId} to ${toCollId}...`, { prefix: "Transfer" });
507
+
508
+ // Prefer adapter for remote to enable bulk operations
509
+ const { adapter: remoteAdapter, client } = await getAdapter(endpoint, projectId, apiKey, 'auto');
510
+ const remoteDb = new Databases(client); // Legacy fallback for HTTP/individual
511
+ let totalDocumentsProcessed = 0;
512
+ let totalSuccessful = 0;
513
+ let totalFailed = 0;
460
514
 
461
515
  // Fetch documents in larger batches (1000 at a time)
462
516
  let hasMoreDocuments = true;
@@ -468,9 +522,15 @@ export const transferDocumentsBetweenDbsLocalToRemote = async (
468
522
  queries.push(Query.cursorAfter(lastDocumentId));
469
523
  }
470
524
 
471
- const fromCollDocs = await tryAwaitWithRetry(async () =>
472
- localDb.listDocuments(fromDbId, fromCollId, queries)
473
- );
525
+ const fromCollDocs = await tryAwaitWithRetry(async () => {
526
+ if (isLegacyDatabases(localDb)) {
527
+ return localDb.listDocuments(fromDbId, fromCollId, queries);
528
+ } else {
529
+ const res = await (localDb as DatabaseAdapter).listRows({ databaseId: fromDbId, tableId: fromCollId, queries });
530
+ const rows = (res as any).rows || (res as any).documents || [];
531
+ return { documents: rows } as any;
532
+ }
533
+ });
474
534
 
475
535
  if (fromCollDocs.documents.length === 0) {
476
536
  hasMoreDocuments = false;
@@ -479,13 +539,27 @@ export const transferDocumentsBetweenDbsLocalToRemote = async (
479
539
 
480
540
  MessageFormatter.progress(`Fetched ${fromCollDocs.documents.length} documents, processing for transfer...`, { prefix: "Transfer" });
481
541
 
482
- const { successful, failed } = await transferDocumentBatchWithRetry(
483
- remoteDb,
484
- client,
485
- toDbId,
486
- toCollId,
487
- fromCollDocs.documents
488
- );
542
+ // Prefer remote adapter bulk upsert if available
543
+ const prepared = fromCollDocs.documents.map((doc: any) => {
544
+ const data: any = { ...doc };
545
+ delete data.$databaseId; delete data.$collectionId; delete data.$createdAt; delete data.$updatedAt;
546
+ return data; // Keep $id and $permissions for upsert
547
+ });
548
+
549
+ let successful = 0; let failed = 0;
550
+ if (typeof (remoteAdapter as any).bulkUpsertRows === 'function' && remoteAdapter.supportsBulkOperations()) {
551
+ try {
552
+ await (remoteAdapter as any).bulkUpsertRows({ databaseId: toDbId, tableId: toCollId, rows: prepared });
553
+ successful = prepared.length;
554
+ } catch (e) {
555
+ MessageFormatter.warning('Remote adapter bulk upsert failed, falling back to HTTP/individual', { prefix: 'Transfer' });
556
+ }
557
+ }
558
+
559
+ if (successful === 0) {
560
+ const res = await transferDocumentBatchWithRetry(remoteDb, client, toDbId, toCollId, fromCollDocs.documents);
561
+ successful = res.successful; failed = res.failed;
562
+ }
489
563
 
490
564
  totalDocumentsProcessed += fromCollDocs.documents.length;
491
565
  totalSuccessful += successful;
@@ -513,4 +587,4 @@ export const transferDocumentsBetweenDbsLocalToRemote = async (
513
587
  } else {
514
588
  MessageFormatter.success(message, { prefix: "Transfer" });
515
589
  }
516
- };
590
+ };
@@ -21,7 +21,10 @@ import {
21
21
  import { createYamlLoader } from "../utils/yamlLoader.js";
22
22
  import { YamlImportIntegration } from "../migrations/yaml/YamlImportIntegration.js";
23
23
  import { createImportSchemas } from "../migrations/yaml/generateImportSchemas.js";
24
- import type { CollectionCreate } from "appwrite-utils";
24
+ import {
25
+ CollectionCreateSchema,
26
+ type CollectionCreate
27
+ } from "appwrite-utils";
25
28
  import fs from "fs";
26
29
  import path from "path";
27
30
 
@@ -271,7 +274,7 @@ export async function runYamlTerminologyExamples(outputDir: string): Promise<voi
271
274
 
272
275
  try {
273
276
  // Example collection for demonstrations
274
- const exampleCollection: CollectionCreate = {
277
+ const exampleCollectionInput: CollectionCreate = {
275
278
  name: "Product",
276
279
  $id: "product",
277
280
  enabled: true,
@@ -286,7 +289,7 @@ export async function runYamlTerminologyExamples(outputDir: string): Promise<voi
286
289
  },
287
290
  {
288
291
  key: "price",
289
- type: "float",
292
+ type: "double",
290
293
  required: true,
291
294
  min: 0
292
295
  },
@@ -296,7 +299,8 @@ export async function runYamlTerminologyExamples(outputDir: string): Promise<voi
296
299
  relationType: "manyToOne",
297
300
  relatedCollection: "Categories",
298
301
  twoWay: false,
299
- onDelete: "setNull"
302
+ onDelete: "setNull",
303
+ required: false
300
304
  }
301
305
  ],
302
306
  indexes: [
@@ -308,6 +312,7 @@ export async function runYamlTerminologyExamples(outputDir: string): Promise<voi
308
312
  ],
309
313
  importDefs: []
310
314
  };
315
+ const exampleCollection = CollectionCreateSchema.parse(exampleCollectionInput);
311
316
 
312
317
  // Run examples
313
318
  await generateTemplateExamples(outputDir);
@@ -338,4 +343,4 @@ export async function runYamlTerminologyExamples(outputDir: string): Promise<voi
338
343
  }
339
344
  }
340
345
 
341
- // Note: Functions are already exported above with their declarations
346
+ // Note: Functions are already exported above with their declarations
@@ -0,0 +1,103 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import yaml from 'js-yaml';
4
+ import { homedir } from 'node:os';
5
+ import { AppwriteFunctionSchema, type AppwriteFunction } from 'appwrite-utils';
6
+ import { shouldIgnoreDirectory } from '../utils/directoryUtils.js';
7
+ import { MessageFormatter } from '../shared/messageFormatter.js';
8
+
9
+ function findGitRoot(startDir: string): string {
10
+ let dir = path.resolve(startDir);
11
+ while (dir !== path.parse(dir).root) {
12
+ if (fs.existsSync(path.join(dir, '.git'))) return dir;
13
+ const parent = path.dirname(dir);
14
+ if (parent === dir) break;
15
+ dir = parent;
16
+ }
17
+ return path.resolve(startDir);
18
+ }
19
+
20
+ function expandTilde(p: string): string {
21
+ if (!p) return p;
22
+ if (p === '~' || p.startsWith('~/')) return p.replace(/^~(?=$|\/|\\)/, homedir());
23
+ return p;
24
+ }
25
+
26
+ export function discoverFnConfigs(startDir: string): AppwriteFunction[] {
27
+ const root = findGitRoot(startDir);
28
+ const results: AppwriteFunction[] = [];
29
+
30
+ const visit = (dir: string, depth = 0) => {
31
+ if (depth > 5) return; // cap depth
32
+ const base = path.basename(dir);
33
+ if (shouldIgnoreDirectory(base)) return;
34
+ let entries: fs.Dirent[] = [];
35
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
36
+
37
+ // Check for .fnconfig.yaml / .fnconfig.yml
38
+ for (const fname of ['.fnconfig.yaml', '.fnconfig.yml']) {
39
+ const cfgPath = path.join(dir, fname);
40
+ if (fs.existsSync(cfgPath)) {
41
+ try {
42
+ const raw = fs.readFileSync(cfgPath, 'utf8');
43
+ const data = yaml.load(raw) as any;
44
+ const parsed = AppwriteFunctionSchema.parse({
45
+ $id: data.id || data.$id,
46
+ name: data.name,
47
+ runtime: data.runtime,
48
+ execute: data.execute || [],
49
+ events: data.events || [],
50
+ schedule: data.schedule,
51
+ timeout: data.timeout,
52
+ enabled: data.enabled,
53
+ logging: data.logging,
54
+ entrypoint: data.entrypoint,
55
+ commands: data.commands,
56
+ scopes: data.scopes,
57
+ installationId: data.installationId,
58
+ providerRepositoryId: data.providerRepositoryId,
59
+ providerBranch: data.providerBranch,
60
+ providerSilentMode: data.providerSilentMode,
61
+ providerRootDirectory: data.providerRootDirectory,
62
+ templateRepository: data.templateRepository,
63
+ templateOwner: data.templateOwner,
64
+ templateRootDirectory: data.templateRootDirectory,
65
+ templateVersion: data.templateVersion,
66
+ specification: data.specification,
67
+ dirPath: data.dirPath,
68
+ predeployCommands: data.predeployCommands,
69
+ deployDir: data.deployDir,
70
+ ignore: data.ignore,
71
+ });
72
+
73
+ // Resolve dirPath relative to the config file directory
74
+ let dirPath = parsed.dirPath || '.';
75
+ dirPath = expandTilde(dirPath);
76
+ if (!path.isAbsolute(dirPath)) dirPath = path.resolve(path.dirname(cfgPath), dirPath);
77
+ const merged: AppwriteFunction = { ...parsed, dirPath };
78
+ results.push(merged);
79
+ } catch (e) {
80
+ MessageFormatter.warning(`Failed to parse ${cfgPath}: ${e instanceof Error ? e.message : String(e)}`, { prefix: 'Functions' });
81
+ }
82
+ }
83
+ }
84
+
85
+ for (const entry of entries) {
86
+ if (entry.isDirectory()) visit(path.join(dir, entry.name), depth + 1);
87
+ }
88
+ };
89
+
90
+ visit(root, 0);
91
+ return results;
92
+ }
93
+
94
+ export function mergeDiscoveredFunctions(
95
+ central: AppwriteFunction[] = [],
96
+ discovered: AppwriteFunction[] = []
97
+ ): AppwriteFunction[] {
98
+ const map = new Map<string, AppwriteFunction>();
99
+ for (const f of central) if (f?.$id) map.set(f.$id, f);
100
+ for (const f of discovered) if (f?.$id) map.set(f.$id, f); // discovered overrides
101
+ return Array.from(map.values());
102
+ }
103
+