@@ -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 { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
5
+ const { questionsDeployBuckets, 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 {
@@ -25,6 +25,9 @@ const {
25
25
databasesListIndexes,
26
26
databasesDeleteIndex
27
27
} = require("./databases");
28
+ const {
29
+ storageGetBucket, storageUpdateBucket, storageCreateBucket
30
+ } = require("./storage");
28
31
29
32
const POOL_DEBOUNCE = 2000; // in milliseconds
30
33
const POOL_MAX_DEBOUNCES = 30;
@@ -593,6 +596,93 @@ const deployCollection = async ({ all, yes } = {}) => {
593
596
}
594
597
}
595
598
599
+ const deployBucket = async ({ all, yes } = {}) => {
600
+ let response = {};
601
+
602
+ let bucketIds = [];
603
+ const configBuckets = localConfig.getBuckets();
604
+
605
+ if(all) {
606
+ if (configBuckets.length === 0) {
607
+ throw new Error("No buckets found in the current directory. Run `appwrite init bucket` to fetch all your buckets.");
608
+ }
609
+ bucketIds.push(...configBuckets.map((c) => c.$id));
610
+ }
611
+
612
+ if(bucketIds.length < = 0) {
613
+ let answers = await inquirer.prompt(questionsDeployBuckets[0])
614
+ bucketIds.push(...answers.buckets);
615
+ }
616
+
617
+ let buckets = [];
618
+
619
+ for(const bucketId of bucketIds) {
620
+ const idBuckets = configBuckets.filter((b) => b.$id === bucketId);
621
+ buckets.push(...idBuckets);
622
+ }
623
+
624
+ for (let bucket of buckets) {
625
+ log(`Deploying bucket ${bucket.name} ( ${bucket['$id']} )`)
626
+
627
+ try {
628
+ response = await storageGetBucket({
629
+ bucketId: bucket['$id'],
630
+ parseOutput: false,
631
+ })
632
+ log(`Bucket ${bucket.name} ( ${bucket['$id']} ) already exists.`);
633
+
634
+ if(!yes) {
635
+ answers = await inquirer.prompt(questionsDeployBuckets[1])
636
+ if (answers.override !== "YES") {
637
+ log(`Received "${answers.override}". Skipping ${bucket.name} ( ${bucket['$id']} )`);
638
+ continue;
639
+ }
640
+ }
641
+
642
+ log(`Updating bucket ...`)
643
+
644
+ await storageUpdateBucket({
645
+ bucketId: bucket['$id'],
646
+ name: bucket.name,
647
+ permissions: bucket['$permissions'],
648
+ fileSecurity: bucket.fileSecurity,
649
+ enabled: bucket.enabled,
650
+ maximumFileSize: bucket.maximumFileSize,
651
+ allowedFileExtensions: bucket.allowedFileExtensions,
652
+ compression: bucket.compression,
653
+ encryption: bucket.encryption,
654
+ antivirus: bucket.antivirus,
655
+ compression: bucket.compression,
656
+ parseOutput: false
657
+ });
658
+
659
+ success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
660
+ } catch (e) {
661
+ if (e.code == 404) {
662
+ log(`Bucket ${bucket.name} does not exist in the project. Creating ... `);
663
+
664
+ response = await storageCreateBucket({
665
+ bucketId: bucket['$id'],
666
+ name: bucket.name,
667
+ permissions: bucket['$permissions'],
668
+ fileSecurity: bucket.fileSecurity,
669
+ enabled: bucket.enabled,
670
+ maximumFileSize: bucket.maximumFileSize,
671
+ allowedFileExtensions: bucket.allowedFileExtensions,
672
+ compression: bucket.compression,
673
+ encryption: bucket.encryption,
674
+ antivirus: bucket.antivirus,
675
+ parseOutput: false
676
+ })
677
+
678
+ success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
679
+ } else {
680
+ throw e;
681
+ }
682
+ }
683
+ }
684
+ }
685
+
596
686
deploy
597
687
.command("function")
598
688
.description("Deploy functions in the current directory.")
@@ -608,6 +698,13 @@ deploy
608
698
.option(`--yes`, `Flag to confirm all warnings`)
609
699
.action(actionRunner(deployCollection));
610
700
701
+ deploy
702
+ .command("bucket")
703
+ .description("Deploy buckets in the current project.")
704
+ .option(`--all`, `Flag to deploy all buckets`)
705
+ .option(`--yes`, `Flag to confirm all warnings`)
706
+ .action(actionRunner(deployBucket));
707
+
611
708
module.exports = {
612
709
deploy
613
710
}
0 commit comments