Skip to content

Commit ce7c501

Browse files
committed
CLI function variables feature, CLI --yes param
1 parent eba0b96 commit ce7c501

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const inquirer = require("inquirer");
22
const JSONbig = require("json-bigint")({ storeAsString: false });
33
const { Command } = require("commander");
44
const { localConfig } = require("../config");
5-
const { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections } = require("../questions");
5+
const { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
66
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
7-
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment } = require('./functions');
7+
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
88
const {
99
databasesGet,
1010
databasesCreate,
@@ -142,7 +142,7 @@ const deploy = new Command("deploy")
142142
command.help()
143143
}));
144144

145-
const deployFunction = async ({ functionId, all } = {}) => {
145+
const deployFunction = async ({ functionId, all, yes } = {}) => {
146146
let response = {};
147147

148148
const functionIds = [];
@@ -160,7 +160,7 @@ const deployFunction = async ({ functionId, all } = {}) => {
160160
}
161161

162162
if(functionIds.length <= 0) {
163-
const answers = await inquirer.prompt(questionsDeployFunctions);
163+
const answers = await inquirer.prompt(questionsDeployFunctions[0]);
164164
functionIds.push(...answers.functions);
165165
}
166166

@@ -188,6 +188,47 @@ const deployFunction = async ({ functionId, all } = {}) => {
188188
throw new Error(`Runtime missmatch! (local=${func.runtime},remote=${response.runtime}) Please delete remote function or update your appwrite.json`);
189189
}
190190

191+
if(func.variables) {
192+
// Delete existing variables
193+
194+
// TODO: Pagination?
195+
const { variables: remoteVariables } = await functionsListVariables({
196+
functionId: func['$id'],
197+
limit: 100,
198+
parseOutput: false
199+
});
200+
201+
if(remoteVariables.length > 0) {
202+
if(!yes) {
203+
const variableAnswers = await inquirer.prompt(questionsDeployFunctions[1])
204+
205+
if (variableAnswers.override !== "YES") {
206+
log(`Received "${variableAnswers.override}". Skipping ${func.name} ( ${func['$id']} )`);
207+
continue;
208+
}
209+
}
210+
211+
await Promise.all(remoteVariables.map(async remoteVariable => {
212+
await functionsDeleteVariable({
213+
functionId: func['$id'],
214+
variableId: remoteVariable['$id'],
215+
parseOutput: false
216+
});
217+
}));
218+
}
219+
220+
// Deploy local variables
221+
222+
await Promise.all(Object.keys(func.variables).map(async localVariableKey => {
223+
await functionsCreateVariable({
224+
functionId: func['$id'],
225+
key: localVariableKey,
226+
value: func.variables[localVariableKey],
227+
parseOutput: false
228+
});
229+
}));
230+
}
231+
191232
response = await functionsUpdate({
192233
functionId: func['$id'],
193234
name: func.name,
@@ -359,7 +400,7 @@ const createAttribute = async (databaseId, collectionId, attribute) => {
359400
}
360401
}
361402

362-
const deployCollection = async ({ all } = {}) => {
403+
const deployCollection = async ({ all, yes } = {}) => {
363404
let response = {};
364405

365406
let collectionIds = [];
@@ -413,10 +454,12 @@ const deployCollection = async ({ all } = {}) => {
413454
})
414455
log(`Collection ${collection.name} ( ${collection['$id']} ) already exists.`);
415456

416-
answers = await inquirer.prompt(questionsDeployCollections[1])
417-
if (answers.override !== "YES") {
418-
log(`Received "${answers.override}". Skipping ${collection.name} ( ${collection['$id']} )`);
419-
continue;
457+
if(!yes) {
458+
answers = await inquirer.prompt(questionsDeployCollections[1])
459+
if (answers.override !== "YES") {
460+
log(`Received "${answers.override}". Skipping ${collection.name} ( ${collection['$id']} )`);
461+
continue;
462+
}
420463
}
421464

422465
log(`Updating attributes ... `);
@@ -556,12 +599,14 @@ deploy
556599
.description("Deploy functions in the current directory.")
557600
.option(`--functionId <functionId>`, `Function ID`)
558601
.option(`--all`, `Flag to deploy all functions`)
602+
.option(`--yes`, `Flag to confirm all warnings`)
559603
.action(actionRunner(deployFunction));
560604

561605
deploy
562606
.command("collection")
563607
.description("Deploy collections in the current project.")
564608
.option(`--all`, `Flag to deploy all functions`)
609+
.option(`--yes`, `Flag to confirm all warnings`)
565610
.action(actionRunner(deployCollection));
566611

567612
module.exports = {

templates/cli/lib/questions.js.twig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const questionsInitProject = [
113113
type: "input",
114114
name: "id",
115115
message: "What ID would you like to have for your project?",
116-
default: "myAwesomeProject",
116+
default: "unique()",
117117
when(answers) {
118118
return answers.start == "new";
119119
},
@@ -251,7 +251,12 @@ const questionsDeployFunctions = [
251251
})
252252
return choices;
253253
}
254-
}
254+
},
255+
{
256+
type: "input",
257+
name: "override",
258+
message: 'Are you sure you want to override this function variables? This can lead to loss of secrets! Type "YES" to confirm.'
259+
},
255260
]
256261

257262
const questionsDeployCollections = [

0 commit comments

Comments
 (0)