Skip to content

Commit c82609d

Browse files
committed
refactor(cli): Moving --all logic into questions
1 parent 9096fae commit c82609d

File tree

3 files changed

+264
-174
lines changed

3 files changed

+264
-174
lines changed

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { databasesGet, databasesListCollections, databasesList } = require("./dat
1111
const { storageListBuckets } = require("./storage");
1212
const { sdkForConsole } = require("../sdks");
1313
const { localConfig } = require("../config");
14-
const ID = require("../id");
14+
const ID = require("../id");
1515
const { paginate } = require("../paginate");
1616
const { questionsPullProject, questionsPullFunction, questionsPullCollection } = require("../questions");
1717
const { cliConfig, success, log, actionRunner, commandDescriptions } = require("../parser");
@@ -164,26 +164,12 @@ const pullFunction = async () => {
164164
success();
165165
}
166166

167-
const pullCollection = async ({ databaseId } = {}) => {
168-
const databaseIds = [];
167+
const pullCollection = async () => {
168+
const databasesIds = (await inquirer.prompt(questionsPullCollection)).databases;
169169

170-
if (databaseId) {
171-
databaseIds.push(databaseId);
172-
} else if (cliConfig.all) {
173-
let allDatabases = await databasesList({
174-
parseOutput: false
175-
})
176-
177-
databaseIds.push(...allDatabases.databases.map((d) => d.$id));
178-
}
170+
for (let dbID of databasesIds) {
171+
const databaseId = dbID.value ? dbID.value : dbID;
179172

180-
if (databaseIds.length <= 0) {
181-
let answers = await inquirer.prompt(questionsPullCollection)
182-
if (!answers.databases) process.exit(1)
183-
databaseIds.push(...answers.databases);
184-
}
185-
186-
for (const databaseId of databaseIds) {
187173
const database = await databasesGet({
188174
databaseId,
189175
parseOutput: false
@@ -198,14 +184,14 @@ const pullCollection = async ({ databaseId } = {}) => {
198184

199185
log(`Found ${total} collections`);
200186

201-
collections.forEach(async collection => {
187+
await Promise.all(collections.map(async collection => {
202188
log(`Fetching ${collection.name} ...`);
203189
localConfig.addCollection({
204190
...collection,
205191
'$createdAt': undefined,
206192
'$updatedAt': undefined,
207193
});
208-
});
194+
}))
209195
}
210196

211197
success();
@@ -264,7 +250,6 @@ pull
264250
pull
265251
.command("collection")
266252
.description("Pulling your {{ spec.title|caseUcfirst }} collections")
267-
.option(`--databaseId <databaseId>`, `Database ID`)
268253
.action(actionRunner(pullCollection))
269254

270255
pull

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

Lines changed: 56 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ const { Command } = require("commander");
44
const { localConfig, globalConfig } = require("../config");
55
const { Spinner, SPINNER_ARC, SPINNER_DOTS } = require('../spinner');
66
const { paginate } = require('../paginate');
7-
const { questionsPushBuckets, questionsPushTeams, questionsPushFunctions, questionsGetEntrypoint, questionsPushCollections, questionsConfirmPushCollections, questionsPushMessagingTopics } = require("../questions");
8-
const { cliConfig, actionRunner, success, log, error, commandDescriptions } = require("../parser");
7+
const {
8+
questionsPushBuckets,
9+
questionsPushTeams,
10+
questionsPushFunctions,
11+
questionsGetEntrypoint,
12+
questionsPushCollections,
13+
questionsPushResources,
14+
questionsPushMessagingTopics
15+
} = require("../questions");
16+
const {
17+
cliConfig, actionRunner,
18+
success, log, error, commandDescriptions
19+
} = require("../parser");
920
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsGetDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
1021
const {
1122
databasesGet,
@@ -241,43 +252,29 @@ const pushResources = async () => {
241252
messages: pushMessagingTopic
242253
}
243254

244-
if (cliConfig.all) {
245-
Object.values(actions).forEach(action => action());
246-
} else {
247-
const answers = await inquirer.prompt(questionsPushResources[0]);
255+
const answers = await inquirer.prompt(questionsPushResources);
248256

249-
answers.resources.forEach((resource) => {
250-
const action = actions[resource];
251-
if (action !== undefined) {
252-
action();
257+
for (const resource of answers.resources) {
258+
const method = resource.value ? resource.value : resource;
259+
const action = actions[method];
260+
if (action !== undefined) {
261+
try {
262+
await action();
263+
} catch (e) {
264+
error(e);
253265
}
254-
})
266+
}
255267
}
256268
};
257269

258-
const pushFunction = async ({ functionId, async } = {}) => {
270+
const pushFunction = async ({ async } = {}) => {
259271
let response = {};
260272

261-
const functionIds = [];
273+
const functionIds = (await inquirer.prompt(questionsPushFunctions.slice(0, 2))).functions;
262274

263-
if (functionId) {
264-
functionIds.push(functionId);
265-
} else if (cliConfig.all) {
266-
const functions = localConfig.getFunctions();
267-
if (functions.length === 0) {
268-
throw new Error("No functions found in the current directory.");
269-
}
270-
functionIds.push(...functions.map((func, idx) => {
271-
return func.$id;
272-
}));
273-
}
275+
let functions = functionIds.map((functionId) => {
276+
const id = functionId.value ? functionId.value : functionId;
274277

275-
if (functionIds.length <= 0) {
276-
const answers = await inquirer.prompt(questionsPushFunctions[0]);
277-
functionIds.push(...answers.functions);
278-
}
279-
280-
let functions = functionIds.map((id) => {
281278
const functions = localConfig.getFunctions();
282279
const func = functions.find((f) => f.$id === id);
283280

@@ -651,22 +648,19 @@ const pushCollection = async () => {
651648

652649
const collections = [];
653650

654-
if (cliConfig.all) {
655-
if (localConfig.getCollections().length === 0) {
656-
throw new Error("No collections found in the current directory. Run `{{ language.params.executableName }} pull collection` to fetch all your collections.");
657-
}
658-
collections.push(...localConfig.getCollections());
659-
} else {
660-
const answers = await inquirer.prompt(questionsPushCollections[0])
661-
const configCollections = new Map();
662-
localConfig.getCollections().forEach((c) => {
663-
configCollections.set(`${c['databaseId']}|${c['$id']}`, c);
664-
});
665-
answers.collections.forEach((a) => {
666-
const collection = configCollections.get(a);
667-
collections.push(collection);
668-
})
669-
}
651+
const answers = await inquirer.prompt(questionsPushCollections.slice(0, 2))
652+
653+
const configCollections = new Map();
654+
655+
localConfig.getCollections().forEach((c) => {
656+
configCollections.set(`${c['databaseId']}|${c['$id']}`, c);
657+
});
658+
659+
answers.collections.forEach((c) => {
660+
const id = c.value ? c.value : c;
661+
const collection = configCollections.get(id);
662+
collections.push(collection);
663+
})
670664

671665
for (let collection of collections) {
672666
log(`Pushing collection ${collection.name} ( ${collection['databaseId']} - ${collection['$id']} )`)
@@ -714,7 +708,7 @@ const pushCollection = async () => {
714708
log(`Collection ${collection.name} ( ${collection['$id']} ) already exists.`);
715709

716710
if (!cliConfig.force) {
717-
const answers = await inquirer.prompt(questionsPushCollections[1])
711+
const answers = await inquirer.prompt(questionsPushCollections[2])
718712
if (answers.override.toLowerCase() !== "yes") {
719713
log(`Received "${answers.override}". Skipping ${collection.name} ( ${collection['$id']} )`);
720714
continue;
@@ -870,25 +864,13 @@ const pushCollection = async () => {
870864
const pushBucket = async () => {
871865
let response = {};
872866

873-
let bucketIds = [];
867+
const bucketIds = (await inquirer.prompt(questionsPushBuckets.slice(0, 2))).buckets;
868+
const buckets = [];
874869
const configBuckets = localConfig.getBuckets();
875870

876-
if (cliConfig.all) {
877-
if (configBuckets.length === 0) {
878-
throw new Error("No buckets found in the current directory. Run `appwrite pull bucket` to fetch all your buckets.");
879-
}
880-
bucketIds.push(...configBuckets.map((b) => b.$id));
881-
}
882-
883-
if (bucketIds.length === 0) {
884-
const answers = await inquirer.prompt(questionsPushBuckets[0])
885-
bucketIds.push(...answers.buckets);
886-
}
887-
888-
let buckets = [];
889-
890871
for (const bucketId of bucketIds) {
891-
const idBuckets = configBuckets.filter((b) => b.$id === bucketId);
872+
const id = bucketId.value ? bucketId.value : bucketId;
873+
const idBuckets = configBuckets.filter((b) => b.$id === id);
892874
buckets.push(...idBuckets);
893875
}
894876

@@ -903,7 +885,7 @@ const pushBucket = async () => {
903885
log(`Bucket ${bucket.name} ( ${bucket['$id']} ) already exists.`);
904886

905887
if (!cliConfig.force) {
906-
const answers = await inquirer.prompt(questionsPushBuckets[1])
888+
const answers = await inquirer.prompt(questionsPushBuckets[2])
907889
if (answers.override.toLowerCase() !== "yes") {
908890
log(`Received "${answers.override}". Skipping ${bucket.name} ( ${bucket['$id']} )`);
909891
continue;
@@ -957,25 +939,13 @@ const pushBucket = async () => {
957939
const pushTeam = async () => {
958940
let response = {};
959941

960-
let teamIds = [];
942+
const teamIds = (await inquirer.prompt(questionsPushTeams.slice(0, 2))).teams;
961943
const configTeams = localConfig.getTeams();
962-
963-
if (cliConfig.all) {
964-
if (configTeams.length === 0) {
965-
throw new Error("No teams found in the current directory. Run `appwrite pull team` to fetch all your teams.");
966-
}
967-
teamIds.push(...configTeams.map((t) => t.$id));
968-
}
969-
970-
if (teamIds.length === 0) {
971-
const answers = await inquirer.prompt(questionsPushTeams[0])
972-
teamIds.push(...answers.teams);
973-
}
974-
975-
let teams = [];
944+
const teams = [];
976945

977946
for (const teamId of teamIds) {
978-
const idTeams = configTeams.filter((t) => t.$id === teamId);
947+
const id = teamId.value ? teamId.value : teamId
948+
const idTeams = configTeams.filter((t) => t.$id === id);
979949
teams.push(...idTeams);
980950
}
981951

@@ -990,7 +960,7 @@ const pushTeam = async () => {
990960
log(`Team ${team.name} ( ${team['$id']} ) already exists.`);
991961

992962
if (!cliConfig.force) {
993-
const answers = await inquirer.prompt(questionsPushTeams[1])
963+
const answers = await inquirer.prompt(questionsPushTeams[2])
994964
if (answers.override.toLowerCase() !== "yes") {
995965
log(`Received "${answers.override}". Skipping ${team.name} ( ${team['$id']} )`);
996966
continue;
@@ -1027,31 +997,19 @@ const pushTeam = async () => {
1027997
const pushMessagingTopic = async () => {
1028998
let response = {};
1029999

1030-
let topicsIds = [];
10311000
const configTopics = localConfig.getMessagingTopics();
1001+
const topicsIds = (await inquirer.prompt(questionsPushMessagingTopics.slice(0, 2))).topics;
10321002
let overrideExisting = cliConfig.force;
1033-
1034-
if (cliConfig.all) {
1035-
if (configTopics.length === 0) {
1036-
throw new Error("No topics found in the current directory. Run `appwrite pull topics` to pull all your messaging topics.");
1037-
}
1038-
topicsIds.push(...configTopics.map((b) => b.$id));
1039-
}
1040-
1041-
if (topicsIds.length === 0) {
1042-
const answers = await inquirer.prompt(questionsPushMessagingTopics[0])
1043-
topicsIds.push(...answers.topics);
1044-
}
1045-
1046-
let topics = [];
1003+
const topics = [];
10471004

10481005
for (const topicId of topicsIds) {
1049-
const idTopic = configTopics.filter((b) => b.$id === topicId);
1006+
const id = topicId.value ? topicId.value : topicId
1007+
const idTopic = configTopics.filter((b) => b.$id === id);
10501008
topics.push(...idTopic);
10511009
}
10521010

10531011
if (!cliConfig.force) {
1054-
const answers = await inquirer.prompt(questionsPushMessagingTopics[1])
1012+
const answers = await inquirer.prompt(questionsPushMessagingTopics[2])
10551013
if (answers.override.toLowerCase() === "yes") {
10561014
overrideExisting = true;
10571015
}

0 commit comments

Comments
 (0)