Skip to content

Commit a6bcc45

Browse files
committed
chore: update existing project to fetch region too
1 parent 9c4daab commit a6bcc45

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const { cliConfig, success, log, hint, error, actionRunner, commandDescriptions
2828
const { accountGet } = require("./account");
2929
const { sitesListTemplates } = require("./sites");
3030
const { sdkForConsole } = require("../sdks");
31+
const { isCloud } = require('../utils');
3132

3233
const initResources = async () => {
3334
const actions = {
@@ -94,12 +95,8 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
9495
}
9596
}
9697

97-
if (answers.region) {
98-
const endpoint = `https://${answers.region}.cloud.appwrite.io/v1`;
99-
localConfig.setEndpoint(endpoint);
100-
}
101-
10298
if (answers.start === 'new') {
99+
localConfig.clear();
103100
response = await projectsCreate({
104101
projectId: answers.id,
105102
name: answers.project,
@@ -108,8 +105,17 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
108105
})
109106

110107
localConfig.setProject(response['$id']);
108+
109+
if (answers.region) {
110+
const endpoint = `https://${answers.region}.cloud.appwrite.io/v1`;
111+
localConfig.setEndpoint(endpoint);
112+
}
111113
} else {
112-
localConfig.setProject(answers.project);
114+
localConfig.setProject(answers.project["$id"]);
115+
if(isCloud()) {
116+
const endpoint = `https://${answers.project["region"]}.cloud.appwrite.io/v1`;
117+
localConfig.setEndpoint(endpoint);
118+
}
113119
}
114120

115121
success(`Project successfully ${answers.start === 'existing' ? 'linked' : 'created'}. Details are now stored in appwrite.json file.`);

templates/cli/lib/questions.js.twig

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { validateRequired } = require("./validations");
1111
const { paginate } = require('./paginate');
1212
const { isPortTaken } = require('./utils');
1313
const { databasesList } = require('./commands/databases');
14-
const { checkDeployConditions } = require('./utils');
14+
const { checkDeployConditions, isCloud } = require('./utils');
1515
const JSONbig = require("json-bigint")({ storeAsString: false });
1616
const { sitesListFrameworks, sitesListSpecifications, sitesList } = require('./commands/sites');
1717

@@ -154,8 +154,7 @@ const questionsInitProject = [
154154
message: "Choose your organization",
155155
choices: async () => {
156156
let client = await sdkForConsole(true);
157-
const hostname = new URL(client.endpoint).hostname;
158-
const { teams } = hostname.endsWith('appwrite.io')
157+
const { teams } = isCloud()
159158
? await paginate(organizationsList, { parseOutput: false, sdk: client }, 100, 'teams')
160159
: await paginate(teamsList, { parseOutput: false, sdk: client }, 100, 'teams');
161160

@@ -203,7 +202,10 @@ const questionsInitProject = [
203202
let choices = projects.map((project) => {
204203
return {
205204
name: `${project.name} (${project['$id']})`,
206-
value: project['$id']
205+
value: {
206+
"$id": project['$id'],
207+
"region": project.region || ''
208+
}
207209
}
208210
})
209211

@@ -231,14 +233,9 @@ const questionsInitProject = [
231233
value: region.$id
232234
}));
233235
},
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-
}
236+
when: (answer) => {
237+
if (answer.start === 'existing') return false;
238+
return isCloud();
242239
}
243240
}
244241
];

templates/cli/lib/utils.js.twig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,17 @@ function getUsersPath(action, ids) {
274274
return path;
275275
}
276276

277+
function isCloud() {
278+
const endpoint = globalConfig.getEndpoint() || "https://cloud.appwrite.io/v1";
279+
const hostname = new URL(endpoint).hostname;
280+
return hostname.endsWith('appwrite.io');
281+
}
282+
277283
module.exports = {
278284
getAllFiles,
279285
isPortTaken,
280286
systemHasCommand,
281287
checkDeployConditions,
282-
showConsoleLink
288+
showConsoleLink,
289+
isCloud
283290
};

0 commit comments

Comments
 (0)