@@ -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 { 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,14 +230,27 @@ const awaitPools = {
227
230
},
228
231
}
229
232
230
- const push = new Command("push")
231
- .description(commandDescriptions['push'])
232
- .configureHelp({
233
- helpWidth: process.stdout.columns || 80
234
- })
235
- .action(actionRunner(async (_options, command) => {
236
- command.help()
237
- }));
233
+ const pushResources = async ({ all, yes } = {}) => {
234
+ const actions = {
235
+ functions: pushFunction,
236
+ collections: pushCollection,
237
+ buckets: pushBucket,
238
+ teams: pushTeam,
239
+ messages: pushMessagingTopic
240
+ }
241
+
242
+ if (all) {
243
+ Object.values(actions).forEach(action => action({ all: true, yes }));
244
+ } else {
245
+ const answers = await inquirer.prompt(questionsPushResources[0]);
246
+ answers.resources.forEach((resource) => {
247
+ const action = actions[resource];
248
+ if (action !== undefined) {
249
+ action({ all: true, yes });
250
+ }
251
+ })
252
+ }
253
+ };
238
254
239
255
const pushFunction = async ({ functionId, all, yes } = {}) => {
240
256
let response = {};
@@ -907,6 +923,89 @@ const pushTeam = async ({ all, yes } = {}) => {
907
923
}
908
924
}
909
925
926
+ const pushMessagingTopic = async ({ all, yes } = {}) => {
927
+ let response = {};
928
+
929
+ let topicsIds = [];
930
+ const configTopics = localConfig.getMessagingTopics();
931
+ let overrideExisting = yes;
932
+
933
+ if (all) {
934
+ if (configTopics.length === 0) {
935
+ throw new Error("No topics found in the current directory. Run `appwrite pull topics` to pull all your messaging topics.");
936
+ }
937
+ topicsIds.push(...configTopics.map((b) => b.$id));
938
+ }
939
+
940
+ if (topicsIds.length === 0) {
941
+ const answers = await inquirer.prompt(questionsPushMessagingTopics[0])
942
+ topicsIds.push(...answers.topics);
943
+ }
944
+
945
+ let topics = [];
946
+
947
+ for (const topicId of topicsIds) {
948
+ const idTopic = configTopics.filter((b) => b.$id === topicId);
949
+ topics.push(...idTopic);
950
+ }
951
+
952
+ if (!yes) {
953
+ const answers = await inquirer.prompt(questionsPushMessagingTopics[1])
954
+ if (answers.override.toLowerCase() === "yes") {
955
+ overrideExisting = true;
956
+ }
957
+ }
958
+
959
+ for (let topic of topics) {
960
+ log(`Pushing topic ${topic.name} ( ${topic['$id']} )`)
961
+
962
+ try {
963
+ response = await messagingGetTopic({
964
+ topicId: topic['$id'],
965
+ parseOutput: false
966
+ })
967
+ log(`Topic ${topic.name} ( ${topic['$id']} ) already exists.`);
968
+
969
+ if (!overrideExisting) {
970
+ log(`Skipping ${topic.name} ( ${topic['$id']} )`);
971
+ continue;
972
+ }
973
+
974
+ log(`Updating Topic ...`)
975
+
976
+ await messagingUpdateTopic({
977
+ topicId: topic['$id'],
978
+ name: topic.name,
979
+ subscribe: topic.subscribe,
980
+ parseOutput: false
981
+ });
982
+
983
+ success(`Pushed ${topic.name} ( ${topic['$id']} )`);
984
+ } catch (e) {
985
+ if (e.code == 404) {
986
+ log(`Topic ${topic.name} does not exist in the project. Creating ... `);
987
+
988
+ response = await messagingCreateTopic({
989
+ topicId: topic['$id'],
990
+ name: topic.name,
991
+ subscribe: topic.subscribe,
992
+ parseOutput: false
993
+ })
994
+
995
+ success(`Created ${topic.name} ( ${topic['$id']} )`);
996
+ } else {
997
+ throw e;
998
+ }
999
+ }
1000
+ }
1001
+ }
1002
+
1003
+ const push = new Command("push")
1004
+ .description(commandDescriptions['push'])
1005
+ .option(`--all`, `Flag to push all resources`)
1006
+ .option(`--yes`, `Flag to confirm all warnings`)
1007
+ .action(actionRunner(pushResources));
1008
+
910
1009
push
911
1010
.command("function")
912
1011
.description("Push functions in the current directory.")
@@ -936,6 +1035,13 @@ push
936
1035
.option(`--yes`, `Flag to confirm all warnings`)
937
1036
.action(actionRunner(pushTeam));
938
1037
1038
+ push
1039
+ .command("topic")
1040
+ .description("Push messaging topics in the current project.")
1041
+ .option(`--all`, `Flag to deploy all topics`)
1042
+ .option(`--yes`, `Flag to confirm all warnings`)
1043
+ .action(actionRunner(pushMessagingTopic));
1044
+
939
1045
module.exports = {
940
1046
push
941
1047
}
0 commit comments