From 952b661c2e355d608b31c527928a7d52d7ad89e1 Mon Sep 17 00:00:00 2001 From: TfTHacker Date: Mon, 4 Dec 2023 14:09:59 +0100 Subject: [PATCH] Refactor code and update settings --- CHANGELOG.md | 5 +++-- src/features/BetaPlugins.ts | 42 +++++++++++++++++++++++-------------- src/features/themes.ts | 10 +++++---- src/main.ts | 6 +++--- src/settings.ts | 8 +++---- src/ui/PluginCommands.ts | 12 +++++------ src/utils/logging.ts | 19 ++++++++--------- 7 files changed, 56 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 293b9cb..fc51df5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,11 @@ ### New -- In order to better conform with Obsidian's naming policies for settings screen, Obsidian42-BRAT is now just know as BRAT. -- Major code refactoring - the goal was to make this strongly typed according to Typescript rules. Additionally applied a new protocal to the formatting of the code. The result is extensive changes in all files. +- In order to better conform with Obsidian's naming policies for settings screen, Obsidian42-BRAT is now just known as BRAT in the Settings Tab. - In settings, when a plugin or theme are listed, they are now links to their github repositories. Small addition, but very nice to quickly jump to a repo for plugins or themes being tested. Addresses FR #67 - Removed the Ribbon icon toggle from settings, as this is now controlled natively by Obsidian since v1.1.0 +- **Major** code refactoring - the goal was to make this strongly typed according to Typescript rules. Additionally applied a new protocal to the formatting of the code. The result is extensive changes in all files. While this won't mean a lot to users, it will make the code easier to maintain, as well as making the code easier to understand for others. +- chore: update all dependencies. # 0.7.1 diff --git a/src/features/BetaPlugins.ts b/src/features/BetaPlugins.ts index 75a1aeb..4334805 100644 --- a/src/features/BetaPlugins.ts +++ b/src/features/BetaPlugins.ts @@ -199,6 +199,16 @@ export default class BetaPlugins { specifyVersion = '', forceReinstall = false ): Promise { + console.log( + 'BRAT: addPlugin', + repositoryPath, + updatePluginFiles, + seeIfUpdatedOnly, + reportIfNotUpdted, + specifyVersion, + forceReinstall + ); + const noticeTimeout = 10; // attempt to get manifest-beta.json let primaryManifest = await this.validateRepository(repositoryPath, true, false); @@ -209,7 +219,7 @@ export default class BetaPlugins { if (primaryManifest === null) { const msg = `${repositoryPath}\nA manifest.json or manifest-beta.json file does not exist in the root directory of the repository. This plugin cannot be installed.`; - this.plugin.log(msg, true); + await this.plugin.log(msg, true); toastMessage(this.plugin, `${msg}`, noticeTimeout); return false; } @@ -218,7 +228,7 @@ export default class BetaPlugins { const msg = `${repositoryPath}\nThe manifest${ usingBetaManifest ? '-beta' : '' }.json file in the root directory of the repository does not have a version number in the file. This plugin cannot be installed.`; - this.plugin.log(msg, true); + await this.plugin.log(msg, true); toastMessage(this.plugin, `${msg}`, noticeTimeout); return false; } @@ -234,15 +244,15 @@ export default class BetaPlugins { `version of the app needs to be ${primaryManifest.minAppVersion}, ` + `but this installation of Obsidian is ${apiVersion}. \n\nYou will need to update your ` + `Obsidian to use this plugin or contact the plugin developer for more information.`; - this.plugin.log(msg, true); + await this.plugin.log(msg, true); toastMessage(this.plugin, `${msg}`, 30); return false; } } - type ErrnoType = { + interface ErrnoType { errno: number; - }; + } const getRelease = async () => { const rFiles = await this.getAllReleaseFiles( @@ -258,7 +268,7 @@ export default class BetaPlugins { if (usingBetaManifest || rFiles.mainJs === null) { const msg = `${repositoryPath}\nThe release is not complete and cannot be download. main.js is missing from the Release`; - this.plugin.log(msg, true); + await this.plugin.log(msg, true); toastMessage(this.plugin, `${msg}`, noticeTimeout); return null; } @@ -276,7 +286,7 @@ export default class BetaPlugins { if (forceReinstall) { // reload if enabled await this.reloadPlugin(primaryManifest.id); - this.plugin.log(`${repositoryPath} reinstalled`, true); + await this.plugin.log(`${repositoryPath} reinstalled`, true); toastMessage( this.plugin, `${repositoryPath}\nPlugin has been reinstalled and reloaded.`, @@ -285,7 +295,7 @@ export default class BetaPlugins { } else { const versionText = specifyVersion === '' ? '' : ` (version: ${specifyVersion})`; const msg = `${repositoryPath}${versionText}\nThe plugin has been registered with BRAT. You may still need to enable it the Community Plugin List.`; - this.plugin.log(msg, true); + await this.plugin.log(msg, true); toastMessage(this.plugin, msg, noticeTimeout); } } else { @@ -344,10 +354,10 @@ export default class BetaPlugins { if (seeIfUpdatedOnly) { // dont update, just report it const msg = `There is an update available for ${primaryManifest.id} from version ${localManifestJson.version} to ${primaryManifest.version}. `; - this.plugin.log( + await this.plugin.log( msg + `[Release Info](https://github.com/${repositoryPath}/releases/tag/${primaryManifest.version})`, - false + true ); toastMessage(this.plugin, msg, 30, () => { if (primaryManifest) { @@ -361,10 +371,10 @@ export default class BetaPlugins { await this.plugin.app.plugins.loadManifests(); await this.reloadPlugin(primaryManifest.id); const msg = `${primaryManifest.id}\nPlugin has been updated from version ${localManifestJson.version} to ${primaryManifest.version}. `; - this.plugin.log( + await this.plugin.log( msg + `[Release Info](https://github.com/${repositoryPath}/releases/tag/${primaryManifest.version})`, - false + true ); toastMessage(this.plugin, msg, 30, () => { if (primaryManifest) { @@ -429,7 +439,7 @@ export default class BetaPlugins { * @param showInfo - should this with a started/completed message - useful when ran from CP * */ - async checkForUpdatesAndInstallUpdates( + async checkForPluginUpdatesAndInstallUpdates( showInfo = false, onlyCheckDontUpdate = false ): Promise { @@ -439,7 +449,7 @@ export default class BetaPlugins { } let newNotice: Notice | undefined; const msg1 = `Checking for plugin updates STARTED`; - this.plugin.log(msg1, true); + await this.plugin.log(msg1, true); if (showInfo && this.plugin.settings.notificationsEnabled) newNotice = new Notice(`BRAT\n${msg1}`, 30000); const pluginSubListFrozenVersionNames = new Set( @@ -452,7 +462,7 @@ export default class BetaPlugins { await this.updatePlugin(bp, onlyCheckDontUpdate); } const msg2 = `Checking for plugin updates COMPLETED`; - this.plugin.log(msg2, true); + await this.plugin.log(msg2, true); if (showInfo) { if (newNotice) { newNotice.hide(); @@ -469,7 +479,7 @@ export default class BetaPlugins { */ deletePlugin(repositoryPath: string): void { const msg = `Removed ${repositoryPath} from BRAT plugin list`; - this.plugin.log(msg, true); + void this.plugin.log(msg, true); this.plugin.settings.pluginList = this.plugin.settings.pluginList.filter( (b) => b !== repositoryPath ); diff --git a/src/features/themes.ts b/src/features/themes.ts index 8f3af8a..a4b0067 100644 --- a/src/features/themes.ts +++ b/src/features/themes.ts @@ -91,7 +91,7 @@ export const themeSave = async ( msg = `${manifestInfo.name} theme updated from ${cssGithubRepository}.`; } - plugin.log(msg + `[Theme Info](https://github.com/${cssGithubRepository})`, false); + void plugin.log(msg + `[Theme Info](https://github.com/${cssGithubRepository})`, false); toastMessage(plugin, `${msg}`, 20, (): void => { window.open(`https://github.com/${cssGithubRepository}`); }); @@ -115,7 +115,7 @@ export const themesCheckAndUpdates = async ( } let newNotice: Notice | undefined; const msg1 = `Checking for beta theme updates STARTED`; - plugin.log(msg1, true); + await plugin.log(msg1, true); if (showInfo && plugin.settings.notificationsEnabled) newNotice = new Notice(`BRAT\n${msg1}`, 30000); for (const t of plugin.settings.themesList) { @@ -136,7 +136,9 @@ export const themesCheckAndUpdates = async ( if (lastUpdateOnline !== t.lastUpdate) await themeSave(plugin, t.repo, false); } const msg2 = `Checking for beta theme updates COMPLETED`; - plugin.log(msg2, true); + (async (): Promise => { + await plugin.log(msg2, true); + })(); if (showInfo) { if (plugin.settings.notificationsEnabled && newNotice) newNotice.hide(); toastMessage(plugin, msg2); @@ -156,7 +158,7 @@ export const themeDelete = (plugin: ThePlugin, cssGithubRepository: string): voi ); void plugin.saveSettings(); const msg = `Removed ${cssGithubRepository} from BRAT themes list and will no longer be updated. However, the theme files still exist in the vault. To remove them, go into Settings > Appearance and remove the theme.`; - plugin.log(msg, true); + void plugin.log(msg, true); toastMessage(plugin, `${msg}`); }; diff --git a/src/main.ts b/src/main.ts index 00a98cc..65c306e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,7 +30,7 @@ export default class ThePlugin extends Plugin { // let obsidian load and calm down before checking for updates if (this.settings.updateAtStartup) { setTimeout(() => { - void this.betaPlugins.checkForUpdatesAndInstallUpdates(false); + void this.betaPlugins.checkForPluginUpdatesAndInstallUpdates(false); }, 60000); } if (this.settings.updateThemesAtStartup) { @@ -49,8 +49,8 @@ export default class ThePlugin extends Plugin { }); } - log(textToLog: string, verbose = false): void { - logger(this, textToLog, verbose); + async log(textToLog: string, verbose = false): Promise { + await logger(this, textToLog, verbose); } onunload(): void { diff --git a/src/settings.ts b/src/settings.ts index 259f29c..777a901 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -18,7 +18,6 @@ export interface Settings { themesList: ThemeInforamtion[]; updateAtStartup: boolean; updateThemesAtStartup: boolean; - ribbonIconEnabled: boolean; loggingEnabled: boolean; loggingPath: string; loggingVerboseEnabled: boolean; @@ -30,13 +29,12 @@ export const DEFAULT_SETTINGS: Settings = { pluginList: [], pluginSubListFrozenVersion: [], themesList: [], - updateAtStartup: false, - updateThemesAtStartup: false, - ribbonIconEnabled: true, + updateAtStartup: true, + updateThemesAtStartup: true, loggingEnabled: false, loggingPath: 'BRAT-log', loggingVerboseEnabled: false, - debuggingMode: true, + debuggingMode: false, notificationsEnabled: true, }; diff --git a/src/ui/PluginCommands.ts b/src/ui/PluginCommands.ts index 3a1c4f5..3c08915 100644 --- a/src/ui/PluginCommands.ts +++ b/src/ui/PluginCommands.ts @@ -38,7 +38,7 @@ export default class PluginCommands { name: 'Plugins: Check for updates to all beta plugins and UPDATE', showInRibbon: true, callback: async () => { - await this.plugin.betaPlugins.checkForUpdatesAndInstallUpdates(true, false); + await this.plugin.betaPlugins.checkForPluginUpdatesAndInstallUpdates(true, false); }, }, { @@ -47,7 +47,7 @@ export default class PluginCommands { name: "Plugins: Only check for updates to beta plugins, but don't Update", showInRibbon: true, callback: async () => { - await this.plugin.betaPlugins.checkForUpdatesAndInstallUpdates(true, true); + await this.plugin.betaPlugins.checkForPluginUpdatesAndInstallUpdates(true, true); }, }, { @@ -68,7 +68,7 @@ export default class PluginCommands { gfs.setSuggesterData(pluginList); gfs.display((results) => { const msg = `Checking for updates for ${results.info as string}`; - this.plugin.log(msg, true); + void this.plugin.log(msg, true); toastMessage(this.plugin, `\n${msg}`, 3); void this.plugin.betaPlugins.updatePlugin(results.info as string, false, true); }); @@ -93,7 +93,7 @@ export default class PluginCommands { gfs.display((results) => { const msg = `Reinstalling ${results.info as string}`; toastMessage(this.plugin, `\n${msg}`, 3); - this.plugin.log(msg, true); + void this.plugin.log(msg, true); void this.plugin.betaPlugins.updatePlugin( results.info as string, false, @@ -140,7 +140,7 @@ export default class PluginCommands { const gfs = new GenericFuzzySuggester(this.plugin); gfs.setSuggesterData(pluginList); gfs.display((results) => { - this.plugin.log(`${results.display} plugin disabled`, false); + void this.plugin.log(`${results.display} plugin disabled`, false); if (this.plugin.settings.debuggingMode) console.log(results.info); void this.plugin.app.plugins.disablePluginAndSave(results.info as string); }); @@ -160,7 +160,7 @@ export default class PluginCommands { const gfs = new GenericFuzzySuggester(this.plugin); gfs.setSuggesterData(pluginList); gfs.display((results) => { - this.plugin.log(`${results.display} plugin enabled`, false); + void this.plugin.log(`${results.display} plugin enabled`, false); void this.plugin.app.plugins.enablePluginAndSave(results.info as string); }); }, diff --git a/src/utils/logging.ts b/src/utils/logging.ts index 1fcf0c8..51a892b 100644 --- a/src/utils/logging.ts +++ b/src/utils/logging.ts @@ -11,11 +11,11 @@ import { getDailyNoteSettings } from 'obsidian-daily-notes-interface'; * @param verboseLoggingOn - True if should only be logged if verbose logging is enabled * */ -export function logger( +export async function logger( plugin: ThePlugin, textToLog: string, verboseLoggingOn = false -): void { +): Promise { if (plugin.settings.debuggingMode) console.log('BRAT: ' + textToLog); if (plugin.settings.loggingEnabled) { if (!plugin.settings.loggingVerboseEnabled && verboseLoggingOn) { @@ -31,14 +31,13 @@ export function logger( const machineName = Platform.isDesktop ? os.hostname() : 'MOBILE'; let output = dateOutput + ' ' + machineName + ' ' + textToLog.replace('\n', ' ') + '\n\n'; - (async () => { - if (await plugin.app.vault.adapter.exists(fileName)) { - const fileContents = await plugin.app.vault.adapter.read(fileName); - output = output + fileContents; - const file = plugin.app.vault.getAbstractFileByPath(fileName) as TFile; - void plugin.app.vault.modify(file, output); - } else void plugin.app.vault.create(fileName, output); - })(); + + if (await plugin.app.vault.adapter.exists(fileName)) { + const fileContents = await plugin.app.vault.adapter.read(fileName); + output = output + fileContents; + const file = plugin.app.vault.getAbstractFileByPath(fileName) as TFile; + await plugin.app.vault.modify(file, output); + } else await plugin.app.vault.create(fileName, output); } } }