@@ -2,7 +2,7 @@ const inquirer = require("inquirer");
22const JSONbig = require("json-bigint")({ storeAsString: false });
33const { Command } = require("commander");
44const { localConfig } = require("../config");
5- const { questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
5+ const { questionsDeployBuckets, questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
66const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
77const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
88const {
@@ -26,6 +26,9 @@ const {
2626 databasesDeleteIndex,
2727 databasesUpdateCollection
2828} = require("./databases");
29+ const {
30+ storageGetBucket, storageUpdateBucket, storageCreateBucket
31+ } = require("./storage");
2932const {
3033 teamsGet,
3134 teamsUpdate,
@@ -624,6 +627,93 @@ const deployCollection = async ({ all, yes } = {}) => {
624627 }
625628}
626629
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+
627717const deployTeam = async ({ all, yes } = {}) => {
628718 let response = {};
629719
@@ -709,6 +799,13 @@ deploy
709799 .option(`--yes`, `Flag to confirm all warnings`)
710800 .action(actionRunner(deployCollection));
711801
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+
712809deploy
713810 .command("team")
714811 .description("Deploy teams in the current project.")
0 commit comments