Skip to content

Commit 8265f1a

Browse files
Merge pull request #930 from appwrite/feat-latest-deployment
feat(cli): Pull last deployment instead of local when available
2 parents 1aadc4d + 0d8fa5c commit 8265f1a

File tree

1 file changed

+62
-37
lines changed

1 file changed

+62
-37
lines changed

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

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const inquirer = require("inquirer");
66
const { messagingListTopics } = require("./messaging");
77
const { teamsList } = require("./teams");
88
const { projectsGet } = require("./projects");
9-
const { functionsList, functionsDownloadDeployment } = require("./functions");
9+
const { functionsList, functionsDownloadDeployment, functionsListDeployments } = require("./functions");
1010
const { databasesGet, databasesListCollections, databasesList } = require("./databases");
1111
const { storageListBuckets } = require("./storage");
1212
const { localConfig } = require("../config");
@@ -97,45 +97,70 @@ const pullFunctions = async ({ code, withVariables }) => {
9797

9898
if (code === false) {
9999
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;
100+
continue;
101+
}
102+
103+
if (allowCodePull === null) {
104+
const codeAnswer = await inquirer.prompt(questionsPullFunctionsCode);
105+
allowCodePull = codeAnswer.override;
106+
}
107+
108+
if (!allowCodePull) {
109+
continue;
110+
}
111+
112+
let deploymentId = null;
113+
114+
try {
115+
const fetchResponse = await functionsListDeployments({
116+
functionId: func['$id'],
117+
queries: [
118+
JSON.stringify({ method: 'limit', values: [1] }),
119+
JSON.stringify({ method: 'orderDesc', values: ['$id'] })
120+
],
121+
parseOutput: false
122+
});
123+
124+
if (fetchResponse['total'] > 0) {
125+
deploymentId = fetchResponse['deployments'][0]['$id'];
106126
}
107127

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-
}
128+
} catch {
129+
}
130+
131+
if (deploymentId === null) {
132+
log("Source code download skipped because function doesn't have any available deployment");
133+
continue;
134+
}
135+
136+
log("Pulling latest deployment code ...");
137+
138+
const compressedFileName = `${func['$id']}-${+new Date()}.tar.gz`
139+
await functionsDownloadDeployment({
140+
functionId: func['$id'],
141+
deploymentId,
142+
destination: compressedFileName,
143+
overrideForCli: true,
144+
parseOutput: false
145+
});
146+
147+
tar.extract({
148+
sync: true,
149+
cwd: func['path'],
150+
file: compressedFileName,
151+
strict: false,
152+
});
153+
154+
fs.rmSync(compressedFileName);
155+
156+
if (withVariables) {
157+
const envFileLocation = `${func['path']}/.env`
158+
try {
159+
fs.rmSync(envFileLocation);
160+
} catch {
138161
}
162+
163+
fs.writeFileSync(envFileLocation, func['vars'].map(r => `${r.key}=${r.value}\n`).join(''))
139164
}
140165
}
141166

0 commit comments

Comments
 (0)