Skip to content

Commit 309d764

Browse files
committed
refactor: reviews
1 parent 3e054b0 commit 309d764

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

templates/cli/lib/client.js.twig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { fetch, FormData, Agent } = require("undici");
44
const JSONbig = require("json-bigint")({ storeAsString: false });
55
const {{spec.title | caseUcfirst}}Exception = require("./exception.js");
66
const { globalConfig } = require("./config.js");
7-
const { log } = require('./parser');
7+
const chalk = require("chalk");
88

99
class Client {
1010
CHUNK_SIZE = 5*1024*1024; // 5MB
@@ -147,10 +147,11 @@ class Client {
147147
}
148148

149149
if (path !== '/account' && json.code === 401 && json.type === 'user_more_factors_required') {
150-
log('Unusable account found, removing...')
151-
const current = globalConfig.getCurrentLogin();
152-
globalConfig.setCurrentLogin('');
153-
globalConfig.removeLogin(current);
150+
console.log(`${chalk.cyan.bold("ℹ Info")} ${chalk.cyan("Unusable account found, removing...")}`);
151+
152+
const current = globalConfig.getCurrentSession();
153+
globalConfig.setCurrentSession('');
154+
globalConfig.removeSession(current);
154155
}
155156
throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, json);
156157
}

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accou
1212
const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1';
1313

1414
const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
15-
const oldCurrent = globalConfig.getCurrentLogin();
15+
const oldCurrent = globalConfig.getCurrentSession();
1616
let configEndpoint = endpoint ?? DEFAULT_ENDPOINT;
1717

1818
const answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
@@ -24,16 +24,16 @@ const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
2424
throw Error('Login ID not found');
2525
}
2626

27-
globalConfig.setCurrentLogin(accountId);
27+
globalConfig.setCurrentSession(accountId);
2828
success(`Current account is ${accountId}`);
2929

3030
return;
3131
}
3232

3333
const id = ID.unique();
3434

35-
globalConfig.addLogin(id, {});
36-
globalConfig.setCurrentLogin(id);
35+
globalConfig.addSession(id, {});
36+
globalConfig.setCurrentSession(id);
3737
globalConfig.setEndpoint(configEndpoint);
3838
globalConfig.setEmail(answers.email);
3939

@@ -79,8 +79,8 @@ const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
7979
parseOutput: false
8080
});
8181
} else {
82-
globalConfig.removeLogin(id);
83-
globalConfig.setCurrentLogin(oldCurrent);
82+
globalConfig.removeSession(id);
83+
globalConfig.setCurrentSession(oldCurrent);
8484
throw error;
8585
}
8686
}
@@ -141,7 +141,7 @@ const login = new Command("login")
141141
})
142142
.action(actionRunner(loginCommand));
143143

144-
const singleLogout = async (accountId) => {
144+
const deleteSession = async (accountId) => {
145145
try {
146146
let client = await sdkForConsole();
147147

@@ -151,11 +151,11 @@ const singleLogout = async (accountId) => {
151151
sdk: client
152152
})
153153

154-
globalConfig.removeLogin(accountId);
154+
globalConfig.removeSession(accountId);
155155
} catch (e) {
156156
error('Unable to log out, removing locally saved session information')
157157
}
158-
globalConfig.removeLogin(accountId);
158+
globalConfig.removeSession(accountId);
159159
}
160160

161161
const logout = new Command("logout")
@@ -164,14 +164,14 @@ const logout = new Command("logout")
164164
helpWidth: process.stdout.columns || 80
165165
})
166166
.action(actionRunner(async () => {
167-
const logins = globalConfig.getLogins();
168-
const current = globalConfig.getCurrentLogin();
167+
const logins = globalConfig.getSessions();
168+
const current = globalConfig.getCurrentSession();
169169

170170
if (current === '') {
171171
return;
172172
}
173173
if (logins.length === 1) {
174-
await singleLogout(current);
174+
await deleteSession(current);
175175
success();
176176

177177
return;
@@ -181,16 +181,16 @@ const logout = new Command("logout")
181181

182182
if (answers.accounts) {
183183
for (let accountId of answers.accounts) {
184-
globalConfig.setCurrentLogin(accountId);
185-
await singleLogout(accountId);
184+
globalConfig.setCurrentSession(accountId);
185+
await deleteSession(accountId);
186186
}
187187
}
188188

189-
const leftLogins = globalConfig.getLogins();
189+
const leftLogins = globalConfig.getSessions();
190190

191191
if (leftLogins.length > 0 && leftLogins.filter(login => login.id === current).length !== 1) {
192192
const accountId = leftLogins[0].id;
193-
globalConfig.setCurrentLogin(accountId);
193+
globalConfig.setCurrentSession(accountId);
194194

195195
success(`Current account is ${accountId}`);
196196
}
@@ -243,8 +243,8 @@ const client = new Command("client")
243243
if (!response.version) {
244244
throw new Error();
245245
}
246-
globalConfig.setCurrentLogin(id);
247-
globalConfig.addLogin(id, {});
246+
globalConfig.setCurrentSession(id);
247+
globalConfig.addSession(id, {});
248248
globalConfig.setEndpoint(endpoint);
249249
} catch (_) {
250250
throw new Error("Invalid endpoint or your Appwrite server is not running as expected.");
@@ -264,11 +264,11 @@ const client = new Command("client")
264264
}
265265

266266
if (reset !== undefined) {
267-
const logins = globalConfig.getLogins();
267+
const logins = globalConfig.getSessions();
268268

269269
for (let accountId of logins.map(login => login.id)) {
270-
globalConfig.setCurrentLogin(accountId);
271-
await singleLogout(accountId);
270+
globalConfig.setCurrentSession(accountId);
271+
await deleteSession(accountId);
272272
}
273273
}
274274

@@ -290,8 +290,8 @@ const migrate = async () => {
290290
email: 'legacy'
291291
};
292292

293-
globalConfig.addLogin(id, data);
294-
globalConfig.setCurrentLogin(id);
293+
globalConfig.addSession(id, data);
294+
globalConfig.setCurrentSession(id);
295295
globalConfig.delete('endpoint');
296296
globalConfig.delete('cookie');
297297

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ const pushProject = async () => {
593593
try {
594594
const projectId = localConfig.getProject().projectId;
595595
const projectName = localConfig.getProject().projectName;
596+
const settings = localConfig.getProject().projectSettings;
596597

597598
log(`Updating project ${projectName} ( ${projectId} )`);
598599

@@ -602,8 +603,8 @@ const pushProject = async () => {
602603
name: projectName,
603604
parseOutput: false
604605
});
606+
}
605607

606-
const settings = localConfig.getProject().projectSettings;
607608

608609
if (settings.services) {
609610
log('Updating service statuses');

templates/cli/lib/config.js.twig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,22 +407,22 @@ class Global extends Config {
407407
super(`${homeDir}/${path}`);
408408
}
409409

410-
getCurrentLogin() {
410+
getCurrentSession() {
411411
if (!this.has(Global.PREFERENCE_CURRENT)) {
412412
return "";
413413
}
414414
return this.get(Global.PREFERENCE_CURRENT);
415415
}
416416

417-
setCurrentLogin(endpoint) {
417+
setCurrentSession(endpoint) {
418418
this.set(Global.PREFERENCE_CURRENT, endpoint);
419419
}
420420

421421
getLoginIds() {
422422
return Object.keys(this.data).filter((key) => !Global.IGNORE_ATTRIBUTES.includes(key));
423423
}
424424

425-
getLogins() {
425+
getSessions() {
426426
const logins = Object.keys(this.data).filter((key) => !Global.IGNORE_ATTRIBUTES.includes(key))
427427

428428
return logins.map((login) => {
@@ -435,11 +435,11 @@ class Global extends Config {
435435
})
436436
}
437437

438-
addLogin(login, data) {
438+
addSession(login, data) {
439439
this.set(login, data);
440440
}
441441

442-
removeLogin(login, data) {
442+
removeSession(login, data) {
443443
this.delete(login);
444444
}
445445

@@ -513,7 +513,7 @@ class Global extends Config {
513513

514514

515515
hasFrom(key) {
516-
const current = this.getCurrentLogin();
516+
const current = this.getCurrentSession();
517517

518518
if (current) {
519519
const config = this.get(current);
@@ -523,7 +523,7 @@ class Global extends Config {
523523
}
524524

525525
getFrom(key) {
526-
const current = this.getCurrentLogin();
526+
const current = this.getCurrentSession();
527527

528528
if (current) {
529529
const config = this.get(current);
@@ -533,7 +533,7 @@ class Global extends Config {
533533
}
534534

535535
setTo(key, value) {
536-
const current = this.getCurrentLogin();
536+
const current = this.getCurrentSession();
537537

538538
if (current) {
539539
const config = this.get(current);

templates/cli/lib/questions.js.twig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ const questionsLogin = [
444444
{ name: 'Login to a different account', value: 'login' },
445445
{ name: 'Change to a different existed account', value: 'select' }
446446
],
447-
when: () => globalConfig.getCurrentLogin() !== ''
447+
when: () => globalConfig.getCurrentSession() !== ''
448448
},
449449
{
450450
type: "input",
@@ -476,8 +476,8 @@ const questionsLogin = [
476476
name: "accountId",
477477
message: "Select an account to use",
478478
choices() {
479-
const logins = globalConfig.getLogins();
480-
const current = globalConfig.getCurrentLogin();
479+
const logins = globalConfig.getSessions();
480+
const current = globalConfig.getCurrentSession();
481481

482482
const data = [];
483483

@@ -538,8 +538,8 @@ const questionsLogout = [
538538
message: "Select accounts to logout from",
539539
validate: (value) => validateRequired('account', value),
540540
choices() {
541-
const logins = globalConfig.getLogins();
542-
const current = globalConfig.getCurrentLogin();
541+
const logins = globalConfig.getSessions();
542+
const current = globalConfig.getCurrentSession();
543543

544544
const data = [];
545545

0 commit comments

Comments
 (0)