From 9959515c1461b27adb4143ff4f1520f5a045924e Mon Sep 17 00:00:00 2001 From: CK <69121180+TfTHacker@users.noreply.github.com> Date: Mon, 18 Oct 2021 11:59:34 +0200 Subject: [PATCH] 0.4.2 GitHub CP open --- manifest-beta.json | 2 +- src/BetaPlugins.ts | 30 ++++++++++++++++-------------- src/githubUtils.ts | 11 +++++++++++ src/main.ts | 20 +++++++++++++++++++- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/manifest-beta.json b/manifest-beta.json index be5f0ac..10efb17 100644 --- a/manifest-beta.json +++ b/manifest-beta.json @@ -1,7 +1,7 @@ { "id": "obsidian42-brat", "name": "Obsidian42 - BRAT", - "version": "0.4.1", + "version": "0.4.2", "minAppVersion": "0.9.12", "description": "Easily install a beta version of a plugin for testing.", "author": "TfTHacker", diff --git a/src/BetaPlugins.ts b/src/BetaPlugins.ts index b15d6b7..e92fa0e 100644 --- a/src/BetaPlugins.ts +++ b/src/BetaPlugins.ts @@ -1,7 +1,7 @@ import ThePlugin from "./main"; import AddNewPluginModal from "./AddNewPluginModal"; import { grabManifestJsonFromRepository, grabReleaseFileFromRepository } from "./githubUtils"; -import { Notice, PluginManifest } from "obsidian"; +import { normalizePath, Notice, PluginManifest } from "obsidian"; import { addBetaPluginToList } from "./settings"; /** @@ -88,15 +88,16 @@ export default class BetaPlugins { * @return {Promise} */ async writeReleaseFilesToPluginFolder(betaPluginID: string, relFiles: ReleaseFiles): Promise { - const pluginTargetFolderPath = this.plugin.app.vault.configDir + "/plugins/" + betaPluginID + "/"; - if (await this.plugin.app.vault.adapter.exists(pluginTargetFolderPath) === false || - !(await this.plugin.app.vault.adapter.exists(pluginTargetFolderPath + "manifest.json"))) { + const pluginTargetFolderPath = normalizePath(this.plugin.app.vault.configDir + "/plugins/" + betaPluginID) + "/"; + const adapter = this.plugin.app.vault.adapter; + if (await adapter.exists(pluginTargetFolderPath) === false || + !(await adapter.exists(pluginTargetFolderPath + "manifest.json"))) { // if plugin folder doesnt exist or manifest.json doesn't exist, create it and save the plugin files - await this.plugin.app.vault.adapter.mkdir(pluginTargetFolderPath); + await adapter.mkdir(pluginTargetFolderPath); } - await this.plugin.app.vault.adapter.write(pluginTargetFolderPath + "main.js", relFiles.mainJs); - await this.plugin.app.vault.adapter.write(pluginTargetFolderPath + "manifest.json", relFiles.manifest); - if (relFiles.styles) await this.plugin.app.vault.adapter.write(pluginTargetFolderPath + "styles.css", relFiles.styles); + await adapter.write(pluginTargetFolderPath + "main.js", relFiles.mainJs); + await adapter.write(pluginTargetFolderPath + "manifest.json", relFiles.manifest); + if (relFiles.styles) await adapter.write(pluginTargetFolderPath + "styles.css", relFiles.styles); } /** @@ -145,10 +146,8 @@ export default class BetaPlugins { await this.writeReleaseFilesToPluginFolder(primaryManifest.id, releaseFiles); await addBetaPluginToList(this.plugin, repositoryPath); //@ts-ignore - const appPlugins = this.plugin.app.plugins; - await appPlugins.loadManifests(); - await appPlugins.enablePlugin(primaryManifest.id); - new Notice(`BRAT\n${repositoryPath}\nThe plugin has been installed.`, noticeTimeout); + await this.plugin.app.plugins.loadManifests(); + new Notice(`BRAT\n${repositoryPath}\nThe plugin has been registered with BRAT. You may still need to enable it the Community Plugin List.`, noticeTimeout); } else { // test if the plugin needs to be updated const pluginTargetFolderPath = this.plugin.app.vault.configDir + "/plugins/" + primaryManifest.id + "/"; @@ -172,8 +171,11 @@ export default class BetaPlugins { new Notice(`BRAT\nThere is an update available for ${primaryManifest.id}`); } else { await this.writeReleaseFilesToPluginFolder(primaryManifest.id, releaseFiles); - await this.reloadPlugin(primaryManifest.id) - new Notice(`BRAT\n${primaryManifest.id}\nplugin has been updated and reloaded`, noticeTimeout); + //@ts-ignore + await this.plugin.app.plugins.loadManifests(); + //@ts-ignore + if(!this.plugin.app.plugins.plugins[primaryManifest.id]===undefined) await this.reloadPlugin(primaryManifest.id); //reload if enabled + new Notice(`BRAT\n${primaryManifest.id}\nPlugin has been updated.`, noticeTimeout); } } else if (reportIfNotUpdted) new Notice(`BRAT\nNo update available for ${repositoryPath}`, 3000); diff --git a/src/githubUtils.ts b/src/githubUtils.ts index 0d7e0a3..67644d6 100644 --- a/src/githubUtils.ts +++ b/src/githubUtils.ts @@ -39,3 +39,14 @@ export const grabManifestJsonFromRepository = async (repositoryPath: string, roo console.log("error in grabManifestJsonFromRepository", error) } } + + +export const grabCommmunityPluginList = async(): Promise => { + const pluginListURL = `https://raw.githubusercontent.com/obsidianmd/obsidian-releases/HEAD/community-plugins.json`; + try { + const response = await request({ url: pluginListURL }); + return (response === "404: Not Found" ? null : await JSON.parse(response)); + } catch (error) { + console.log("error in grabCommmunityPluginList", error) + } +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index adecc32..2654448 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { SettingsTab } from "./SettingsTab"; import { Settings, DEFAULT_SETTINGS } from "./settings"; import BetaPlugins from "./BetaPlugins"; import { GenericFuzzySuggester, SuggesterItem } from "./GenericFuzzySuggester"; +import { grabCommmunityPluginList } from "./githubUtils"; export default class ThePlugin extends Plugin { appName = "Obsidian42 - Beta Reviewer's Auto-update Tool (BRAT)"; @@ -43,7 +44,7 @@ export default class ThePlugin extends Plugin { const gfs = new GenericFuzzySuggester(this); gfs.setSuggesterData(pluginList); await gfs.display(async (results) => { - new Notice(`BRAT\nChecking for updates for ${results.info}`,3000); + new Notice(`BRAT\nChecking for updates for ${results.info}`, 3000); await this.betaPlugins.updatePlugin(results.info, false, true); }); } @@ -64,6 +65,23 @@ export default class ThePlugin extends Plugin { } }); + this.addCommand({ + id: "BRAT-openGitHubRepository", + name: "Open the GitHub repository for a plugin", + callback: async () => { + let communityPlugins = await grabCommmunityPluginList(); + let communityPluginList: SuggesterItem[] = Object.values(communityPlugins).map((p) => { return { display: `${p.name} (${p.repo})`, info: p.repo } }); + const bratList: SuggesterItem[] = Object.values(this.settings.pluginList).map((p) => { return { display: "BRAT: " + p, info: p } }); + bratList.push({ display: "-------------------------- Community Plugins --------------------------", info: null }); + communityPluginList.forEach(si => bratList.push(si)); + const gfs = new GenericFuzzySuggester(this); + gfs.setSuggesterData(bratList); + await gfs.display(async (results) => { + if (results.info) window.open(`https://github.com/${results.info}`) + }); + } + }); + this.app.workspace.onLayoutReady((): void => { if (this.settings.updateAtStartup) // let obsidian load and calm down before check setTimeout(async () => { await this.betaPlugins.checkForUpdatesAndInstallUpdates(false) }, 60000);