Skip to content

Commit 2db03b5

Browse files
Merge pull request #874 from appwrite/feat-general-improvements
Feat general improvements
2 parents b5ca8bf + 5fe0bd2 commit 2db03b5

File tree

8 files changed

+55
-30
lines changed

8 files changed

+55
-30
lines changed

templates/cli/lib/client.js.twig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ 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');
78

89
class Client {
910
CHUNK_SIZE = 5*1024*1024; // 5MB
10-
11+
1112
constructor() {
1213
this.endpoint = '{{spec.endpoint}}';
1314
this.headers = {
@@ -144,6 +145,13 @@ class Client {
144145
} catch (error) {
145146
throw new {{spec.title | caseUcfirst}}Exception(text, response.status, "", text);
146147
}
148+
149+
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);
154+
}
147155
throw new {{spec.title | caseUcfirst}}Exception(json.message, json.code, json.type, json);
148156
}
149157

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@ 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, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
10-
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
9+
const { questionsLogin, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
10+
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
1111

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

14-
const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }) => {
14+
const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
1515
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-
}
16+
let configEndpoint = endpoint ?? DEFAULT_ENDPOINT;
2517

18+
const answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
2619

2720
if (answers.method === 'select') {
2821
const accountId = answers.accountId;
@@ -123,7 +116,8 @@ const whoami = new Command("whoami")
123116
'ID': account.$id,
124117
'Name': account.name,
125118
'Email': account.email,
126-
'MFA enabled': account.mfa ? 'Yes' : 'No'
119+
'MFA enabled': account.mfa ? 'Yes' : 'No',
120+
'Endpoint': globalConfig.getEndpoint()
127121
}
128122
];
129123
if (json) {
@@ -137,7 +131,6 @@ const whoami = new Command("whoami")
137131

138132
const login = new Command("login")
139133
.description(commandDescriptions['login'])
140-
.option(`-sh, --self-hosted`, `Flag for enabling custom endpoint for self hosted instances`)
141134
.option(`--email [email]`, `User email`)
142135
.option(`--password [password]`, `User password`)
143136
.option(`--endpoint [endpoint]`, `Appwrite endpoint for self hosted instances`)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ const {
1818
questionsCreateCollection,
1919
questionsInitProject
2020
} = require("../questions");
21-
const { success, log, error, actionRunner, commandDescriptions } = require("../parser");
21+
const { success, log, error, actionRunner } = require("../parser");
2222
const { accountGet } = require("./account");
23-
const { loginCommand } = require("./generic");
2423
const { sdkForConsole } = require("../sdks");
2524

2625
const initProject = async ({ organizationId, projectId, projectName } = {}) => {
@@ -37,8 +36,8 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
3736
sdk: client
3837
});
3938
} catch (e) {
40-
log('You must login first')
41-
await loginCommand();
39+
error('Error Session not found. Please run `appwrite login` to create a session');
40+
process.exit(1);
4241
}
4342

4443
let answers = {};

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ const createAttribute = async (databaseId, collectionId, attribute) => {
344344
collectionId,
345345
key: attribute.key,
346346
required: attribute.required,
347-
min: attribute.min,
348-
max: attribute.max,
347+
min: parseInt(attribute.min.toString()),
348+
max: parseInt(attribute.max.toString()),
349349
xdefault: attribute.default,
350350
array: attribute.array,
351351
parseOutput: false
@@ -356,8 +356,8 @@ const createAttribute = async (databaseId, collectionId, attribute) => {
356356
collectionId,
357357
key: attribute.key,
358358
required: attribute.required,
359-
min: attribute.min,
360-
max: attribute.max,
359+
min: parseFloat(attribute.min.toString()),
360+
max: parseFloat(attribute.max.toString()),
361361
xdefault: attribute.default,
362362
array: attribute.array,
363363
parseOutput: false
@@ -481,7 +481,14 @@ const attributesToCreate = async (remoteAttributes, localAttributes, collection)
481481
return { Key: change.key, Action: change.action, Reason: change.reason, };
482482
}));
483483

484-
if (!cliConfig.force) {
484+
if (!cliConfig.force && (deleting.length > 0 || conflicts.length > 0)) {
485+
if (deleting.length > 0) {
486+
log(`Attribute deletion will cause ${chalk.red('loss of data')}`);
487+
}
488+
if (conflicts.length > 0) {
489+
log(`Attribute recreation will cause ${chalk.red('loss of data')}`);
490+
}
491+
485492
const answers = await inquirer.prompt(questionsPushCollections[1]);
486493

487494
if (answers.changes.toLowerCase() !== 'yes') {
@@ -932,7 +939,18 @@ const pushFunction = async ({ functionId, async, returnOnZero } = { returnOnZero
932939
error(`Deployment of ${name} has failed. Check at ${failUrl} for more details\n`);
933940
})
934941

935-
success(`Pushed ${successfullyPushed} functions with ${successfullyDeployed} successful deployments.`);
942+
let message = chalk.green(`Pushed and deployed ${successfullyPushed} functions`);
943+
944+
if (!async) {
945+
if (successfullyDeployed < successfullyPushed) {
946+
message = `${chalk.green(`Pushed and deployed ${successfullyPushed} functions.`)} ${chalk.red(`${successfullyPushed - successfullyDeployed} failed to deploy`)}`;
947+
} else {
948+
if (successfullyPushed === 0) {
949+
message = chalk.red(`Error pushing ${functions.length} functions`)
950+
}
951+
}
952+
}
953+
log(message);
936954
}
937955

938956
const pushCollection = async ({ returnOnZero } = { returnOnZero: false }) => {
@@ -1034,7 +1052,7 @@ const pushCollection = async ({ returnOnZero } = { returnOnZero: false }) => {
10341052
}
10351053
}))
10361054

1037-
// Serialize attribute actions
1055+
// Serialize attribute actions
10381056
for (let collection of collections) {
10391057
let attributes = collection.attributes;
10401058

templates/cli/lib/parser.js.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ const parseError = (err) => {
151151
if (cliConfig.verbose) {
152152
console.error(err);
153153
} else {
154+
log('For detailed error pass the --verbose or --report flag');
154155
error(err.message);
155156
}
156157
process.exit(1);

templates/cli/lib/questions.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ const questionsPushCollections = [
628628
{
629629
type: "input",
630630
name: "changes",
631-
message: `Are you sure you want to override this collection? This can lead to loss of data! Type "YES" to confirm.`
631+
message: `Would you like to apply these changes? Type "YES" to confirm.`
632632
}
633633
]
634634

templates/cli/lib/sdks.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const sdkForProject = async () => {
6868
}
6969

7070
if (!project) {
71-
throw new Error("Project is not set. Please run `{{ language.params.executableName }} pull project` to initialize the current directory with an {{ spec.title|caseUcfirst }} project.");
71+
throw new Error("Project is not set. Please run `{{ language.params.executableName }} init` to initialize the current directory with an {{ spec.title|caseUcfirst }} project.");
7272
}
7373

7474
client

templates/cli/lib/utils.js.twig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require("fs");
22
const path = require("path");
33
const { localConfig, globalConfig } = require("./config");
4-
const { success, log } = require('./parser')
4+
const { success, log, error } = require('./parser')
55
const readline = require('readline');
66
const cp = require('child_process');
77

@@ -67,7 +67,13 @@ function showConsoleLink(serviceName, action, open, ...ids) {
6767

6868
if (open) {
6969
const start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open');
70-
cp.exec(`${start} ${url}`);
70+
71+
cp.exec(`${start} ${url}`, (err, stdout, stderr) => {
72+
if (err !== null) {
73+
console.log('\n');
74+
error('Opening in default browser. ' + err)
75+
}
76+
});
7177
}
7278
}
7379

0 commit comments

Comments
 (0)