Skip to content

Commit 2029e8a

Browse files
committed
feat(cli): Split project creation and pulling
1 parent 31a7560 commit 2029e8a

File tree

6 files changed

+65
-59
lines changed

6 files changed

+65
-59
lines changed

src/SDK/Language/CLI.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public function getFiles(): array
167167
'destination' => 'lib/utils.js',
168168
'template' => 'cli/lib/utils.js.twig',
169169
],
170+
[
171+
'scope' => 'default',
172+
'destination' => 'lib/commands/create.js',
173+
'template' => 'cli/lib/commands/create.js.twig',
174+
],
170175
[
171176
'scope' => 'default',
172177
'destination' => 'lib/commands/pull.js',

templates/cli/index.js.twig

Lines changed: 2 additions & 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 } = require("./lib/commands/generic");
15+
const { create } = require("./lib/commands/create");
1516
const { pull } = require("./lib/commands/pull");
1617
const { push } = require("./lib/commands/push");
1718
{% endif %}
@@ -37,6 +38,7 @@ program
3738
.showSuggestionAfterError()
3839
{% if sdk.test != "true" %}
3940
.addCommand(login)
41+
.addCommand(create)
4042
.addCommand(pull)
4143
.addCommand(push)
4244
.addCommand(logout)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { Command } = require("commander");
2+
const inquirer = require("inquirer");
3+
const { projectsCreate } = require("./projects");
4+
const { sdkForConsole } = require("../sdks");
5+
const { localConfig } = require("../config");
6+
const { questionsCreateProject } = require("../questions");
7+
const { success, actionRunner, commandDescriptions } = require("../parser");
8+
9+
const create = new Command("create")
10+
.description(commandDescriptions['create'])
11+
.configureHelp({
12+
helpWidth: process.stdout.columns || 80
13+
})
14+
.action(actionRunner(async (_options, command) => {
15+
command.help();
16+
}));
17+
18+
const createProject = async () => {
19+
let response = {}
20+
const answers = await inquirer.prompt(questionsCreateProject)
21+
if (!answers.project || !answers.organization) process.exit(1)
22+
23+
response = await projectsCreate({
24+
projectId: answers.id,
25+
name: answers.project,
26+
teamId: answers.organization.id,
27+
parseOutput: false
28+
})
29+
30+
localConfig.setProject(response['$id'], response.name);
31+
success();
32+
}
33+
34+
create
35+
.command("project")
36+
.description("Create a new {{ spec.title|caseUcfirst }} project")
37+
.action(actionRunner(createProject));
38+
39+
module.exports = {
40+
create,
41+
};

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { databasesGet, databasesListCollections, databasesList } = require("./dat
1010
const { storageListBuckets } = require("./storage");
1111
const { sdkForConsole } = require("../sdks");
1212
const { localConfig } = require("../config");
13-
const ID = require("../id");
13+
const ID = require("../id");
1414
const { paginate } = require("../paginate");
1515
const { questionsPullProject, questionsPullFunction, questionsPullCollection } = require("../questions");
1616
const { success, log, actionRunner, commandDescriptions } = require("../parser");
@@ -25,31 +25,10 @@ const pull = new Command("pull")
2525
}));
2626

2727
const pullProject = async () => {
28-
let response = {}
2928
const answers = await inquirer.prompt(questionsPullProject)
3029
if (!answers.project) process.exit(1)
3130

32-
let sdk = await sdkForConsole();
33-
if (answers.start === "new") {
34-
response = await teamsCreate({
35-
teamId: 'unique()',
36-
name: answers.project,
37-
sdk,
38-
parseOutput: false
39-
})
40-
41-
let teamId = response['$id'];
42-
response = await projectsCreate({
43-
projectId: answers.id,
44-
name: answers.project,
45-
teamId,
46-
parseOutput: false
47-
})
48-
49-
localConfig.setProject(response['$id'], response.name);
50-
} else {
51-
localConfig.setProject(answers.project.id, answers.project.name);
52-
}
31+
localConfig.setProject(answers.project.id, answers.project.name);
5332
success();
5433
}
5534

templates/cli/lib/parser.js.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const commandDescriptions = {
156156
"graphql": `The graphql command allows you to query and mutate any resource type on your Appwrite server.`,
157157
"avatars": `The avatars command aims to help you complete everyday tasks related to your app image, icons, and avatars.`,
158158
"databases": `The databases command allows you to create structured collections of documents, query and filter lists of documents.`,
159+
"create": `The create command provides a convenient wrapper for creating projects functions, collections, buckets, teams and messaging.`,
159160
"push": `The push command provides a convenient wrapper for pushing your functions, collections, buckets, teams and messaging.`,
160161
"functions": `The functions command allows you view, create and manage your Cloud Functions.`,
161162
"health": `The health command allows you to both validate and monitor your {{ spec.title|caseUcfirst }} server's health.`,

templates/cli/lib/questions.js.twig

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const getInstallCommand = (runtime) => {
110110
return undefined;
111111
};
112112

113-
const questionsPullProject = [
113+
const questionsProject = [
114114
{
115115
type: "confirm",
116116
name: "override",
@@ -126,7 +126,7 @@ const questionsPullProject = [
126126
message: "Choose the project organization",
127127
choices: async () => {
128128
let client = await sdkForConsole(true);
129-
const { teams } = await paginate(teamsList, { parseOutput: false , sdk: client}, 100, 'teams');
129+
const { teams } = await paginate(teamsList, { parseOutput: false, sdk: client }, 100, 'teams');
130130

131131
let choices = teams.map((team, idx) => {
132132
return {
@@ -145,56 +145,33 @@ const questionsPullProject = [
145145
return choices;
146146
}
147147
},
148-
{
149-
type: "list",
150-
name: "start",
151-
when(answers) {
152-
if (answers.override == undefined) {
153-
return true
154-
}
155-
return answers.override;
156-
},
157-
message: "How would you like to start?",
158-
choices: [
159-
{
160-
name: "Create a new {{ spec.title|caseUcfirst }} project",
161-
value: "new",
162-
},
163-
{
164-
name: "Link this directory to an existing {{ spec.title|caseUcfirst }} project",
165-
value: "existing",
166-
},
167-
],
168-
},
148+
];
149+
150+
const questionsCreateProject = [
151+
...questionsProject,
169152
{
170153
type: "input",
171154
name: "project",
172155
message: "What would you like to name your project?",
173-
default: "My Awesome Project",
174-
when(answers) {
175-
return answers.start == "new";
176-
},
156+
default: "My Awesome Project"
177157
},
178158
{
179159
type: "input",
180160
name: "id",
181161
message: "What ID would you like to have for your project?",
182-
default: "unique()",
183-
when(answers) {
184-
return answers.start == "new";
185-
},
186-
},
162+
default: "unique()"
163+
}
164+
];
165+
const questionsPullProject = [
166+
...questionsProject,
187167
{
188168
type: "list",
189169
name: "project",
190170
message: "Choose your {{ spec.title|caseUcfirst }} project.",
191-
when(answers) {
192-
return answers.start == "existing";
193-
},
194171
choices: async (answers) => {
195172
let response = await projectsList({
196173
parseOutput: false,
197-
queries: [JSON.stringify({ method: 'equal', attribute:'teamId', values: [answers.organization.id] })],
174+
queries: [JSON.stringify({ method: 'equal', attribute: 'teamId', values: [answers.organization.id] })],
198175
})
199176
let projects = response["projects"]
200177
let choices = projects.map((project, idx) => {
@@ -476,6 +453,7 @@ const questionsMfaChallenge = [
476453
];
477454

478455
module.exports = {
456+
questionsCreateProject,
479457
questionsPullProject,
480458
questionsLogin,
481459
questionsPullFunction,

0 commit comments

Comments
 (0)