From bf7b50a64d97e9f1fd36827fee81b9089b70dbf8 Mon Sep 17 00:00:00 2001 From: Mara Date: Sat, 24 Feb 2024 19:40:37 +0100 Subject: [PATCH] fix: forgot to push token if not exists in env file --- src/settings/migrate.ts | 35 ++++++++++++++++++++---------- src/settings/modals/manage_repo.ts | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/settings/migrate.ts b/src/settings/migrate.ts index e8290fcb..59896d83 100644 --- a/src/settings/migrate.ts +++ b/src/settings/migrate.ts @@ -132,6 +132,7 @@ async function migrateWorFlow(plugin: GithubPublisher) { export async function migrateToken(plugin: GithubPublisher, token?: string, repo?: string) { logs({ settings: plugin.settings }, "migrating token"); const tokenPath = createTokenPath(plugin, plugin.settings.github.tokenPath); + console.log("TOKEN", token); //@ts-ignore if (plugin.settings.github.token && !token) { logs({ settings: plugin.settings }, `Moving the GitHub Token in the file : ${tokenPath}`); @@ -145,6 +146,10 @@ export async function migrateToken(plugin: GithubPublisher, token?: string, repo return; } logs({ settings: plugin.settings }, `Moving the GitHub Token in the file : ${tokenPath} for ${repo ?? "default"} repository`); + const exists = await plugin.app.vault.adapter.exists(tokenPath); + if (!exists) { + await plugin.app.vault.adapter.mkdir(normalizePath(tokenPath).split("/").slice(0, -1).join("/")); + } if (tokenPath.endsWith(".json")) { const envToken = repo ? { "GITHUB_PUBLISHER_REPOS" : { @@ -153,8 +158,9 @@ export async function migrateToken(plugin: GithubPublisher, token?: string, repo } : { GITHUB_PUBLISHER_TOKEN: token }; - if (!(await plugin.app.vault.adapter.exists(tokenPath))) { - await plugin.app.vault.adapter.mkdir(normalizePath(tokenPath).split("/").slice(0, -1).join("/")); + if (!exists) { + await plugin.app.vault.adapter.write(tokenPath, JSON.stringify(envToken, null, 2)); + return; } const oldToken = JSON.parse(await plugin.app.vault.adapter.read(tokenPath)); const newToken = { ...oldToken, ...envToken }; @@ -162,19 +168,26 @@ export async function migrateToken(plugin: GithubPublisher, token?: string, repo } else { const envToken = repo ? `${repo}_TOKEN=${token}` : `GITHUB_TOKEN=${token}`; + console.log("ENV TOKEN", envToken, tokenPath, exists); + if (!exists) { + await plugin.app.vault.adapter.write(tokenPath, envToken); + return; + } const oldToken = (await plugin.app.vault.adapter.read(tokenPath)).split("\n"); //search if old token is already in the file, if yes, replace it - for (const key in oldToken) { - if (key.startsWith("GITHUB_TOKEN") && !repo) { - oldToken[key] = envToken; - await plugin.app.vault.adapter.write(tokenPath, oldToken.join("\n")); - return; - } else if (key.startsWith(`${repo}_TOKEN`) && repo) { - oldToken[key] = envToken; - await plugin.app.vault.adapter.write(tokenPath, oldToken.join("\n")); - return; + for (let i = 0; i < oldToken.length; i++) { + if (oldToken[i].startsWith("GITHUB_TOKEN") && !repo) { + oldToken[i] = envToken; + break; + } else if (oldToken[i].startsWith(`${repo}_TOKEN`) && repo) { + oldToken[i] = envToken; + break; + } else if (i === oldToken.length - 1) { + oldToken.push(envToken); } } + await plugin.app.vault.adapter.write(tokenPath, oldToken.join("\n")); + } return; } diff --git a/src/settings/modals/manage_repo.ts b/src/settings/modals/manage_repo.ts index 44db5211..045c28d7 100644 --- a/src/settings/modals/manage_repo.ts +++ b/src/settings/modals/manage_repo.ts @@ -330,7 +330,7 @@ class ModalEditingRepository extends Modal { const decryptedToken: string = await this.plugin.loadToken(this.repository.smartKey); text .setPlaceholder("ghp_1234567890") - .setValue(this.repository.token ?? decryptedToken) + .setValue(decryptedToken) .onChange(async (value) => { await migrateToken(this.plugin, value.trim(), this.repository.smartKey); await this.plugin.saveSettings();