Skip to content

Commit 3621c3c

Browse files
committed
refactor(cli): reorder questions
1 parent b35b4bb commit 3621c3c

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
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: 39 additions & 29 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');
@@ -11,7 +12,7 @@ const { databasesList } = require('./commands/databases');
1112
const { checkDeployConditions } = require('./utils');
1213
const JSONbig = require("json-bigint")({ storeAsString: false });
1314

14-
const whenOverride = (answers)=> answers.override === undefined ? true : answers.override;
15+
const whenOverride = (answers) => answers.override === undefined ? true : answers.override;
1516

1617
const getIgnores = (runtime) => {
1718
const languge = runtime.split('-')[0];
@@ -238,7 +239,7 @@ const questionsPullFunctions = [
238239
choices: async () => {
239240
const { functions } = await paginate(functionsList, { parseOutput: false }, 100, 'functions');
240241

241-
if(functions.length === 0){
242+
if (functions.length === 0) {
242243
throw "We couldn't find any functions in your {{ spec.title|caseUcfirst }} project";
243244
}
244245

@@ -298,7 +299,7 @@ const questionsCreateFunctionSelectTemplate = (templates) => {
298299
name: "template",
299300
message: "What template would you like to use?",
300301
choices: templates.map((template) => {
301-
const name =`${template[0].toUpperCase()}${template.split('').slice(1).join('')}`.replace(/[-_]/g,' ');
302+
const name = `${template[0].toUpperCase()}${template.split('').slice(1).join('')}`.replace(/[-_]/g, ' ');
302303

303304
return { value: template, name }
304305
})
@@ -474,9 +475,40 @@ const questionsLogin = [
474475
},
475476
when: (answers) => answers.method === 'select'
476477
},
477-
478+
];
479+
const questionGetEndpoint = [
480+
{
481+
type: "input",
482+
name: "endpoint",
483+
message: "Enter the endpoint of your {{ spec.title|caseUcfirst }} server",
484+
default: "http://localhost/v1",
485+
async validate(value) {
486+
if (!value) {
487+
return "Please enter a valid endpoint.";
488+
}
489+
let client = new Client().setEndpoint(value);
490+
try {
491+
let response = await client.call('get', '/health/version');
492+
if (response.version) {
493+
return true;
494+
} else {
495+
throw new Error();
496+
}
497+
} catch (error) {
498+
return "Invalid endpoint or your Appwrite server is not running as expected.";
499+
}
500+
}
501+
}
478502
];
479503

504+
const questionLoginWithEndpoint = [
505+
questionsLogin[0],
506+
{ ...questionGetEndpoint[0], when: (answers) => answers.method !== 'select' },
507+
questionsLogin[1],
508+
questionsLogin[2],
509+
questionsLogin[3]
510+
]
511+
480512
const questionsLogout = [
481513
{
482514
type: "checkbox",
@@ -722,30 +754,7 @@ const questionsMfaChallenge = [
722754
}
723755
];
724756

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

750759
module.exports = {
751760
questionsInitProject,
@@ -768,5 +777,6 @@ module.exports = {
768777
questionsGetEntrypoint,
769778
questionsListFactors,
770779
questionsMfaChallenge,
771-
questionGetEndpoint
780+
questionGetEndpoint,
781+
questionLoginWithEndpoint
772782
};

0 commit comments

Comments
 (0)