Skip to content

Commit 64c364e

Browse files
committed
Merge branch 'refs/heads/feat-multiple-accounts-and-instances' into feat-arg-project-init
2 parents 5bba9cd + 3621c3c commit 64c364e

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ const { globalConfig, localConfig } = require("../config");
66
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser");
77
const ID = require("../id");
88
{% if sdk.test != "true" %}
9-
const { questionsLogin, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
9+
const { questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
1010
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
1111

1212
const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1';
1313

1414
const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }) => {
15-
const answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
15+
const oldCurrent = globalConfig.getCurrentLogin();
16+
let answers = {};
17+
let configEndpoint = DEFAULT_ENDPOINT;
18+
19+
if (selfHosted) {
20+
answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionLoginWithEndpoint);
21+
configEndpoint = answers.endpoint;
22+
} else {
23+
answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
24+
}
25+
1626

1727
if (answers.method === 'select') {
1828
const accountId = answers.accountId;
@@ -27,19 +37,12 @@ const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }
2737
return;
2838
}
2939

30-
const oldCurrent = globalConfig.getCurrentLogin();
3140
const id = ID.unique();
3241

33-
globalConfig.setCurrentLogin(id);
3442
globalConfig.addLogin(id, {});
43+
globalConfig.setCurrentLogin(id);
44+
globalConfig.setEndpoint(configEndpoint);
3545
globalConfig.setEmail(answers.email);
36-
globalConfig.setEndpoint(DEFAULT_ENDPOINT);
37-
38-
if (selfHosted) {
39-
const selfHostedAnswers = endpoint ? { endpoint } : await inquirer.prompt(questionGetEndpoint);
40-
41-
globalConfig.setEndpoint(selfHostedAnswers.endpoint);
42-
}
4346

4447
let client = await sdkForConsole(false);
4548

templates/cli/lib/questions.js.twig

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const chalk = require("chalk");
2+
const Client = require("./client");
23
const { localConfig, globalConfig } = require('./config');
34
const { projectsList } = require('./commands/projects');
45
const { teamsList } = require('./commands/teams');
@@ -471,8 +472,39 @@ const questionsLogin = [
471472
},
472473
when: (answers) => answers.method === 'select'
473474
},
474-
475475
];
476+
const questionGetEndpoint = [
477+
{
478+
type: "input",
479+
name: "endpoint",
480+
message: "Enter the endpoint of your {{ spec.title|caseUcfirst }} server",
481+
default: "http://localhost/v1",
482+
async validate(value) {
483+
if (!value) {
484+
return "Please enter a valid endpoint.";
485+
}
486+
let client = new Client().setEndpoint(value);
487+
try {
488+
let response = await client.call('get', '/health/version');
489+
if (response.version) {
490+
return true;
491+
} else {
492+
throw new Error();
493+
}
494+
} catch (error) {
495+
return "Invalid endpoint or your Appwrite server is not running as expected.";
496+
}
497+
}
498+
}
499+
];
500+
501+
const questionLoginWithEndpoint = [
502+
questionsLogin[0],
503+
{ ...questionGetEndpoint[0], when: (answers) => answers.method !== 'select' },
504+
questionsLogin[1],
505+
questionsLogin[2],
506+
questionsLogin[3]
507+
]
476508

477509
const questionsLogout = [
478510
{
@@ -719,30 +751,7 @@ const questionsMfaChallenge = [
719751
}
720752
];
721753

722-
const questionGetEndpoint = [
723-
{
724-
type: "input",
725-
name: "endpoint",
726-
message: "Enter the endpoint of your {{ spec.title|caseUcfirst }} server",
727-
default: "http://localhost/v1",
728-
async validate(value) {
729-
if (!value) {
730-
return "Please enter a valid endpoint.";
731-
}
732-
let client = new Client().setEndpoint(value);
733-
try {
734-
let response = await client.call('get', '/health/version');
735-
if (response.version) {
736-
return true;
737-
} else {
738-
throw new Error();
739-
}
740-
} catch (error) {
741-
return "Invalid endpoint or your Appwrite server is not running as expected.";
742-
}
743-
}
744-
}
745-
];
754+
746755

747756
module.exports = {
748757
questionsInitProject,
@@ -765,5 +774,6 @@ module.exports = {
765774
questionsGetEntrypoint,
766775
questionsListFactors,
767776
questionsMfaChallenge,
768-
questionGetEndpoint
777+
questionGetEndpoint,
778+
questionLoginWithEndpoint
769779
};

0 commit comments

Comments
 (0)