Skip to content

Commit fcb2ddc

Browse files
Merge pull request #868 from appwrite/feat-arg-project-init
Feat arg project init
2 parents 3621c3c + 0cf2882 commit fcb2ddc

File tree

13 files changed

+239
-166
lines changed

13 files changed

+239
-166
lines changed

templates/cli/base/requests/api.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if (parseOutput) {
2020
{%~ if methodHaveConsolePreview(method.name,service.name) %}
2121
if(console) {
22-
showConsoleLink('{{service.name}}', '{{ method.name }}',open
22+
showConsoleLink('{{service.name}}', '{{ method.name }}'
2323
{%- for parameter in method.parameters.path -%}{%- set param = (parameter.name | caseCamel | escapeKeyword) -%}{%- if param ends with 'Id' -%}, {{ param }} {%- endif -%}{%- endfor -%}
2424
);
2525
} else {

templates/cli/index.js.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const chalk = require("chalk");
1010
const { version } = require("./package.json");
1111
const { commandDescriptions, cliConfig } = require("./lib/parser");
1212
const { client } = require("./lib/commands/generic");
13+
const inquirer = require("inquirer");
1314
{% if sdk.test != "true" %}
1415
const { login, logout, whoami, migrate } = require("./lib/commands/generic");
1516
const { init } = require("./lib/commands/init");
@@ -22,6 +23,8 @@ const { migrate } = require("./lib/commands/generic");
2223
const { {{ service.name | caseLower }} } = require("./lib/commands/{{ service.name | caseLower }}");
2324
{% endfor %}
2425

26+
inquirer.registerPrompt('search-list', require('inquirer-search-list'));
27+
2528
program
2629
.description(commandDescriptions['main'])
2730
.configureHelp({

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/command.js.twig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({
7070
{%- if 'multipart/form-data' in method.consumes -%},onProgress = () => {}{%- endif -%}
7171

7272
{%- if method.type == 'location' -%}, destination{%- endif -%}
73-
{% if methodHaveConsolePreview(method.name,service.name) %}, console, open{%- endif -%}
73+
{% if methodHaveConsolePreview(method.name,service.name) %}, console{%- endif -%}
7474
}) => {
7575
{%~ endblock %}
7676
let client = !sdk ? await {% if service.name == "projects" %}sdkForConsole(){% else %}sdkForProject(){% endif %} :
@@ -97,7 +97,6 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({
9797
{% endif %}
9898
{% if methodHaveConsolePreview(method.name,service.name) %}
9999
.option(`--console`, `Get the resource console url`)
100-
.option(`--open`, `Use with '--console' to open the using default browser`)
101100
{% endif %}
102101
{% endautoescape %}
103102
.action(actionRunner({{ service.name | caseLower }}{{ method.name | caseUcfirst }}))

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`)

0 commit comments

Comments
 (0)