@@ -6,7 +6,7 @@ const inquirer = require("inquirer");
6
6
const { messagingListTopics } = require("./messaging");
7
7
const { teamsList } = require("./teams");
8
8
const { projectsGet } = require("./projects");
9
- const { functionsList, functionsDownloadDeployment } = require("./functions");
9
+ const { functionsList, functionsGetDeploymentDownload, functionsListDeployments } = require("./functions");
10
10
const { databasesGet, databasesListCollections, databasesList } = require("./databases");
11
11
const { storageListBuckets } = require("./storage");
12
12
const { localConfig } = require("../config");
@@ -57,6 +57,8 @@ const pullSettings = async () => {
57
57
}
58
58
59
59
const pullFunctions = async ({ code, withVariables }) => {
60
+ process.chdir(localConfig.configDirectoryPath)
61
+
60
62
log("Fetching functions ...");
61
63
let total = 0;
62
64
@@ -84,7 +86,7 @@ const pullFunctions = async ({ code, withVariables }) => {
84
86
85
87
func['path'] = localFunction['path'];
86
88
if (!localFunction['path']) {
87
- func['path'] = `functions/${func.$id }`;
89
+ func['path'] = `functions/${func.name }`;
88
90
}
89
91
if (!withVariables) {
90
92
delete func['vars'];
@@ -97,45 +99,70 @@ const pullFunctions = async ({ code, withVariables }) => {
97
99
98
100
if (code === false) {
99
101
warn("Source code download skipped.");
100
- } else if (!func['deployment']) {
101
- warn("Source code download skipped because function doesn't have active deployment.");
102
- } else {
103
- if (allowCodePull === null) {
104
- const codeAnswer = await inquirer.prompt(questionsPullFunctionsCode);
105
- allowCodePull = codeAnswer.override;
102
+ continue;
103
+ }
104
+
105
+ if (allowCodePull === null) {
106
+ const codeAnswer = await inquirer.prompt(questionsPullFunctionsCode);
107
+ allowCodePull = codeAnswer.override;
108
+ }
109
+
110
+ if (!allowCodePull) {
111
+ continue;
112
+ }
113
+
114
+ let deploymentId = null;
115
+
116
+ try {
117
+ const fetchResponse = await functionsListDeployments({
118
+ functionId: func['$id'],
119
+ queries: [
120
+ JSON.stringify({ method: 'limit', values: [1] }),
121
+ JSON.stringify({ method: 'orderDesc', values: ['$id'] })
122
+ ],
123
+ parseOutput: false
124
+ });
125
+
126
+ if (fetchResponse['total'] > 0) {
127
+ deploymentId = fetchResponse['deployments'][0]['$id'];
106
128
}
107
129
108
- if (allowCodePull) {
109
- log("Pulling active deployment's code ...");
110
-
111
- const compressedFileName = `${func['$id']}-${+new Date()}.tar.gz`
112
- await functionsDownloadDeployment({
113
- functionId: func['$id'],
114
- deploymentId: func['deployment'],
115
- destination: compressedFileName,
116
- overrideForCli: true,
117
- parseOutput: false
118
- });
119
-
120
- tar.extract({
121
- sync: true,
122
- cwd: func['path'],
123
- file: compressedFileName,
124
- strict: false,
125
- });
126
-
127
- fs.rmSync(compressedFileName);
128
-
129
- if (withVariables) {
130
- const envFileLocation = `${func['path']}/.env`
131
- try {
132
- fs.rmSync(envFileLocation);
133
- } catch {
134
- }
135
-
136
- fs.writeFileSync(envFileLocation, func['vars'].map(r => `${r.key}=${r.value}\n`).join(''))
137
- }
130
+ } catch {
131
+ }
132
+
133
+ if (deploymentId === null) {
134
+ log("Source code download skipped because function doesn't have any available deployment");
135
+ continue;
136
+ }
137
+
138
+ log("Pulling latest deployment code ...");
139
+
140
+ const compressedFileName = `${func['$id']}-${+new Date()}.tar.gz`
141
+ await functionsGetDeploymentDownload({
142
+ functionId: func['$id'],
143
+ deploymentId,
144
+ destination: compressedFileName,
145
+ overrideForCli: true,
146
+ parseOutput: false
147
+ });
148
+
149
+ tar.extract({
150
+ sync: true,
151
+ cwd: func['path'],
152
+ file: compressedFileName,
153
+ strict: false,
154
+ });
155
+
156
+ fs.rmSync(compressedFileName);
157
+
158
+ if (withVariables) {
159
+ const envFileLocation = `${func['path']}/.env`
160
+ try {
161
+ fs.rmSync(envFileLocation);
162
+ } catch {
138
163
}
164
+
165
+ fs.writeFileSync(envFileLocation, func['vars'].map(r => `${r.key}=${r.value}\n`).join(''))
139
166
}
140
167
}
141
168
@@ -289,33 +316,33 @@ pull
289
316
pull
290
317
.command("function")
291
318
.alias("functions")
292
- .description("Pulling your {{ spec .title | caseUcfirst }} cloud function")
319
+ .description("Pull your {{ spec .title | caseUcfirst }} cloud function")
293
320
.option("--no-code", "Don't pull the function's code")
294
321
.option("--with-variables", `Pull function variables. ${chalk.red('recommend for testing purposes only')}`)
295
322
.action(actionRunner(pullFunctions))
296
323
297
324
pull
298
325
.command("collection")
299
326
.alias("collections")
300
- .description("Pulling your {{ spec .title | caseUcfirst }} collections")
327
+ .description("Pull your {{ spec .title | caseUcfirst }} collections")
301
328
.action(actionRunner(pullCollection))
302
329
303
330
pull
304
331
.command("bucket")
305
332
.alias("buckets")
306
- .description("Pulling your Appwrite buckets")
333
+ .description("Pull your Appwrite buckets")
307
334
.action(actionRunner(pullBucket))
308
335
309
336
pull
310
337
.command("team")
311
338
.alias("teams")
312
- .description("Pulling your Appwrite teams")
339
+ .description("Pull your Appwrite teams")
313
340
.action(actionRunner(pullTeam))
314
341
315
342
pull
316
343
.command("topic")
317
344
.alias("topics")
318
- .description("Initialise your Appwrite messaging topics")
345
+ .description("Pull your Appwrite messaging topics")
319
346
.action(actionRunner(pullMessagingTopic))
320
347
321
348
module.exports = {
0 commit comments