Skip to content

Commit

Permalink
refactor(cli): reorder questions
Browse files Browse the repository at this point in the history
  • Loading branch information
byawitz committed May 31, 2024
1 parent b35b4bb commit 3621c3c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
25 changes: 14 additions & 11 deletions templates/cli/lib/commands/generic.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ const { globalConfig, localConfig } = require("../config");
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser");
const ID = require("../id");
{% if sdk.test != "true" %}
const { questionsLogin, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
const { questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");

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

const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }) => {
const answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
const oldCurrent = globalConfig.getCurrentLogin();
let answers = {};
let configEndpoint = DEFAULT_ENDPOINT;

if (selfHosted) {
answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionLoginWithEndpoint);
configEndpoint = answers.endpoint;
} else {
answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
}


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

const oldCurrent = globalConfig.getCurrentLogin();
const id = ID.unique();

globalConfig.setCurrentLogin(id);
globalConfig.addLogin(id, {});
globalConfig.setCurrentLogin(id);
globalConfig.setEndpoint(configEndpoint);
globalConfig.setEmail(answers.email);
globalConfig.setEndpoint(DEFAULT_ENDPOINT);

if (selfHosted) {
const selfHostedAnswers = endpoint ? { endpoint } : await inquirer.prompt(questionGetEndpoint);

globalConfig.setEndpoint(selfHostedAnswers.endpoint);
}

let client = await sdkForConsole(false);

Expand Down
68 changes: 39 additions & 29 deletions templates/cli/lib/questions.js.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chalk = require("chalk");
const Client = require("./client");
const { localConfig, globalConfig } = require('./config');
const { projectsList } = require('./commands/projects');
const { teamsList } = require('./commands/teams');
Expand All @@ -11,7 +12,7 @@ const { databasesList } = require('./commands/databases');
const { checkDeployConditions } = require('./utils');
const JSONbig = require("json-bigint")({ storeAsString: false });

const whenOverride = (answers)=> answers.override === undefined ? true : answers.override;
const whenOverride = (answers) => answers.override === undefined ? true : answers.override;

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

if(functions.length === 0){
if (functions.length === 0) {
throw "We couldn't find any functions in your {{ spec.title|caseUcfirst }} project";
}

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

return { value: template, name }
})
Expand Down Expand Up @@ -474,9 +475,40 @@ const questionsLogin = [
},
when: (answers) => answers.method === 'select'
},

];
const questionGetEndpoint = [
{
type: "input",
name: "endpoint",
message: "Enter the endpoint of your {{ spec.title|caseUcfirst }} server",
default: "http://localhost/v1",
async validate(value) {
if (!value) {
return "Please enter a valid endpoint.";
}
let client = new Client().setEndpoint(value);
try {
let response = await client.call('get', '/health/version');
if (response.version) {
return true;
} else {
throw new Error();
}
} catch (error) {
return "Invalid endpoint or your Appwrite server is not running as expected.";
}
}
}
];

const questionLoginWithEndpoint = [
questionsLogin[0],
{ ...questionGetEndpoint[0], when: (answers) => answers.method !== 'select' },
questionsLogin[1],
questionsLogin[2],
questionsLogin[3]
]

const questionsLogout = [
{
type: "checkbox",
Expand Down Expand Up @@ -722,30 +754,7 @@ const questionsMfaChallenge = [
}
];

const questionGetEndpoint = [
{
type: "input",
name: "endpoint",
message: "Enter the endpoint of your {{ spec.title|caseUcfirst }} server",
default: "http://localhost/v1",
async validate(value) {
if (!value) {
return "Please enter a valid endpoint.";
}
let client = new Client().setEndpoint(value);
try {
let response = await client.call('get', '/health/version');
if (response.version) {
return true;
} else {
throw new Error();
}
} catch (error) {
return "Invalid endpoint or your Appwrite server is not running as expected.";
}
}
}
];


module.exports = {
questionsInitProject,
Expand All @@ -768,5 +777,6 @@ module.exports = {
questionsGetEntrypoint,
questionsListFactors,
questionsMfaChallenge,
questionGetEndpoint
questionGetEndpoint,
questionLoginWithEndpoint
};

0 comments on commit 3621c3c

Please sign in to comment.