Skip to content

Commit

Permalink
Refactor code and update settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TfTHacker committed Dec 4, 2023
1 parent c4c9fd7 commit 952b661
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 46 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 26 additions & 16 deletions src/features/BetaPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ export default class BetaPlugins {
specifyVersion = '',
forceReinstall = false
): Promise<boolean> {
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);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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(
Expand All @@ -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;
}
Expand All @@ -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.`,
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<void> {
Expand All @@ -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(
Expand All @@ -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();
Expand All @@ -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
);
Expand Down
10 changes: 6 additions & 4 deletions src/features/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
Expand All @@ -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) {
Expand All @@ -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<void> => {
await plugin.log(msg2, true);
})();
if (showInfo) {
if (plugin.settings.notificationsEnabled && newNotice) newNotice.hide();
toastMessage(plugin, msg2);
Expand All @@ -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}`);
};

Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<void> {
await logger(this, textToLog, verbose);
}

onunload(): void {
Expand Down
8 changes: 3 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface Settings {
themesList: ThemeInforamtion[];
updateAtStartup: boolean;
updateThemesAtStartup: boolean;
ribbonIconEnabled: boolean;
loggingEnabled: boolean;
loggingPath: string;
loggingVerboseEnabled: boolean;
Expand All @@ -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,
};

Expand Down
12 changes: 6 additions & 6 deletions src/ui/PluginCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
},
{
Expand All @@ -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);
},
},
{
Expand All @@ -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);
});
Expand All @@ -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,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
},
Expand Down
19 changes: 9 additions & 10 deletions src/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
if (plugin.settings.debuggingMode) console.log('BRAT: ' + textToLog);
if (plugin.settings.loggingEnabled) {
if (!plugin.settings.loggingVerboseEnabled && verboseLoggingOn) {
Expand All @@ -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);
}
}
}

0 comments on commit 952b661

Please sign in to comment.