Skip to content

Commit e41c3a8

Browse files
committed
feat: per project endpoint to config
1 parent 312ba54 commit e41c3a8

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
9494
}
9595
}
9696

97+
if (answers.region) {
98+
const endpoint = `https://${answers.region}.cloud.appwrite.io/v1`;
99+
localConfig.setEndpoint(endpoint);
100+
}
101+
97102
if (answers.start === 'new') {
98103
response = await projectsCreate({
99104
projectId: answers.id,

templates/cli/lib/config.js.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class Local extends Config {
150150
return _path.dirname(this.path)
151151
}
152152

153+
getEndpoint() {
154+
return this.get('endpoint') || '';
155+
}
156+
157+
setEndpoint(endpoint) {
158+
this.set('endpoint', endpoint);
159+
}
160+
153161
getSites() {
154162
if (!this.has("sites")) {
155163
return [];

templates/cli/lib/questions.js.twig

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,39 @@ const questionsInitProject = [
207207
}
208208
})
209209

210-
if (choices.length == 0) {
210+
if (choices.length === 0) {
211211
throw new Error("No projects found. Please create a new project.")
212212
}
213213

214214
return choices;
215215
},
216216
when: (answer) => answer.start === 'existing'
217+
},
218+
{
219+
type: "list",
220+
name: "region",
221+
message: "Select your Appwrite Cloud region",
222+
choices: [
223+
{ name: "Frankfurt (fra)", value: "fra" },
224+
{ name: "New York (nyc)", value: "nyc" },
225+
{ name: "Sydney (syd)", value: "syd" }
226+
],
227+
default: () => {
228+
const endpoint = globalConfig.getEndpoint() || "https://cloud.appwrite.io/v1";
229+
if (endpoint.includes('fra')) return 'fra';
230+
if (endpoint.includes('nyc')) return 'nyc';
231+
if (endpoint.includes('syd')) return 'syd';
232+
return '';
233+
},
234+
when: () => {
235+
try {
236+
const endpoint = globalConfig.getEndpoint() || "https://cloud.appwrite.io/v1";
237+
const hostname = new URL(endpoint).hostname;
238+
return hostname.endsWith('appwrite.io');
239+
} catch {
240+
return false;
241+
}
242+
}
217243
}
218244
];
219245
const questionsInitProjectAutopull = [

templates/cli/lib/sdks.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const sdkForConsole = async (requiresAuth = true) => {
2323

2424
const sdkForProject = async () => {
2525
let client = new Client();
26-
let endpoint = globalConfig.getEndpoint();
26+
let endpoint = localConfig.getEndpoint() || globalConfig.getEndpoint();
2727
let project = localConfig.getProject().projectId ? localConfig.getProject().projectId : globalConfig.getProject();
2828
let key = globalConfig.getKey();
2929
let cookie = globalConfig.getCookie()

0 commit comments

Comments
 (0)