@@ -2,7 +2,7 @@ const inquirer = require("inquirer");
2
2
const JSONbig = require("json-bigint")({ storeAsString: false });
3
3
const { Command } = require("commander");
4
4
const { localConfig } = require("../config");
5
- const { questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
5
+ const { questionsDeployBuckets, questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
6
6
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
7
7
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
8
8
const {
@@ -26,6 +26,9 @@ const {
26
26
databasesDeleteIndex,
27
27
databasesUpdateCollection
28
28
} = require("./databases");
29
+ const {
30
+ storageGetBucket, storageUpdateBucket, storageCreateBucket
31
+ } = require("./storage");
29
32
const {
30
33
teamsGet,
31
34
teamsUpdate,
@@ -624,6 +627,93 @@ const deployCollection = async ({ all, yes } = {}) => {
624
627
}
625
628
}
626
629
630
+ const deployBucket = async ({ all, yes } = {}) => {
631
+ let response = {};
632
+
633
+ let bucketIds = [];
634
+ const configBuckets = localConfig.getBuckets();
635
+
636
+ if(all) {
637
+ if (configBuckets.length === 0) {
638
+ throw new Error("No buckets found in the current directory. Run `appwrite init bucket` to fetch all your buckets.");
639
+ }
640
+ bucketIds.push(...configBuckets.map((b) => b.$id));
641
+ }
642
+
643
+ if(bucketIds.length === 0) {
644
+ let answers = await inquirer.prompt(questionsDeployBuckets[0])
645
+ bucketIds.push(...answers.buckets);
646
+ }
647
+
648
+ let buckets = [];
649
+
650
+ for(const bucketId of bucketIds) {
651
+ const idBuckets = configBuckets.filter((b) => b.$id === bucketId);
652
+ buckets.push(...idBuckets);
653
+ }
654
+
655
+ for (let bucket of buckets) {
656
+ log(`Deploying bucket ${bucket.name} ( ${bucket['$id']} )`)
657
+
658
+ try {
659
+ response = await storageGetBucket({
660
+ bucketId: bucket['$id'],
661
+ parseOutput: false,
662
+ })
663
+ log(`Bucket ${bucket.name} ( ${bucket['$id']} ) already exists.`);
664
+
665
+ if(!yes) {
666
+ answers = await inquirer.prompt(questionsDeployBuckets[1])
667
+ if (answers.override !== "YES") {
668
+ log(`Received "${answers.override}". Skipping ${bucket.name} ( ${bucket['$id']} )`);
669
+ continue;
670
+ }
671
+ }
672
+
673
+ log(`Updating bucket ...`)
674
+
675
+ await storageUpdateBucket({
676
+ bucketId: bucket['$id'],
677
+ name: bucket.name,
678
+ permissions: bucket['$permissions'],
679
+ fileSecurity: bucket.fileSecurity,
680
+ enabled: bucket.enabled,
681
+ maximumFileSize: bucket.maximumFileSize,
682
+ allowedFileExtensions: bucket.allowedFileExtensions,
683
+ compression: bucket.compression,
684
+ encryption: bucket.encryption,
685
+ antivirus: bucket.antivirus,
686
+ compression: bucket.compression,
687
+ parseOutput: false
688
+ });
689
+
690
+ success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
691
+ } catch (e) {
692
+ if (e.code == 404) {
693
+ log(`Bucket ${bucket.name} does not exist in the project. Creating ... `);
694
+
695
+ response = await storageCreateBucket({
696
+ bucketId: bucket['$id'],
697
+ name: bucket.name,
698
+ permissions: bucket['$permissions'],
699
+ fileSecurity: bucket.fileSecurity,
700
+ enabled: bucket.enabled,
701
+ maximumFileSize: bucket.maximumFileSize,
702
+ allowedFileExtensions: bucket.allowedFileExtensions,
703
+ compression: bucket.compression,
704
+ encryption: bucket.encryption,
705
+ antivirus: bucket.antivirus,
706
+ parseOutput: false
707
+ })
708
+
709
+ success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
710
+ } else {
711
+ throw e;
712
+ }
713
+ }
714
+ }
715
+ }
716
+
627
717
const deployTeam = async ({ all, yes } = {}) => {
628
718
let response = {};
629
719
@@ -709,6 +799,13 @@ deploy
709
799
.option(`--yes`, `Flag to confirm all warnings`)
710
800
.action(actionRunner(deployCollection));
711
801
802
+ deploy
803
+ .command("bucket")
804
+ .description("Deploy buckets in the current project.")
805
+ .option(`--all`, `Flag to deploy all buckets`)
806
+ .option(`--yes`, `Flag to confirm all warnings`)
807
+ .action(actionRunner(deployBucket));
808
+
712
809
deploy
713
810
.command("team")
714
811
.description("Deploy teams in the current project.")
0 commit comments