Skip to content

Commit 416ed81

Browse files
committed
Merge branch 'refs/heads/feat-adding-messaging' into feat-deploy-init-everything
# Conflicts: # templates/cli/lib/commands/push.js.twig
2 parents cda1847 + e44eec0 commit 416ed81

File tree

4 files changed

+178
-6
lines changed

4 files changed

+178
-6
lines changed

templates/cli/lib/commands/pull.js.twig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require("path");
33
const childProcess = require('child_process');
44
const { Command } = require("commander");
55
const inquirer = require("inquirer");
6+
const { messagingCreateTopic, messagingListTopics } = require("./messaging");
67
const { teamsCreate, teamsList } = require("./teams");
78
const { projectsCreate } = require("./projects");
89
const { functionsCreate } = require("./functions");
@@ -237,6 +238,19 @@ const pullTeam = async () => {
237238
success();
238239
}
239240

241+
const pullMessagingTopic = async () => {
242+
const { topics } = await paginate(messagingListTopics, { parseOutput: false }, 100, 'topics');
243+
244+
log(`Found ${topics.length} topics`);
245+
246+
topics.forEach(async topic => {
247+
log(`Pulling ${topic.name} ...`);
248+
localConfig.addMessagingTopic(topic);
249+
});
250+
251+
success();
252+
}
253+
240254
pull
241255
.command("project")
242256
.description("Pulling your {{ spec.title|caseUcfirst }} project")
@@ -264,6 +278,11 @@ pull
264278
.description("Pulling your Appwrite teams")
265279
.action(actionRunner(pullTeam))
266280

281+
pull
282+
.command("topic")
283+
.description("Initialise your Appwrite messaging topics")
284+
.action(actionRunner(pullMessagingTopic))
285+
267286
module.exports = {
268287
pull,
269288
};

templates/cli/lib/commands/push.js.twig

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const JSONbig = require("json-bigint")({ storeAsString: false });
33
const { Command } = require("commander");
44
const { localConfig } = require("../config");
55
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");
77
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
88
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
99
const {
@@ -32,6 +32,9 @@ const {
3232
const {
3333
storageGetBucket, storageUpdateBucket, storageCreateBucket
3434
} = require("./storage");
35+
const {
36+
messagingGetTopic, messagingUpdateTopic, messagingCreateTopic
37+
} = require("./messaging");
3538
const {
3639
teamsGet,
3740
teamsUpdate,
@@ -227,6 +230,12 @@ const awaitPools = {
227230
},
228231
}
229232

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+
230239
const pushResources = async ({ all, yes } = {}) => {
231240
const actions = {
232241
functions: pushFunction,
@@ -920,11 +929,83 @@ const pushTeam = async ({ all, yes } = {}) => {
920929
}
921930
}
922931

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+
9281009

9291010
push
9301011
.command("function")
@@ -955,6 +1036,13 @@ push
9551036
.option(`--yes`, `Flag to confirm all warnings`)
9561037
.action(actionRunner(pushTeam));
9571038

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+
9581046
module.exports = {
9591047
push
9601048
}

templates/cli/lib/config.js.twig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,45 @@ class Local extends Config {
204204
this.set("buckets", buckets);
205205
}
206206

207+
getMessagingTopics() {
208+
if (!this.has("topics")) {
209+
return [];
210+
}
211+
return this.get("topics");
212+
}
213+
214+
getMessagingTopic($id) {
215+
if (!this.has("topics")) {
216+
return {};
217+
}
218+
219+
let topic = this.get("topics");
220+
for (let i = 0; i < topic.length; i++) {
221+
if (topic[i]['$id'] == $id) {
222+
return topic[i];
223+
}
224+
}
225+
226+
return {};
227+
}
228+
229+
addMessagingTopic(props) {
230+
if (!this.has("topics")) {
231+
this.set("topics", []);
232+
}
233+
234+
let topics = this.get("topics");
235+
for (let i = 0; i < topics.length; i++) {
236+
if (topics[i]['$id'] == props['$id']) {
237+
topics[i] = props;
238+
this.set("topics", topics);
239+
return;
240+
}
241+
}
242+
topics.push(props);
243+
this.set("topics", topics);
244+
}
245+
207246
getDatabases() {
208247
if (!this.has("databases")) {
209248
return [];

templates/cli/lib/questions.js.twig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,31 @@ const questionsPushBuckets = [
371371
},
372372
]
373373

374+
const questionsPushMessagingTopics = [
375+
{
376+
type: "checkbox",
377+
name: "topics",
378+
message: "Which messaging topic would you like to push?",
379+
choices: () => {
380+
let topics = localConfig.getMessagingTopics();
381+
if (topics.length === 0) {
382+
throw new Error("No topics found in the current directory. Run `appwrite pull messaging` to fetch all your messaging topics.");
383+
}
384+
return topics.map(topic => {
385+
return {
386+
name: `${topic.name} (${topic['$id']})`,
387+
value: topic.$id
388+
}
389+
});
390+
}
391+
},
392+
{
393+
type: "input",
394+
name: "override",
395+
message: 'What you like to override existing topics? This can lead to loss of data! Type "YES" to confirm.'
396+
}
397+
]
398+
374399
const questionsGetEntrypoint = [
375400
{
376401
type: "input",
@@ -471,6 +496,7 @@ module.exports = {
471496
questionsPushFunctions,
472497
questionsPushCollections,
473498
questionsPushBuckets,
499+
questionsPushMessagingTopics,
474500
questionsPushTeams,
475501
questionsGetEntrypoint,
476502
questionsListFactors,

0 commit comments

Comments
 (0)