Skip to content

Commit b7f278b

Browse files
committed
Merge branch 'refs/heads/feat-split-project-actions' into feat-create-resources
# Conflicts: # templates/cli/lib/commands/init.js.twig # templates/cli/lib/questions.js.twig
2 parents 9b71779 + 2d8fe79 commit b7f278b

File tree

6 files changed

+116
-61
lines changed

6 files changed

+116
-61
lines changed

src/SDK/Language/CLI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public function getFiles(): array
174174
],
175175
[
176176
'scope' => 'default',
177-
'destination' => 'lib/commands/create.js',
178-
'template' => 'cli/lib/commands/create.js.twig',
177+
'destination' => 'lib/commands/init.js',
178+
'template' => 'cli/lib/commands/init.js.twig',
179179
],
180180
[
181181
'scope' => 'default',

templates/cli/index.js.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +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");
15+
const { init } = require("./lib/commands/init");
1616
const { pull } = require("./lib/commands/pull");
1717
const { push } = require("./lib/commands/push");
1818
{% endif %}
@@ -38,7 +38,7 @@ program
3838
.showSuggestionAfterError()
3939
{% if sdk.test != "true" %}
4040
.addCommand(login)
41-
.addCommand(create)
41+
.addCommand(init)
4242
.addCommand(pull)
4343
.addCommand(push)
4444
.addCommand(logout)

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

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,64 @@ const { actionRunner, success, parseBool, commandDescriptions, log, parse } = re
88
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
99
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
1010

11-
const login = new Command("login")
12-
.description(commandDescriptions['login'])
13-
.configureHelp({
14-
helpWidth: process.stdout.columns || 80
11+
const loginCommand = async () => {
12+
const answers = await inquirer.prompt(questionsLogin)
13+
14+
let client = await sdkForConsole(false);
15+
16+
await accountCreateEmailPasswordSession({
17+
email: answers.email,
18+
password: answers.password,
19+
parseOutput: false,
20+
sdk: client
1521
})
16-
.action(actionRunner(async () => {
17-
const answers = await inquirer.prompt(questionsLogin)
1822

19-
let client = await sdkForConsole(false);
23+
client.setCookie(globalConfig.getCookie());
2024

21-
await accountCreateEmailPasswordSession({
22-
email: answers.email,
23-
password: answers.password,
24-
parseOutput: false,
25-
sdk: client
26-
})
25+
let account;
26+
27+
try {
28+
account = await accountGet({
29+
sdk: client,
30+
parseOutput: false
31+
});
32+
} catch(error) {
33+
if (error.response === 'user_more_factors_required') {
34+
const { factor } = await inquirer.prompt(questionsListFactors);
2735

28-
client.setCookie(globalConfig.getCookie());
36+
const challenge = await accountCreateMfaChallenge({
37+
factor,
38+
parseOutput: false,
39+
sdk: client
40+
});
2941

30-
let account;
42+
const { otp } = await inquirer.prompt(questionsMfaChallenge);
43+
44+
await accountUpdateMfaChallenge({
45+
challengeId: challenge.$id,
46+
otp,
47+
parseOutput: false,
48+
sdk: client
49+
});
3150

32-
try {
3351
account = await accountGet({
3452
sdk: client,
3553
parseOutput: false
3654
});
37-
} catch(error) {
38-
if (error.response === 'user_more_factors_required') {
39-
const { factor } = await inquirer.prompt(questionsListFactors);
40-
41-
const challenge = await accountCreateMfaChallenge({
42-
factor,
43-
parseOutput: false,
44-
sdk: client
45-
});
46-
47-
const { otp } = await inquirer.prompt(questionsMfaChallenge);
48-
49-
await accountUpdateMfaChallenge({
50-
challengeId: challenge.$id,
51-
otp,
52-
parseOutput: false,
53-
sdk: client
54-
});
55-
56-
account = await accountGet({
57-
sdk: client,
58-
parseOutput: false
59-
});
60-
} else {
61-
throw error;
62-
}
55+
} else {
56+
throw error;
6357
}
58+
}
6459

65-
success("Signed in as user with ID: " + account.$id);
66-
}));
60+
success("Signed in as user with ID: " + account.$id);
61+
};
62+
63+
const login = new Command("login")
64+
.description(commandDescriptions['login'])
65+
.configureHelp({
66+
helpWidth: process.stdout.columns || 80
67+
})
68+
.action(actionRunner(loginCommand));
6769

6870
const logout = new Command("logout")
6971
.description(commandDescriptions['logout'])
@@ -159,6 +161,7 @@ const client = new Command("client")
159161

160162
module.exports = {
161163
{% if sdk.test != "true" %}
164+
loginCommand,
162165
login,
163166
logout,
164167
{% endif %}

templates/cli/lib/commands/create.js.twig renamed to templates/cli/lib/commands/init.js.twig

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { functionsCreate } = require("./functions");
1010
const { databasesCreateCollection } = require("./databases");
1111
const { sdkForConsole } = require("../sdks");
1212
const ID = require("../id");
13-
const { localConfig } = require("../config");
13+
const { localConfig, globalConfig } = require("../config");
1414
const {
1515
questionsCreateProject,
1616
questionsCreateFunction,
@@ -19,18 +19,33 @@ const {
1919
questionsCreateCollection
2020
} = require("../questions");
2121
const { success, error, actionRunner, commandDescriptions } = require("../parser");
22+
const { accountGet } = require("./account");
23+
const { loginCommand } = require("./generic");
2224

23-
const create = new Command("create")
24-
.description(commandDescriptions['create'])
25+
const init = new Command("init")
26+
.description(commandDescriptions['init'])
2527
.configureHelp({
2628
helpWidth: process.stdout.columns || 80
2729
})
2830
.action(actionRunner(async (_options, command) => {
2931
command.help();
3032
}));
3133

32-
const createProject = async () => {
33-
let response = {}
34+
const initProject = async () => {
35+
let response = {};
36+
37+
try {
38+
if (globalConfig.getEndpoint() === '' || globalConfig.getCookie() === '') {
39+
throw '';
40+
}
41+
await accountGet({
42+
parseOutput: false
43+
});
44+
} catch (e) {
45+
error('You must login first')
46+
await loginCommand()
47+
}
48+
3449
const answers = await inquirer.prompt(questionsCreateProject)
3550
if (!answers.project || !answers.organization) process.exit(1)
3651

@@ -219,10 +234,10 @@ const createFunction = async () => {
219234

220235

221236

222-
create
237+
init
223238
.command("project")
224-
.description("Create a new {{ spec.title|caseUcfirst }} project")
225-
.action(actionRunner(createProject));
239+
.description("Init and create a new {{ spec.title|caseUcfirst }} project")
240+
.action(actionRunner(initProject));
226241

227242
create
228243
.command("function")
@@ -245,5 +260,5 @@ create
245260
.action(actionRunner(createTopic));
246261

247262
module.exports = {
248-
create,
263+
init,
249264
};

templates/cli/lib/parser.js.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ 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.`,
159+
"init": `The init command provides a convenient wrapper for creating and initializing project in Appwrite.`,
160+
"create": `The create command provides a convenient wrapper for creating functions, collections, buckets, teams and messaging.`,
160161
"push": `The push command provides a convenient wrapper for pushing your functions, collections, buckets, teams and messaging.`,
161162
"functions": `The functions command allows you view, create and manage your Cloud Functions.`,
162163
"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: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const { paginate } = require('./paginate');
1010
const { databasesList } = require('./commands/databases');
1111
const JSONbig = require("json-bigint")({ storeAsString: false });
1212

13+
const whenOverride = (answers)=> answers.override === undefined ? true : answers.override;
14+
1315
const getIgnores = (runtime) => {
1416
const languge = runtime.split('-')[0];
1517

@@ -143,7 +145,8 @@ const questionsProject = [
143145
}
144146

145147
return choices;
146-
}
148+
},
149+
when: whenOverride
147150
},
148151
];
149152

@@ -153,13 +156,46 @@ const questionsCreateProject = [
153156
type: "input",
154157
name: "project",
155158
message: "What would you like to name your project?",
156-
default: "My Awesome Project"
159+
default: "My Awesome Project",
160+
when: whenOverride
157161
},
158162
{
159163
type: "input",
160164
name: "id",
161165
message: "What ID would you like to have for your project?",
162-
default: "unique()"
166+
default: "unique()",
167+
when: whenOverride
168+
}
169+
];
170+
const questionsPullProject = [
171+
...questionsProject,
172+
{
173+
type: "list",
174+
name: "project",
175+
message: "Choose your {{ spec.title|caseUcfirst }} project.",
176+
choices: async (answers) => {
177+
let response = await projectsList({
178+
parseOutput: false,
179+
queries: [JSON.stringify({ method: 'equal', attribute: 'teamId', values: [answers.organization.id] })],
180+
})
181+
let projects = response["projects"]
182+
let choices = projects.map((project, idx) => {
183+
return {
184+
name: `${project.name} (${project['$id']})`,
185+
value: {
186+
name: project.name,
187+
id: project['$id']
188+
}
189+
}
190+
})
191+
192+
if (choices.length == 0) {
193+
throw new Error("No projects found. Please create a new project.")
194+
}
195+
196+
return choices;
197+
},
198+
when: whenOverride
163199
}
164200
];
165201

0 commit comments

Comments
 (0)