Skip to content

Commit e10fa1f

Browse files
committed
CLI: Add support for teams
1 parent ed8d3da commit e10fa1f

File tree

4 files changed

+173
-2
lines changed

4 files changed

+173
-2
lines changed

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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, questionsConfirmDeployCollections } = require("../questions");
5+
const { questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
66
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
77
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
88
const {
@@ -25,6 +25,11 @@ const {
2525
databasesListIndexes,
2626
databasesDeleteIndex
2727
} = require("./databases");
28+
const {
29+
teamsGet,
30+
teamsUpdate,
31+
teamsCreate
32+
} = require("./teams");
2833

2934
const POOL_DEBOUNCE = 2000; // in milliseconds
3035
const POOL_MAX_DEBOUNCES = 30;
@@ -593,6 +598,76 @@ const deployCollection = async ({ all, yes } = {}) => {
593598
}
594599
}
595600

601+
const deployTeam = async ({ all, yes } = {}) => {
602+
let response = {};
603+
604+
let teamIds = [];
605+
const configTeams = localConfig.getTeams();
606+
607+
if(all) {
608+
if (configTeams.length === 0) {
609+
throw new Error("No teams found in the current directory. Run `appwrite init team` to fetch all your teams.");
610+
}
611+
teamIds.push(...configTeams.map((t) => t.$id));
612+
}
613+
614+
if(teamIds.length <= 0) {
615+
let answers = await inquirer.prompt(questionsDeployTeams[0])
616+
teamIds.push(...answers.teams);
617+
}
618+
619+
let teams = [];
620+
621+
for(const teamId of teamIds) {
622+
const idTeams = configTeams.filter((t) => t.$id === teamId);
623+
teams.push(...idTeams);
624+
}
625+
626+
for (let team of teams) {
627+
log(`Deploying team ${team.name} ( ${team['$id']} )`)
628+
629+
try {
630+
response = await teamsGet({
631+
teamId: team['$id'],
632+
parseOutput: false,
633+
})
634+
log(`Team ${team.name} ( ${team['$id']} ) already exists.`);
635+
636+
if(!yes) {
637+
answers = await inquirer.prompt(questionsDeployTeams[1])
638+
if (answers.override !== "YES") {
639+
log(`Received "${answers.override}". Skipping ${team.name} ( ${team['$id']} )`);
640+
continue;
641+
}
642+
}
643+
644+
log(`Updating team ...`)
645+
646+
await teamsUpdate({
647+
teamId: team['$id'],
648+
name: team.name,
649+
parseOutput: false
650+
});
651+
652+
success(`Deployed ${team.name} ( ${team['$id']} )`);
653+
} catch (e) {
654+
if (e.code == 404) {
655+
log(`Team ${team.name} does not exist in the project. Creating ... `);
656+
657+
response = await teamsCreate({
658+
teamId: team['$id'],
659+
name: team.name,
660+
parseOutput: false
661+
})
662+
663+
success(`Deployed ${team.name} ( ${team['$id']} )`);
664+
} else {
665+
throw e;
666+
}
667+
}
668+
}
669+
}
670+
596671
deploy
597672
.command("function")
598673
.description("Deploy functions in the current directory.")
@@ -608,6 +683,13 @@ deploy
608683
.option(`--yes`, `Flag to confirm all warnings`)
609684
.action(actionRunner(deployCollection));
610685

686+
deploy
687+
.command("team")
688+
.description("Deploy teams in the current project.")
689+
.option(`--all`, `Flag to deploy all teams`)
690+
.option(`--yes`, `Flag to confirm all warnings`)
691+
.action(actionRunner(deployTeam));
692+
611693
module.exports = {
612694
deploy
613695
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require("path");
33
const childProcess = require('child_process');
44
const { Command } = require("commander");
55
const inquirer = require("inquirer");
6-
const { teamsCreate } = require("./teams");
6+
const { teamsCreate, teamsList } = require("./teams");
77
const { projectsCreate } = require("./projects");
88
const { functionsCreate } = require("./functions");
99
const { databasesListCollections, databasesList } = require("./databases");
@@ -186,6 +186,24 @@ const initCollection = async ({ all, databaseId } = {}) => {
186186
success();
187187
}
188188

189+
const initTeam = async () => {
190+
// TODO: Pagination?
191+
let response = await teamsList({
192+
queries: [ 'limit(100)' ],
193+
parseOutput: false
194+
})
195+
196+
let teams = response.teams;
197+
log(`Found ${teams.length} teams`);
198+
199+
teams.forEach(async team => {
200+
log(`Fetching ${team.name} ...`);
201+
localConfig.addTeam(team);
202+
});
203+
204+
success();
205+
}
206+
189207
init
190208
.command("project")
191209
.description("Initialise your {{ spec.title|caseUcfirst }} project")
@@ -203,6 +221,11 @@ init
203221
.option(`--all`, `Flag to initialize all databases`)
204222
.action(actionRunner(initCollection))
205223

224+
init
225+
.command("team")
226+
.description("Initialise your Appwrite teams")
227+
.action(actionRunner(initTeam))
228+
206229
module.exports = {
207230
init,
208231
};

templates/cli/lib/config.js.twig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,45 @@ class Local extends Config {
166166
this.set("collections", collections);
167167
}
168168

169+
getTeams() {
170+
if (!this.has("teams")) {
171+
return [];
172+
}
173+
return this.get("teams");
174+
}
175+
176+
getTeam($id) {
177+
if (!this.has("teams")) {
178+
return {};
179+
}
180+
181+
let teams = this.get("teams");
182+
for (let i = 0; i < teams.length; i++) {
183+
if (teams[i]['$id'] == $id) {
184+
return teams[i];
185+
}
186+
}
187+
188+
return {};
189+
}
190+
191+
addTeam(props) {
192+
if (!this.has("teams")) {
193+
this.set("teams", []);
194+
}
195+
196+
let teams = this.get("teams");
197+
for (let i = 0; i < teams.length; i++) {
198+
if (teams[i]['$id'] == props['$id']) {
199+
teams[i] = props;
200+
this.set("teams", teams);
201+
return;
202+
}
203+
}
204+
teams.push(props);
205+
this.set("teams", teams);
206+
}
207+
169208
getProject() {
170209
if (!this.has("projectId") || !this.has("projectName")) {
171210
return {};

templates/cli/lib/questions.js.twig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,39 @@ const questionsGetEntrypoint = [
300300
},
301301
]
302302

303+
const questionsDeployTeams = [
304+
{
305+
type: "checkbox",
306+
name: "teams",
307+
message: "Which teams would you like to deploy?",
308+
choices: () => {
309+
let teams = localConfig.getTeams();
310+
if (teams.length === 0) {
311+
throw new Error("No teams found in the current directory. Run `appwrite init team` to fetch all your teams.");
312+
}
313+
let choices = teams.map((team, idx) => {
314+
return {
315+
name: `${team.name} (${team['$id']})`,
316+
value: team.$id
317+
}
318+
})
319+
return choices;
320+
}
321+
},
322+
{
323+
type: "input",
324+
name: "override",
325+
message: 'Are you sure you want to override this team? This can lead to loss of data! Type "YES" to confirm.'
326+
},
327+
]
328+
303329
module.exports = {
304330
questionsInitProject,
305331
questionsLogin,
306332
questionsInitFunction,
307333
questionsInitCollection,
308334
questionsDeployFunctions,
309335
questionsDeployCollections,
336+
questionsDeployTeams,
310337
questionsGetEntrypoint
311338
};

0 commit comments

Comments
 (0)