Content refresh

This commit is contained in:
Jiiks 2018-01-31 00:21:06 +02:00
parent a19570c205
commit 1896a0d558
5 changed files with 84 additions and 9 deletions

View File

@ -14,6 +14,7 @@ import { Events, CssEditor, Globals, PluginManager, ThemeManager } from 'modules
class BetterDiscord { class BetterDiscord {
constructor() { constructor() {
window.pom = PluginManager;
DOM.injectStyle(BdCss, 'bdmain'); DOM.injectStyle(BdCss, 'bdmain');
Events.on('global-ready', this.globalReady.bind(this)); Events.on('global-ready', this.globalReady.bind(this));
} }

View File

@ -24,7 +24,6 @@ export default class {
static async loadAllContent() { static async loadAllContent() {
try { try {
await FileUtils.ensureDirectory(this.contentPath); await FileUtils.ensureDirectory(this.contentPath);
const directories = await FileUtils.listDirectory(this.contentPath); const directories = await FileUtils.listDirectory(this.contentPath);
@ -43,6 +42,39 @@ export default class {
} }
} }
static async refreshContent() {
if (!this.localContent.length) return this.loadAllContent();
try {
await FileUtils.ensureDirectory(this.contentPath);
const directories = await FileUtils.listDirectory(this.contentPath);
for (let dir of directories) {
// If content is already loaded this should resolve.
if (this.getContentByDirName(dir)) continue;
try {
// Load if not
await this.preloadContent(dir);
} catch (err) {
//We don't want every plugin/theme to fail loading when one does
Logger.err(this.moduleName, err);
}
}
for (let content of this.localContent) {
if (directories.includes(content.dirName)) continue;
//Plugin/theme was deleted manually, stop it and remove any reference
this.unloadContent(content);
}
return this.localContent;
} catch (err) {
throw err;
}
}
static async preloadContent(dirName, reload = false, index) { static async preloadContent(dirName, reload = false, index) {
try { try {
const contentPath = path.join(this.contentPath, dirName); const contentPath = path.join(this.contentPath, dirName);
@ -102,4 +134,21 @@ export default class {
return FileUtils.readJsonFromFile(configPath); return FileUtils.readJsonFromFile(configPath);
} }
//TODO make this nicer
static findContent(wild) {
let content = this.getContentByName(wild);
if (content) return content;
content = this.getContentById(wild);
if (content) return content;
content = this.getContentByPath(wild);
if (content) return content;
return this.getContentByDirName(wild);
}
static getContentIndex(content) { return this.localContent.findIndex(c => c === content) }
static getContentByName(name) { return this.localContent.find(c => c.name === name) }
static getContentById(id) { return this.localContent.find(c => c.id === id) }
static getContentByPath(path) { return this.localContent.find(c => c.contentPath === path) }
static getContentByDirName(dirName) { return this.localContent.find(c => c.dirName === dirName) }
} }

View File

@ -26,10 +26,10 @@ export default class {
get name() { return this.info.name } get name() { return this.info.name }
get authors() { return this.info.authors } get authors() { return this.info.authors }
get version() { return this.info.version } get version() { return this.info.version }
get pluginPath() { return this.paths.pluginPath } get pluginPath() { return this.paths.contentPath }
get dirName() { return this.paths.dirName } get dirName() { return this.paths.dirName }
get enabled() { return this.userConfig.enabled } get enabled() { return this.userConfig.enabled }
get pluginConfig() { return this.userConfig.pluginConfig } get pluginConfig() { return this.userConfig.config }
start() { start() {
if (this.onStart) { if (this.onStart) {

View File

@ -25,17 +25,38 @@ export default class extends ContentManager {
return 'plugins'; return 'plugins';
} }
static get loadAllPlugins() { static get loadAllPlugins() { return this.loadAllContent }
return this.loadAllContent; static get refreshPlugins() { return this.refreshContent }
}
static get loadContent() { return this.loadPlugin } static get loadContent() { return this.loadPlugin }
static async loadPlugin(paths, configs, info, main) { static async loadPlugin(paths, configs, info, main) {
const plugin = window.require(paths.mainPath)(Plugin, {}, {}); const plugin = window.require(paths.mainPath)(Plugin, {}, {});
const instance = new plugin({ configs, info, main, paths: { pluginPath: paths.contentPath, dirName: paths.dirName } }); const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName } });
if (instance.enabled) instance.start(); if (instance.enabled) instance.start();
return instance; return instance;
} }
static get unloadContent() { return this.unloadPlugin }
static async unloadPlugin(plugin) {
try {
if (plugin.enabled) plugin.stop();
const { pluginPath } = plugin;
const index = this.getPluginIndex(plugin);
delete window.require.cache[window.require.resolve(pluginPath)];
this.localPlugins.splice(index, 1);
} catch (err) {
//This might fail but we don't have any other option at this point
Logger.err('PluginManager', err);
}
}
static get findPlugin() { return this.findContent }
static get getPluginIndex() { return this.getContentIndex }
static get getPluginByName() { return this.getContentByName }
static get getPluginById() { return this.getContentById }
static get getPluginByPath() { return this.getContentByPath }
static get getPluginByDirName() { return this.getContentByDirName }
} }

View File

@ -63,8 +63,12 @@
} }
}, },
methods: { methods: {
showLocal() { }, showLocal() {
showOnline() { }, this.local = true;
},
showOnline() {
this.local = false;
},
refreshLocal() { }, refreshLocal() { },
togglePlugin() { }, togglePlugin() { },
reloadPlugin() { }, reloadPlugin() { },