Skip to content

Commit 2d8fe79

Browse files
committed
feat(cli): Init project forces login
1 parent 69d0eb2 commit 2d8fe79

File tree

6 files changed

+88
-63
lines changed

6 files changed

+88
-63
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: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@ const { Command } = require("commander");
22
const inquirer = require("inquirer");
33
const { projectsCreate } = require("./projects");
44
const { sdkForConsole } = require("../sdks");
5-
const { localConfig } = require("../config");
5+
const { localConfig, globalConfig } = require("../config");
66
const { questionsCreateProject } = require("../questions");
7-
const { success, actionRunner, commandDescriptions } = require("../parser");
7+
const { success, error, actionRunner, commandDescriptions } = require("../parser");
8+
const { accountGet } = require("./account");
9+
const { loginCommand } = require("./generic");
810

9-
const create = new Command("create")
10-
.description(commandDescriptions['create'])
11+
const init = new Command("init")
12+
.description(commandDescriptions['init'])
1113
.configureHelp({
1214
helpWidth: process.stdout.columns || 80
1315
})
1416
.action(actionRunner(async (_options, command) => {
1517
command.help();
1618
}));
1719

18-
const createProject = async () => {
19-
let response = {}
20+
const initProject = async () => {
21+
let response = {};
22+
23+
try {
24+
if (globalConfig.getEndpoint() === '' || globalConfig.getCookie() === '') {
25+
throw '';
26+
}
27+
await accountGet({
28+
parseOutput: false
29+
});
30+
} catch (e) {
31+
error('You must login first')
32+
await loginCommand()
33+
}
34+
2035
const answers = await inquirer.prompt(questionsCreateProject)
2136
if (!answers.project || !answers.organization) process.exit(1)
2237

@@ -31,11 +46,11 @@ const createProject = async () => {
3146
success();
3247
}
3348

34-
create
49+
init
3550
.command("project")
36-
.description("Create a new {{ spec.title|caseUcfirst }} project")
37-
.action(actionRunner(createProject));
51+
.description("Init and create a new {{ spec.title|caseUcfirst }} project")
52+
.action(actionRunner(initProject));
3853

3954
module.exports = {
40-
create,
55+
init,
4156
};

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: 10 additions & 4 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,15 @@ 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
163168
}
164169
];
165170
const questionsPullProject = [
@@ -189,7 +194,8 @@ const questionsPullProject = [
189194
}
190195

191196
return choices;
192-
}
197+
},
198+
when: whenOverride
193199
}
194200
];
195201

0 commit comments

Comments
 (0)