Skip to content

Commit bce40e8

Browse files
committed
feat(cli): Pulling function without questions, and review refactoring
1 parent 793689e commit bce40e8

File tree

4 files changed

+33
-44
lines changed

4 files changed

+33
-44
lines changed

templates/cli/index.js.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { commandDescriptions, cliConfig } = require("./lib/parser");
1212
const { client } = require("./lib/commands/generic");
1313
{% if sdk.test != "true" %}
1414
const { login, logout, whoami } = require("./lib/commands/generic");
15+
const { init } = require("./lib/commands/init");
1516
const { pull } = require("./lib/commands/pull");
1617
const { push } = require("./lib/commands/push");
1718
{% endif %}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
const { success, log, error, actionRunner, commandDescriptions } = require("../parser");
2121
const { accountGet } = require("./account");
2222
const { loginCommand } = require("./generic");
23+
const { sdkForConsole } = require("../sdks");
2324

2425
const initProject = async () => {
2526
let response = {};
@@ -28,16 +29,19 @@ const initProject = async () => {
2829
if (globalConfig.getEndpoint() === '' || globalConfig.getCookie() === '') {
2930
throw '';
3031
}
32+
const client = await sdkForConsole();
33+
3134
await accountGet({
32-
parseOutput: false
35+
parseOutput: false,
36+
sdk: client
3337
});
3438
} catch (e) {
3539
log('You must login first')
36-
await loginCommand()
40+
await loginCommand();
3741
}
3842

3943
const answers = await inquirer.prompt(questionsInitProject)
40-
if (!answers.project || !answers.organization) {
44+
if (answers.override === false) {
4145
process.exit(1)
4246
}
4347

@@ -60,7 +64,6 @@ const initProject = async () => {
6064
const initBucket = async () => {
6165
let response = {}
6266
const answers = await inquirer.prompt(questionsCreateBucket)
63-
if (!answers.bucket || !answers.id || !answers.fileSecurity) process.exit(1)
6467

6568
try {
6669
response = await storageCreateBucket({
@@ -81,7 +84,6 @@ const initBucket = async () => {
8184
const initCollection = async () => {
8285
let response = {}
8386
const answers = await inquirer.prompt(questionsCreateCollection)
84-
if (!answers.database || !answers.collection || !answers.id || !answers.documentSecurity) process.exit(1)
8587

8688
try {
8789
response = await databasesCreateCollection({
@@ -103,7 +105,6 @@ const initCollection = async () => {
103105
const initTopic = async () => {
104106
let response = {}
105107
const answers = await inquirer.prompt(questionsCreateMessagingTopic)
106-
if (!answers.topic || !answers.id) process.exit(1)
107108

108109
try {
109110
response = await messagingCreateTopic({
@@ -115,7 +116,7 @@ const initTopic = async () => {
115116
localConfig.addMessagingTopic(response);
116117
success();
117118
} catch (e) {
118-
error(e.getMessage ?? 'Unknown error occurred. Please try again');
119+
error(e.message ?? 'Unknown error occurred. Please try again');
119120
}
120121
};
121122

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

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { Command } = require("commander");
44
const inquirer = require("inquirer");
55
const { messagingListTopics } = require("./messaging");
66
const { teamsList } = require("./teams");
7-
const { projectsList } = require("./projects");
7+
const { projectsGet } = require("./projects");
88
const { functionsList, functionsDownloadDeployment } = require("./functions");
99
const { databasesGet, databasesListCollections, databasesList } = require("./databases");
1010
const { storageListBuckets } = require("./storage");
@@ -24,43 +24,31 @@ const pull = new Command("pull")
2424

2525
const pullProject = async () => {
2626
try {
27-
let response = await projectsList({
27+
let response = await projectsGet({
2828
parseOutput: false,
29-
queries: [JSON.stringify({ method: 'equal', attribute: '$id', values: [localConfig.getProject().projectId] })]
29+
projectId: localConfig.getProject().projectId
3030

3131
})
32-
if(response.total === 1){
33-
localConfig.setProject(response.projects[0].$id, response.projects[0].name);
34-
}
32+
33+
localConfig.setProject(response.$id, response.name);
3534
success();
3635
} catch (e) {
3736
throw e;
3837
}
3938
}
4039

41-
const pullFunctions = async ({ all, yes } = {}) => {
42-
let functions = [];
43-
let questions = questionsPullFunctions;
44-
40+
const pullFunctions = async ({ all } = {}) => {
4541
const localFunctions = localConfig.getFunctions();
4642

47-
if (all) {
48-
questions = yes ? [] : questionsPullFunctions[1];
49-
functions = (await paginate(functionsList, { parseOutput: false }, 100, 'functions')).functions;
50-
}
51-
52-
const answers = await inquirer.prompt(questions);
43+
const functions = all
44+
? (await paginate(functionsList, { parseOutput: false }, 100, 'functions')).functions
45+
: (await inquirer.prompt(questionsPullFunctions)).functions;
5346

54-
const overridingLocalChanges = yes ?? answers.override.toLowerCase() === "yes";
55-
const selectedFunctions = functions.length === 0 ? answers.functions : functions;
47+
log(`Pulling ${functions.length} functions`);
5648

57-
for (let func of selectedFunctions) {
49+
for (let func of functions) {
5850
const functionExistLocally = localFunctions.find((localFunc) => localFunc['$id'] === func['$id']) !== undefined;
5951

60-
if (!overridingLocalChanges && functionExistLocally) {
61-
log(`Skipping locally found implementation of ${func['name']}`)
62-
continue;
63-
}
6452
if (functionExistLocally) {
6553
localConfig.updateFunction(func['$id'], func);
6654
} else {
@@ -74,7 +62,7 @@ const pullFunctions = async ({ all, yes } = {}) => {
7462
continue
7563
}
7664

77-
const compressedFileName = `${+new Date()}.tar.gz`
65+
const compressedFileName = `${func['$id']}-${+new Date()}.tar.gz`
7866

7967
await functionsDownloadDeployment({
8068
functionId: func['$id'],
@@ -92,7 +80,7 @@ const pullFunctions = async ({ all, yes } = {}) => {
9280
});
9381

9482
fs.rmSync(compressedFileName);
95-
success(`Pulled ${func['name']} code and definition`)
83+
success(`Pulled ${func['name']} code and settings`)
9684
}
9785
}
9886

@@ -111,7 +99,6 @@ const pullCollection = async ({ all, databaseId } = {}) => {
11199

112100
if (databaseIds.length <= 0) {
113101
let answers = await inquirer.prompt(questionsPullCollection)
114-
if (!answers.databases) process.exit(1)
115102
databaseIds.push(...answers.databases);
116103
}
117104

@@ -185,31 +172,30 @@ const pullMessagingTopic = async () => {
185172

186173
pull
187174
.command("project")
188-
.description("Pulling your Appwrite project name")
175+
.description("Pulling your {{ spec.title|caseUcfirst }} project name")
189176
.action(actionRunner(pullProject));
190177

191178
pull
192179
.command("function")
193-
.description(`Pulling your Appwrite functions`)
194-
.option(`--yes`, `Flag to confirm all warnings`)
180+
.description(`Pulling your {{ spec.title|caseUcfirst }} functions`)
195181
.option(`--all`, `Flag to pull all functions`)
196182
.action(actionRunner(pullFunctions));
197183

198184
pull
199185
.command("collection")
200-
.description("Pulling your Appwrite collections")
186+
.description("Pulling your {{ spec.title|caseUcfirst }} collections")
201187
.option(`--databaseId <databaseId>`, `Database ID`)
202188
.option(`--all`, `Flag to pull all databases`)
203189
.action(actionRunner(pullCollection))
204190

205191
pull
206192
.command("bucket")
207-
.description("Pulling your Appwrite buckets")
193+
.description("Pulling your {{ spec.title|caseUcfirst }} buckets")
208194
.action(actionRunner(pullBucket))
209195

210196
pull
211197
.command("team")
212-
.description("Pulling your Appwrite teams")
198+
.description("Pulling your {{ spec.title|caseUcfirst }} teams")
213199
.action(actionRunner(pullTeam))
214200

215201
pull

templates/cli/lib/questions.js.twig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,21 @@ const questionsPullFunctions = [
218218
type: "checkbox",
219219
name: "functions",
220220
message: "Which functions would you like to pull?",
221+
validate: (value) => validateRequired('function', value),
221222
choices: async () => {
222223
const { functions } = await paginate(functionsList, { parseOutput: false }, 100, 'functions');
223224

225+
if(functions.length === 0){
226+
throw "We couldn't find any functions in your {{ spec.title|caseUcfirst }} project";
227+
}
228+
224229
return functions.map(func => {
225230
return {
226231
name: `${func.name} (${func.$id})`,
227232
value: func
228233
}
229234
});
230235
}
231-
},
232-
{
233-
type: "input",
234-
name: "override",
235-
message: `Are you sure you want to override local functions code and definition? ${chalk.red('All local changes will be lost!')} Type "YES" to confirm.`
236236
}
237237
];
238238

@@ -357,6 +357,7 @@ const questionsPullCollection = [
357357
type: "checkbox",
358358
name: "databases",
359359
message: "From which database would you like to pull collections?",
360+
validate: (value) => validateRequired('collection', value),
360361
choices: async () => {
361362
let response = await databasesList({
362363
parseOutput: false

0 commit comments

Comments
 (0)