@@ -3,7 +3,7 @@ const JSONbig = require("json-bigint")({ storeAsString: false });
3
3
const { Command } = require("commander");
4
4
const { localConfig } = require("../config");
5
5
const { paginate } = require('../paginate');
6
- const { questionsPushResources, questionsPushBuckets, questionsPushTeams, questionsPushFunctions, questionsGetEntrypoint, questionsPushCollections, questionsConfirmPushCollections } = require("../questions");
6
+ const { questionsPushBuckets, questionsPushTeams, questionsPushFunctions, questionsGetEntrypoint, questionsPushCollections, questionsConfirmPushCollections, questionsPushMessagingTopics } = require("../questions");
7
7
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
8
8
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
9
9
const {
@@ -32,6 +32,9 @@ const {
32
32
const {
33
33
storageGetBucket, storageUpdateBucket, storageCreateBucket
34
34
} = require("./storage");
35
+ const {
36
+ messagingGetTopic, messagingUpdateTopic, messagingCreateTopic
37
+ } = require("./messaging");
35
38
const {
36
39
teamsGet,
37
40
teamsUpdate,
@@ -227,6 +230,12 @@ const awaitPools = {
227
230
},
228
231
}
229
232
233
+ const push = new Command("push")
234
+ .description(commandDescriptions['push'])
235
+ .option(`--all`, `Flag to push all resources`)
236
+ .option(`--yes`, `Flag to confirm all warnings`)
237
+ .action(actionRunner(pushResources));
238
+
230
239
const pushResources = async ({ all, yes } = {}) => {
231
240
const actions = {
232
241
functions: pushFunction,
@@ -920,11 +929,83 @@ const pushTeam = async ({ all, yes } = {}) => {
920
929
}
921
930
}
922
931
923
- const push = new Command("push")
924
- .description(commandDescriptions['push'])
925
- .option(`--all`, `Flag to push all resources`)
926
- .option(`--yes`, `Flag to confirm all warnings`)
927
- .action(actionRunner(pushResources));
932
+ const pushMessagingTopic = async ({ all, yes } = {}) => {
933
+ let response = {};
934
+
935
+ let topicsIds = [];
936
+ const configTopics = localConfig.getMessagingTopics();
937
+ let overrideExisting = yes;
938
+
939
+ if (all) {
940
+ if (configTopics.length === 0) {
941
+ throw new Error("No topics found in the current directory. Run `appwrite pull topics` to pull all your messaging topics.");
942
+ }
943
+ topicsIds.push(...configTopics.map((b) => b.$id));
944
+ }
945
+
946
+ if (topicsIds.length === 0) {
947
+ const answers = await inquirer.prompt(questionsPushMessagingTopics[0])
948
+ topicsIds.push(...answers.topics);
949
+ }
950
+
951
+ let topics = [];
952
+
953
+ for (const topicId of topicsIds) {
954
+ const idTopic = configTopics.filter((b) => b.$id === topicId);
955
+ topics.push(...idTopic);
956
+ }
957
+
958
+ if (!yes) {
959
+ const answers = await inquirer.prompt(questionsPushMessagingTopics[1])
960
+ if (answers.override.toLowerCase() === "yes") {
961
+ overrideExisting = true;
962
+ }
963
+ }
964
+
965
+ for (let topic of topics) {
966
+ log(`Pushing topic ${topic.name} ( ${topic['$id']} )`)
967
+
968
+ try {
969
+ response = await messagingGetTopic({
970
+ topicId: topic['$id'],
971
+ parseOutput: false
972
+ })
973
+ log(`Topic ${topic.name} ( ${topic['$id']} ) already exists.`);
974
+
975
+ if (!overrideExisting) {
976
+ log(`Skipping ${topic.name} ( ${topic['$id']} )`);
977
+ continue;
978
+ }
979
+
980
+ log(`Updating Topic ...`)
981
+
982
+ await messagingUpdateTopic({
983
+ topicId: topic['$id'],
984
+ name: topic.name,
985
+ subscribe: topic.subscribe,
986
+ parseOutput: false
987
+ });
988
+
989
+ success(`Pushed ${topic.name} ( ${topic['$id']} )`);
990
+ } catch (e) {
991
+ if (e.code == 404) {
992
+ log(`Topic ${topic.name} does not exist in the project. Creating ... `);
993
+
994
+ response = await messagingCreateTopic({
995
+ topicId: topic['$id'],
996
+ name: topic.name,
997
+ subscribe: topic.subscribe,
998
+ parseOutput: false
999
+ })
1000
+
1001
+ success(`Pushed ${topic.name} ( ${topic['$id']} )`);
1002
+ } else {
1003
+ throw e;
1004
+ }
1005
+ }
1006
+ }
1007
+ }
1008
+
928
1009
929
1010
push
930
1011
.command("function")
@@ -955,6 +1036,13 @@ push
955
1036
.option(`--yes`, `Flag to confirm all warnings`)
956
1037
.action(actionRunner(pushTeam));
957
1038
1039
+ push
1040
+ .command("topic")
1041
+ .description("Push messaging topics in the current project.")
1042
+ .option(`--all`, `Flag to deploy all topics`)
1043
+ .option(`--yes`, `Flag to confirm all warnings`)
1044
+ .action(actionRunner(pushMessagingTopic));
1045
+
958
1046
module.exports = {
959
1047
push
960
1048
}
0 commit comments