Skip to content

Commit fd78fad

Browse files
committed
feat(cli): Hooking migration before any command
1 parent 6eeef88 commit fd78fad

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

templates/cli/index.js.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { version } = require("./package.json");
1111
const { commandDescriptions, cliConfig } = require("./lib/parser");
1212
const { client } = require("./lib/commands/generic");
1313
{% if sdk.test != "true" %}
14-
const { login, logout, whoami } = require("./lib/commands/generic");
14+
const { login, logout, whoami, migrate } = require("./lib/commands/generic");
1515
const { pull } = require("./lib/commands/pull");
1616
const { push } = require("./lib/commands/push");
1717
{% endif %}
@@ -28,6 +28,7 @@ program
2828
.version(version, "-v, --version")
2929
.option("--verbose", "Show complete error log")
3030
.option("--json", "Output in JSON format")
31+
.hook('preAction',migrate)
3132
.on("option:json", () => {
3233
cliConfig.json = true;
3334
})

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

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { Command } = require("commander");
33
const Client = require("../client");
44
const { sdkForConsole, questionGetEndpoint } = require("../sdks");
55
const { globalConfig, localConfig } = require("../config");
6-
const { actionRunner, success, parseBool, commandDescriptions, error, parse, drawTable } = require("../parser");
6+
const { actionRunner, success, parseBool, commandDescriptions, error, parse,log, drawTable } = require("../parser");
77
{% if sdk.test != "true" %}
88
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
99
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
@@ -161,33 +161,6 @@ login
161161
success(`Current account is ${accountId}`);
162162
}));
163163

164-
login
165-
.command('migrate')
166-
.description("Migrate existing login to new scheme")
167-
.action(actionRunner(async ({ accountId }) => {
168-
const endpoint = globalConfig.getEndpoint();
169-
const cookie = globalConfig.getCookie();
170-
171-
if (endpoint === '' || cookie === '') {
172-
throw Error(`Couldn't find any existing account credentials`)
173-
}
174-
175-
const id = ID.unique();
176-
const data = {
177-
endpoint,
178-
cookie,
179-
email: 'legacy'
180-
};
181-
182-
globalConfig.addLogin(id, data);
183-
globalConfig.setCurrentLogin(id);
184-
globalConfig.delete('endpoint');
185-
globalConfig.delete('cookie');
186-
187-
success(`Account was migrated and it's the current account`);
188-
}));
189-
190-
191164
const logout = new Command("logout")
192165
.description(commandDescriptions['logout'])
193166
.configureHelp({
@@ -280,11 +253,35 @@ const client = new Command("client")
280253
success()
281254
}));
282255

256+
const migrate = async ()=>{
257+
if (!globalConfig.has('endpoint') || !globalConfig.has('cookie')) {
258+
return;
259+
}
260+
261+
const endpoint = globalConfig.get('endpoint');
262+
const cookie = globalConfig.get('cookie');
263+
264+
log("Old Appwrite login settings were detected, migrating...");
265+
const id = ID.unique();
266+
const data = {
267+
endpoint,
268+
cookie,
269+
email: 'legacy'
270+
};
271+
272+
globalConfig.addLogin(id, data);
273+
globalConfig.setCurrentLogin(id);
274+
globalConfig.delete('endpoint');
275+
globalConfig.delete('cookie');
276+
277+
success(`Account was migrated and it's the current account`);
278+
}
283279
module.exports = {
284280
{% if sdk.test != "true" %}
285281
whoami,
286282
login,
287283
logout,
284+
migrate,
288285
{% endif %}
289286
client
290287
};

templates/cli/lib/config.js.twig

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -467,46 +467,35 @@ class Global extends Config {
467467
this.setTo(Global.PREFERENCE_KEY, key);
468468
}
469469

470+
470471
hasFrom(key) {
471-
try {
472-
const current = this.getCurrentLogin();
472+
const current = this.getCurrentLogin();
473473

474-
if (current) {
475-
const config = this.get(current);
474+
if (current) {
475+
const config = this.get(current);
476476

477-
return config[key] !== undefined;
478-
}
479-
} catch {
480-
return this.has(key);
477+
return config[key] !== undefined;
481478
}
482479
}
483480

484481
getFrom(key) {
485-
try {
486-
const current = this.getCurrentLogin();
482+
const current = this.getCurrentLogin();
487483

488-
if (current) {
489-
const config = this.get(current);
484+
if (current) {
485+
const config = this.get(current);
490486

491-
return config[key];
492-
}
493-
} catch {
494-
return this.get(key);
487+
return config[key];
495488
}
496489
}
497490

498491
setTo(key, value) {
499-
try {
500-
const current = this.getCurrentLogin();
492+
const current = this.getCurrentLogin();
501493

502-
if (current) {
503-
const config = this.get(current);
494+
if (current) {
495+
const config = this.get(current);
504496

505-
config[key] = value;
506-
this.write();
507-
}
508-
} catch {
509-
this.set(key, value);
497+
config[key] = value;
498+
this.write();
510499
}
511500
}
512501
}

0 commit comments

Comments
 (0)