Cleanup managers
This commit is contained in:
parent
fd5afa06cf
commit
c19259c776
|
@ -174,24 +174,22 @@ export default class AddonManager {
|
|||
}
|
||||
|
||||
requireAddon(filename) {
|
||||
const module = {filename, exports: {}};
|
||||
let fileContent = fs.readFileSync(filename, "utf8");
|
||||
fileContent = stripBOM(fileContent);
|
||||
const stats = fs.statSync(filename);
|
||||
const meta = this.extractMeta(fileContent);
|
||||
if (!meta.author) meta.author = Strings.Addons.unknownAuthor;
|
||||
if (!meta.version) meta.version = "???";
|
||||
if (!meta.description) meta.description = Strings.Addons.noDescription;
|
||||
// if (!meta.name || !meta.author || !meta.description || !meta.version) return new AddonError(meta.name || path.basename(filename), filename, "Addon is missing name, author, description, or version", {message: "Addon must provide name, author, description, and version.", stack: ""}, this.prefix);
|
||||
meta.id = meta.name;
|
||||
meta.slug = path.basename(filename).replace(this.extension, "").replace(/ /g, "-");
|
||||
meta.filename = path.basename(filename);
|
||||
meta.added = stats.atimeMs;
|
||||
meta.modified = stats.mtimeMs;
|
||||
meta.size = stats.size;
|
||||
const error = this.finalizeRequire(module, fileContent, meta);
|
||||
if (error) return error;
|
||||
return module.exports;
|
||||
const addon = this.extractMeta(fileContent);
|
||||
if (!addon.author) addon.author = Strings.Addons.unknownAuthor;
|
||||
if (!addon.version) addon.version = "???";
|
||||
if (!addon.description) addon.description = Strings.Addons.noDescription;
|
||||
// if (!addon.name || !addon.author || !addon.description || !addon.version) return new AddonError(addon.name || path.basename(filename), filename, "Addon is missing name, author, description, or version", {message: "Addon must provide name, author, description, and version.", stack: ""}, this.prefix);
|
||||
addon.id = addon.name;
|
||||
addon.slug = path.basename(filename).replace(this.extension, "").replace(/ /g, "-");
|
||||
addon.filename = path.basename(filename);
|
||||
addon.added = stats.atimeMs;
|
||||
addon.modified = stats.mtimeMs;
|
||||
addon.size = stats.size;
|
||||
addon.fileContent = fileContent;
|
||||
return addon;
|
||||
}
|
||||
|
||||
// Subclasses should use the return (if not AddonError) and push to this.addonList
|
||||
|
|
|
@ -13,7 +13,6 @@ import SettingsRenderer from "../ui/settings";
|
|||
const path = require("path");
|
||||
const vm = require("vm");
|
||||
|
||||
|
||||
const normalizeExports = name => `
|
||||
if (module.exports.default) {
|
||||
module.exports = module.exports.default;
|
||||
|
@ -32,7 +31,6 @@ export default new class PluginManager extends AddonManager {
|
|||
|
||||
constructor() {
|
||||
super();
|
||||
this.promises = {};
|
||||
this.onSwitch = this.onSwitch.bind(this);
|
||||
this.observer = new MutationObserver((mutations) => {
|
||||
for (let i = 0, mlen = mutations.length; i < mlen; i++) {
|
||||
|
@ -114,19 +112,22 @@ export default new class PluginManager extends AddonManager {
|
|||
}
|
||||
}
|
||||
|
||||
finalizeRequire(module, fileContent, meta) {
|
||||
requireAddon(filename) {
|
||||
const addon = super.requireAddon(filename);
|
||||
try {
|
||||
const module = {filename, exports: {}};
|
||||
// Test if the code is valid gracefully
|
||||
vm.compileFunction(fileContent, ["require", "module", "exports", "__filename", "__dirname"]);
|
||||
fileContent += normalizeExports(meta.exports || meta.name);
|
||||
fileContent += `\n//# sourceURL=betterdiscord://plugins/${path.basename(module.filename)}`;
|
||||
const wrappedPlugin = new Function(["require", "module", "exports", "__filename", "__dirname"], fileContent); // eslint-disable-line no-new-func
|
||||
vm.compileFunction(addon.fileContent, ["require", "module", "exports", "__filename", "__dirname"]);
|
||||
addon.fileContent += normalizeExports(addon.exports || addon.name);
|
||||
addon.fileContent += `\n//# sourceURL=betterdiscord://plugins/${addon.filename}`;
|
||||
const wrappedPlugin = new Function(["require", "module", "exports", "__filename", "__dirname"], addon.fileContent); // eslint-disable-line no-new-func
|
||||
wrappedPlugin(window.require, module, module.exports, module.filename, this.addonFolder);
|
||||
meta.exports = module.exports;
|
||||
module.exports = meta;
|
||||
addon.exports = module.exports;
|
||||
delete addon.fileContent;
|
||||
return addon;
|
||||
}
|
||||
catch (err) {
|
||||
return new AddonError(meta.name || path.basename(module.filename), module.filename, "Plugin could not be compiled", {message: err.message, stack: err.stack}, this.prefix);
|
||||
return new AddonError(addon.name || addon.filename, module.filename, "Plugin could not be compiled", {message: err.message, stack: err.stack}, this.prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,9 +176,7 @@ export default new class PluginManager extends AddonManager {
|
|||
}
|
||||
|
||||
setupFunctions() {
|
||||
// electronRemote.getCurrentWebContents().on("did-navigate-in-page", this.onSwitch.bind(this));
|
||||
Events.on("navigate", this.onSwitch);
|
||||
// ipc.on(IPCEvents.NAVIGATE, this.onSwitch);
|
||||
this.observer.observe(document, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
|
|
|
@ -58,10 +58,12 @@ export default new class ThemeManager extends AddonManager {
|
|||
if (!addon.name || !addon.author || !addon.description || !addon.version) return new AddonError(addon.name || addon.filename, addon.filename, "Addon is missing name, author, description, or version", {message: "Addon must provide name, author, description, and version.", stack: ""}, this.prefix);
|
||||
}
|
||||
|
||||
finalizeRequire(module, fileContent, meta) {
|
||||
meta.css = fileContent;
|
||||
if (meta.format == "json") meta.css = meta.css.split("\n").slice(1).join("\n");
|
||||
module.exports = meta;
|
||||
requireAddon(filename) {
|
||||
const addon = super.requireAddon(filename);
|
||||
addon.css = addon.fileContent;
|
||||
delete addon.fileContent;
|
||||
if (addon.format == "json") addon.css = addon.css.split("\n").slice(1).join("\n");
|
||||
return addon;
|
||||
}
|
||||
|
||||
startAddon(id) {return this.addTheme(id);}
|
||||
|
|
Loading…
Reference in New Issue