appwrite-utils-cli 0.9.993 → 0.9.995

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 (46) hide show
  1. package/README.md +1 -2
  2. package/dist/collections/methods.js +6 -4
  3. package/dist/functions/deployments.d.ts +4 -0
  4. package/dist/functions/deployments.js +114 -0
  5. package/dist/functions/methods.d.ts +13 -8
  6. package/dist/functions/methods.js +74 -20
  7. package/dist/functions/templates/count-docs-in-collection/src/main.d.ts +21 -0
  8. package/dist/functions/templates/count-docs-in-collection/src/main.js +114 -0
  9. package/dist/functions/templates/count-docs-in-collection/src/request.d.ts +15 -0
  10. package/dist/functions/templates/count-docs-in-collection/src/request.js +6 -0
  11. package/dist/functions/templates/typescript-node/src/index.d.ts +11 -0
  12. package/dist/functions/templates/typescript-node/src/index.js +11 -0
  13. package/dist/interactiveCLI.d.ts +6 -0
  14. package/dist/interactiveCLI.js +437 -32
  15. package/dist/main.js +0 -0
  16. package/dist/migrations/appwriteToX.js +32 -1
  17. package/dist/migrations/schemaStrings.d.ts +1 -0
  18. package/dist/migrations/schemaStrings.js +74 -2
  19. package/dist/migrations/transfer.js +112 -123
  20. package/dist/utils/loadConfigs.d.ts +1 -0
  21. package/dist/utils/loadConfigs.js +19 -0
  22. package/dist/utils/schemaStrings.js +27 -0
  23. package/dist/utilsController.d.ts +5 -1
  24. package/dist/utilsController.js +54 -2
  25. package/package.json +57 -55
  26. package/src/collections/methods.ts +29 -10
  27. package/src/functions/deployments.ts +190 -0
  28. package/src/functions/methods.ts +295 -235
  29. package/src/functions/templates/count-docs-in-collection/README.md +54 -0
  30. package/src/functions/templates/count-docs-in-collection/src/main.ts +159 -0
  31. package/src/functions/templates/count-docs-in-collection/src/request.ts +9 -0
  32. package/src/functions/templates/poetry/README.md +30 -0
  33. package/src/functions/templates/poetry/pyproject.toml +16 -0
  34. package/src/functions/templates/poetry/src/__init__.py +0 -0
  35. package/src/functions/templates/poetry/src/index.py +16 -0
  36. package/src/functions/templates/typescript-node/README.md +32 -0
  37. package/src/functions/templates/typescript-node/src/index.ts +23 -0
  38. package/src/interactiveCLI.ts +606 -47
  39. package/src/migrations/appwriteToX.ts +44 -1
  40. package/src/migrations/schemaStrings.ts +83 -2
  41. package/src/migrations/transfer.ts +280 -207
  42. package/src/setupController.ts +41 -41
  43. package/src/utils/loadConfigs.ts +24 -0
  44. package/src/utils/schemaStrings.ts +27 -0
  45. package/src/utilsController.ts +63 -3
  46. package/tsconfig.json +8 -1
@@ -9,8 +9,17 @@ import {
9
9
  } from "node-appwrite";
10
10
  import { InputFile } from "node-appwrite/file";
11
11
  import { getAppwriteClient } from "../utils/helperFunctions.js";
12
- import { createOrUpdateAttribute } from "../collections/attributes.js";
12
+ import {
13
+ createOrUpdateAttribute,
14
+ createUpdateCollectionAttributes,
15
+ } from "../collections/attributes.js";
13
16
  import { parseAttribute } from "appwrite-utils";
17
+ import chalk from "chalk";
18
+ import { fetchAllCollections } from "../collections/methods.js";
19
+ import {
20
+ createOrUpdateIndex,
21
+ createOrUpdateIndexes,
22
+ } from "../collections/indexes.js";
14
23
 
15
24
  export interface TransferOptions {
16
25
  fromDb: Models.Database | undefined;
@@ -67,12 +76,12 @@ export const transferStorageLocalToLocal = async (
67
76
  try {
68
77
  await tryAwaitWithRetry(
69
78
  async () =>
70
- await storage.createFile(
71
- toBucketId,
72
- file.$id,
73
- fileToCreate,
74
- file.$permissions
75
- )
79
+ await storage.createFile(
80
+ toBucketId,
81
+ file.$id,
82
+ fileToCreate,
83
+ file.$permissions
84
+ )
76
85
  );
77
86
  } catch (error: any) {
78
87
  // File already exists, so we can skip it
@@ -112,15 +121,18 @@ export const transferStorageLocalToLocal = async (
112
121
  try {
113
122
  await tryAwaitWithRetry(
114
123
  async () =>
115
- await storage.createFile(
116
- toBucketId,
117
- file.$id,
118
- fileToCreate,
119
- file.$permissions
120
- )
124
+ await storage.createFile(
125
+ toBucketId,
126
+ file.$id,
127
+ fileToCreate,
128
+ file.$permissions
129
+ )
121
130
  );
122
131
  } catch (error: any) {
123
132
  // File already exists, so we can skip it
133
+ console.log(
134
+ chalk.yellow(`File ${file.$id} already exists, skipping...`)
135
+ );
124
136
  continue;
125
137
  }
126
138
  numberOfFiles++;
@@ -180,15 +192,16 @@ export const transferStorageLocalToRemote = async (
180
192
  try {
181
193
  await tryAwaitWithRetry(
182
194
  async () =>
183
- await remoteStorage.createFile(
184
- toBucketId,
185
- file.$id,
186
- fileToCreate,
187
- file.$permissions
188
- )
195
+ await remoteStorage.createFile(
196
+ toBucketId,
197
+ file.$id,
198
+ fileToCreate,
199
+ file.$permissions
200
+ )
189
201
  );
190
202
  } catch (error: any) {
191
203
  // File already exists, so we can skip it
204
+ console.log(chalk.yellow(`File ${file.$id} already exists, skipping...`));
192
205
  continue;
193
206
  }
194
207
  numberOfFiles++;
@@ -426,69 +439,64 @@ export const transferDatabaseLocalToLocal = async (
426
439
  fromDbId: string,
427
440
  targetDbId: string
428
441
  ) => {
429
- let lastCollectionId: string | undefined;
430
- let fromCollections = await tryAwaitWithRetry(
431
- async () => await localDb.listCollections(fromDbId, [Query.limit(50)])
432
- );
433
- const allFromCollections = fromCollections.collections;
434
- if (fromCollections.collections.length < 50) {
435
- lastCollectionId = undefined;
436
- } else {
437
- lastCollectionId =
438
- fromCollections.collections[fromCollections.collections.length - 1].$id;
439
- while (lastCollectionId) {
440
- const collections = await localDb.listCollections(fromDbId, [
441
- Query.limit(50),
442
- Query.cursorAfter(lastCollectionId),
443
- ]);
444
- allFromCollections.push(...collections.collections);
445
- if (collections.collections.length < 50) {
446
- break;
447
- }
448
- lastCollectionId =
449
- collections.collections[collections.collections.length - 1].$id;
450
- }
451
- }
452
- lastCollectionId = undefined;
453
- let toCollections = await tryAwaitWithRetry(
454
- async () => await localDb.listCollections(targetDbId, [Query.limit(50)])
442
+ // Get all collections from source database
443
+ const sourceCollections = await fetchAllCollections(fromDbId, localDb);
444
+ console.log(
445
+ chalk.blue(
446
+ `Found ${sourceCollections.length} collections in source database`
447
+ )
455
448
  );
456
- const allToCollections = toCollections.collections;
457
- if (toCollections.collections.length < 50) {
458
- } else {
459
- lastCollectionId =
460
- toCollections.collections[toCollections.collections.length - 1].$id;
461
- while (lastCollectionId) {
462
- const collections = await localDb.listCollections(targetDbId, [
463
- Query.limit(50),
464
- Query.cursorAfter(lastCollectionId),
465
- ]);
466
- allToCollections.push(...collections.collections);
467
- if (collections.collections.length < 50) {
468
- lastCollectionId = undefined;
469
- } else {
470
- lastCollectionId =
471
- collections.collections[collections.collections.length - 1].$id;
472
- }
473
- }
474
- }
475
- for (const collection of allFromCollections) {
476
- const toCollection = allToCollections.find((c) => c.$id === collection.$id);
477
- if (toCollection) {
478
- await transferDocumentsBetweenDbsLocalToLocal(
479
- localDb,
480
- fromDbId,
481
- targetDbId,
482
- collection.$id,
483
- toCollection.$id
484
- );
485
- } else {
486
- console.log(
487
- `Collection ${collection.name} not found in destination database, creating...`
449
+
450
+ // Process each collection
451
+ for (const collection of sourceCollections) {
452
+ console.log(
453
+ chalk.yellow(
454
+ `Processing collection: ${collection.name} (${collection.$id})`
455
+ )
456
+ );
457
+
458
+ try {
459
+ // Create or update collection in target
460
+ let targetCollection: Models.Collection;
461
+ const existingCollection = await tryAwaitWithRetry(async () =>
462
+ localDb.listCollections(targetDbId, [
463
+ Query.equal("$id", collection.$id),
464
+ ])
488
465
  );
489
- const newCollection = await tryAwaitWithRetry(
490
- async () =>
491
- await localDb.createCollection(
466
+
467
+ if (existingCollection.collections.length > 0) {
468
+ targetCollection = existingCollection.collections[0];
469
+ console.log(
470
+ chalk.green(`Collection ${collection.name} exists in target database`)
471
+ );
472
+
473
+ // Update collection if needed
474
+ if (
475
+ targetCollection.name !== collection.name ||
476
+ targetCollection.$permissions !== collection.$permissions ||
477
+ targetCollection.documentSecurity !== collection.documentSecurity ||
478
+ targetCollection.enabled !== collection.enabled
479
+ ) {
480
+ targetCollection = await tryAwaitWithRetry(async () =>
481
+ localDb.updateCollection(
482
+ targetDbId,
483
+ collection.$id,
484
+ collection.name,
485
+ collection.$permissions,
486
+ collection.documentSecurity,
487
+ collection.enabled
488
+ )
489
+ );
490
+ console.log(chalk.green(`Collection ${collection.name} updated`));
491
+ }
492
+ } else {
493
+ console.log(
494
+ chalk.yellow(
495
+ `Creating collection ${collection.name} in target database...`
496
+ )
497
+ );
498
+ targetCollection = await tryAwaitWithRetry(async () =>
499
+ localDb.createCollection(
492
500
  targetDbId,
493
501
  collection.$id,
494
502
  collection.name,
@@ -496,38 +504,91 @@ export const transferDatabaseLocalToLocal = async (
496
504
  collection.documentSecurity,
497
505
  collection.enabled
498
506
  )
507
+ );
508
+ }
509
+
510
+ // Handle attributes
511
+ const existingAttributes = await tryAwaitWithRetry(
512
+ async () =>
513
+ await localDb.listAttributes(targetDbId, targetCollection.$id)
499
514
  );
500
- console.log(`Collection ${newCollection.name} created`);
515
+
501
516
  for (const attribute of collection.attributes) {
502
- await tryAwaitWithRetry(
503
- async () =>
504
- await createOrUpdateAttribute(
517
+ const parsedAttribute = parseAttribute(attribute as any);
518
+ const existingAttribute = existingAttributes.attributes.find(
519
+ (attr: any) => attr.key === parsedAttribute.key
520
+ );
521
+
522
+ if (!existingAttribute) {
523
+ await tryAwaitWithRetry(async () =>
524
+ createOrUpdateAttribute(
505
525
  localDb,
506
526
  targetDbId,
507
- newCollection,
508
- parseAttribute(attribute as any)
527
+ targetCollection,
528
+ parsedAttribute
509
529
  )
510
- );
511
- }
512
- for (const index of collection.indexes) {
513
- await tryAwaitWithRetry(
514
- async () =>
515
- await localDb.createIndex(
530
+ );
531
+ console.log(chalk.green(`Attribute ${parsedAttribute.key} created`));
532
+ } else {
533
+ console.log(
534
+ chalk.blue(
535
+ `Attribute ${parsedAttribute.key} exists, checking for updates...`
536
+ )
537
+ );
538
+ await tryAwaitWithRetry(async () =>
539
+ createOrUpdateAttribute(
540
+ localDb,
516
541
  targetDbId,
517
- newCollection.$id,
518
- index.key,
519
- index.type as IndexType,
520
- index.attributes,
521
- index.orders
542
+ targetCollection,
543
+ parsedAttribute
522
544
  )
545
+ );
546
+ }
547
+ }
548
+
549
+ // Handle indexes
550
+ const existingIndexes = await tryAwaitWithRetry(
551
+ async () => await localDb.listIndexes(targetDbId, targetCollection.$id)
552
+ );
553
+
554
+ for (const index of collection.indexes) {
555
+ const existingIndex = existingIndexes.indexes.find(
556
+ (idx) => idx.key === index.key
523
557
  );
558
+
559
+ if (!existingIndex) {
560
+ await createOrUpdateIndex(
561
+ targetDbId,
562
+ localDb,
563
+ targetCollection.$id,
564
+ index as any
565
+ );
566
+ console.log(chalk.green(`Index ${index.key} created`));
567
+ } else {
568
+ console.log(
569
+ chalk.blue(`Index ${index.key} exists, checking for updates...`)
570
+ );
571
+ await createOrUpdateIndex(
572
+ targetDbId,
573
+ localDb,
574
+ targetCollection.$id,
575
+ index as any
576
+ );
577
+ }
524
578
  }
579
+
580
+ // Transfer documents
525
581
  await transferDocumentsBetweenDbsLocalToLocal(
526
582
  localDb,
527
583
  fromDbId,
528
584
  targetDbId,
529
585
  collection.$id,
530
- newCollection.$id
586
+ targetCollection.$id
587
+ );
588
+ } catch (error) {
589
+ console.error(
590
+ chalk.red(`Error processing collection ${collection.name}:`),
591
+ error
531
592
  );
532
593
  }
533
594
  }
@@ -544,53 +605,44 @@ export const transferDatabaseLocalToRemote = async (
544
605
  const client = getAppwriteClient(endpoint, projectId, apiKey);
545
606
  const remoteDb = new Databases(client);
546
607
 
547
- let lastCollectionId: string | undefined;
548
- let fromCollections = await tryAwaitWithRetry(
549
- async () => await localDb.listCollections(fromDbId, [Query.limit(50)])
608
+ // Get all collections from source database
609
+ const sourceCollections = await fetchAllCollections(fromDbId, localDb);
610
+ console.log(
611
+ chalk.blue(
612
+ `Found ${sourceCollections.length} collections in source database`
613
+ )
550
614
  );
551
- const allFromCollections = fromCollections.collections;
552
- if (fromCollections.collections.length >= 50) {
553
- lastCollectionId =
554
- fromCollections.collections[fromCollections.collections.length - 1].$id;
555
- while (lastCollectionId) {
556
- const collections = await tryAwaitWithRetry(
557
- async () =>
558
- await localDb.listCollections(fromDbId, [
559
- Query.limit(50),
560
- Query.cursorAfter(lastCollectionId!),
561
- ])
562
- );
563
- allFromCollections.push(...collections.collections);
564
- if (collections.collections.length < 50) {
565
- break;
566
- }
567
- lastCollectionId =
568
- collections.collections[collections.collections.length - 1].$id;
569
- }
570
- }
571
615
 
572
- for (const collection of allFromCollections) {
573
- let toCollection: Models.Collection;
574
- const toCollectionExists = await tryAwaitWithRetry(
575
- async () =>
576
- await remoteDb.listCollections(toDbId, [
577
- Query.equal("$id", collection.$id),
578
- ])
616
+ // Process each collection
617
+ for (const collection of sourceCollections) {
618
+ console.log(
619
+ chalk.yellow(
620
+ `Processing collection: ${collection.name} (${collection.$id})`
621
+ )
579
622
  );
580
623
 
581
- if (toCollectionExists.collections.length > 0) {
582
- console.log(`Collection ${collection.name} already exists. Updating...`);
583
- toCollection = toCollectionExists.collections[0];
584
- // Update collection if needed
585
- if (
586
- toCollection.name !== collection.name ||
587
- toCollection.$permissions !== collection.$permissions ||
588
- toCollection.documentSecurity !== collection.documentSecurity ||
589
- toCollection.enabled !== collection.enabled
590
- ) {
591
- toCollection = await tryAwaitWithRetry(
592
- async () =>
593
- await remoteDb.updateCollection(
624
+ try {
625
+ // Create or update collection in target
626
+ let targetCollection: Models.Collection;
627
+ const existingCollection = await tryAwaitWithRetry(async () =>
628
+ remoteDb.listCollections(toDbId, [Query.equal("$id", collection.$id)])
629
+ );
630
+
631
+ if (existingCollection.collections.length > 0) {
632
+ targetCollection = existingCollection.collections[0];
633
+ console.log(
634
+ chalk.green(`Collection ${collection.name} exists in remote database`)
635
+ );
636
+
637
+ // Update collection if needed
638
+ if (
639
+ targetCollection.name !== collection.name ||
640
+ targetCollection.$permissions !== collection.$permissions ||
641
+ targetCollection.documentSecurity !== collection.documentSecurity ||
642
+ targetCollection.enabled !== collection.enabled
643
+ ) {
644
+ targetCollection = await tryAwaitWithRetry(async () =>
645
+ remoteDb.updateCollection(
594
646
  toDbId,
595
647
  collection.$id,
596
648
  collection.name,
@@ -598,13 +650,17 @@ export const transferDatabaseLocalToRemote = async (
598
650
  collection.documentSecurity,
599
651
  collection.enabled
600
652
  )
653
+ );
654
+ console.log(chalk.green(`Collection ${collection.name} updated`));
655
+ }
656
+ } else {
657
+ console.log(
658
+ chalk.yellow(
659
+ `Creating collection ${collection.name} in remote database...`
660
+ )
601
661
  );
602
- console.log(`Collection ${toCollection.name} updated`);
603
- }
604
- } else {
605
- toCollection = await tryAwaitWithRetry(
606
- async () =>
607
- await remoteDb.createCollection(
662
+ targetCollection = await tryAwaitWithRetry(async () =>
663
+ remoteDb.createCollection(
608
664
  toDbId,
609
665
  collection.$id,
610
666
  collection.name,
@@ -612,77 +668,94 @@ export const transferDatabaseLocalToRemote = async (
612
668
  collection.documentSecurity,
613
669
  collection.enabled
614
670
  )
615
- );
616
- console.log(`Collection ${toCollection.name} created`);
617
- }
671
+ );
672
+ }
618
673
 
619
- // Check and update attributes
620
- const existingAttributes = await tryAwaitWithRetry(
621
- async () => await remoteDb.listAttributes(toDbId, toCollection.$id)
622
- );
623
- for (const attribute of collection.attributes) {
624
- const parsedAttribute = parseAttribute(attribute as any);
625
- const existingAttribute = existingAttributes.attributes.find(
626
- // @ts-expect-error
627
- (attr) => attr.key === parsedAttribute.key
674
+ // Handle attributes
675
+ const existingAttributes = await tryAwaitWithRetry(
676
+ async () => await remoteDb.listAttributes(toDbId, targetCollection.$id)
628
677
  );
629
- if (!existingAttribute) {
630
- await tryAwaitWithRetry(
631
- async () =>
632
- await createOrUpdateAttribute(
678
+
679
+ for (const attribute of collection.attributes) {
680
+ const parsedAttribute = parseAttribute(attribute as any);
681
+ const existingAttribute = existingAttributes.attributes.find(
682
+ (attr: any) => attr.key === parsedAttribute.key
683
+ );
684
+
685
+ if (!existingAttribute) {
686
+ await tryAwaitWithRetry(async () =>
687
+ createOrUpdateAttribute(
633
688
  remoteDb,
634
689
  toDbId,
635
- toCollection,
690
+ targetCollection,
636
691
  parsedAttribute
637
692
  )
638
- );
639
- console.log(`Attribute ${parsedAttribute.key} created`);
640
- } else {
641
- // Check if attribute needs updating
642
- // Note: Appwrite doesn't allow updating most attribute properties
643
- // You might need to delete and recreate the attribute if significant changes are needed
644
- console.log(`Attribute ${parsedAttribute.key} already exists`);
693
+ );
694
+ console.log(chalk.green(`Attribute ${parsedAttribute.key} created`));
695
+ } else {
696
+ console.log(
697
+ chalk.blue(
698
+ `Attribute ${parsedAttribute.key} exists, checking for updates...`
699
+ )
700
+ );
701
+ await tryAwaitWithRetry(async () =>
702
+ createOrUpdateAttribute(
703
+ remoteDb,
704
+ toDbId,
705
+ targetCollection,
706
+ parsedAttribute
707
+ )
708
+ );
709
+ }
645
710
  }
646
- }
647
711
 
648
- // Check and update indexes
649
- const existingIndexes = await tryAwaitWithRetry(
650
- async () => await remoteDb.listIndexes(toDbId, toCollection.$id)
651
- );
652
- for (const index of collection.indexes) {
653
- const existingIndex = existingIndexes.indexes.find(
654
- (idx) => idx.key === index.key
712
+ // Handle indexes
713
+ const existingIndexes = await tryAwaitWithRetry(
714
+ async () => await remoteDb.listIndexes(toDbId, targetCollection.$id)
655
715
  );
656
- if (!existingIndex) {
657
- await tryAwaitWithRetry(
658
- async () =>
659
- await remoteDb.createIndex(
660
- toDbId,
661
- toCollection.$id,
662
- index.key,
663
- index.type as IndexType,
664
- index.attributes,
665
- index.orders
666
- )
716
+
717
+ for (const index of collection.indexes) {
718
+ const existingIndex = existingIndexes.indexes.find(
719
+ (idx) => idx.key === index.key
667
720
  );
668
- console.log(`Index ${index.key} created`);
669
- } else {
670
- // Check if index needs updating
671
- // Note: Appwrite doesn't allow updating indexes
672
- // You might need to delete and recreate the index if changes are needed
673
- console.log(`Index ${index.key} already exists`);
721
+
722
+ if (!existingIndex) {
723
+ await createOrUpdateIndex(
724
+ toDbId,
725
+ remoteDb,
726
+ targetCollection.$id,
727
+ index as any
728
+ );
729
+ console.log(chalk.green(`Index ${index.key} created`));
730
+ } else {
731
+ console.log(
732
+ chalk.blue(`Index ${index.key} exists, checking for updates...`)
733
+ );
734
+ await createOrUpdateIndex(
735
+ toDbId,
736
+ remoteDb,
737
+ targetCollection.$id,
738
+ index as any
739
+ );
740
+ }
674
741
  }
675
- }
676
742
 
677
- await transferDocumentsBetweenDbsLocalToRemote(
678
- localDb,
679
- endpoint,
680
- projectId,
681
- apiKey,
682
- fromDbId,
683
- toDbId,
684
- collection.$id,
685
- toCollection.$id
686
- );
743
+ // Transfer documents
744
+ await transferDocumentsBetweenDbsLocalToRemote(
745
+ localDb,
746
+ endpoint,
747
+ projectId,
748
+ apiKey,
749
+ fromDbId,
750
+ toDbId,
751
+ collection.$id,
752
+ targetCollection.$id
753
+ );
754
+ } catch (error) {
755
+ console.error(
756
+ chalk.red(`Error processing collection ${collection.name}:`),
757
+ error
758
+ );
759
+ }
687
760
  }
688
761
  };
@@ -1,42 +1,42 @@
1
- import { setupDirsFiles } from "./utils/setupFiles.js";
2
- import { loadConfig } from "./utils/loadConfigs.js";
3
- import path from "path";
4
- import fs from "fs";
5
- import type { AppwriteConfig } from "appwrite-utils";
6
-
7
- export class SetupController {
8
- private currentDir: string;
9
- private config: AppwriteConfig | null = null;
10
-
11
- constructor(currentDir: string) {
12
- this.currentDir = currentDir;
13
- }
14
-
15
- async runSetup(withExampleData: boolean = false): Promise<void> {
16
- await setupDirsFiles(withExampleData, this.currentDir);
17
- console.log("Setup completed successfully.");
18
- }
19
-
20
- async loadConfig(): Promise<AppwriteConfig | null> {
21
- if (this.hasExistingConfig()) {
22
- try {
23
- const appwriteDir = path.join(this.currentDir, "appwrite");
24
- this.config = await loadConfig(appwriteDir);
25
- return this.config;
26
- } catch (error) {
27
- console.error("Error loading config:", error);
28
- return null;
29
- }
30
- }
31
- return null;
32
- }
33
-
34
- hasExistingConfig(): boolean {
35
- const configPath = path.join(
36
- this.currentDir,
37
- "appwrite",
38
- "appwriteConfig.ts"
39
- );
40
- return fs.existsSync(configPath);
41
- }
1
+ import { setupDirsFiles } from "./utils/setupFiles.js";
2
+ import { loadConfig } from "./utils/loadConfigs.js";
3
+ import path from "path";
4
+ import fs from "fs";
5
+ import type { AppwriteConfig } from "appwrite-utils";
6
+
7
+ export class SetupController {
8
+ private currentDir: string;
9
+ private config: AppwriteConfig | null = null;
10
+
11
+ constructor(currentDir: string) {
12
+ this.currentDir = currentDir;
13
+ }
14
+
15
+ async runSetup(withExampleData: boolean = false): Promise<void> {
16
+ await setupDirsFiles(withExampleData, this.currentDir);
17
+ console.log("Setup completed successfully.");
18
+ }
19
+
20
+ async loadConfig(): Promise<AppwriteConfig | null> {
21
+ if (this.hasExistingConfig()) {
22
+ try {
23
+ const appwriteDir = path.join(this.currentDir, "appwrite");
24
+ this.config = await loadConfig(appwriteDir);
25
+ return this.config;
26
+ } catch (error) {
27
+ console.error("Error loading config:", error);
28
+ return null;
29
+ }
30
+ }
31
+ return null;
32
+ }
33
+
34
+ hasExistingConfig(): boolean {
35
+ const configPath = path.join(
36
+ this.currentDir,
37
+ "appwrite",
38
+ "appwriteConfig.ts"
39
+ );
40
+ return fs.existsSync(configPath);
41
+ }
42
42
  }