initial rewrite of contentmanager

This commit is contained in:
Zack Rauen 2019-06-06 23:52:08 -04:00
parent 632d37f93e
commit 1b84d577db
8 changed files with 239 additions and 320 deletions

View File

@ -1,30 +1,13 @@
const gulp = require("gulp");
const rename = require("gulp-rename");
const minify = require("gulp-babel-minify");
const csso = require("gulp-csso");
gulp.task("minify-js", minifyJS);
gulp.task("minify-css", minifyCSS);
gulp.task("watch-js", function() {
return gulp.watch(["./js/main.js"], minifyJS);
});
gulp.task("watch-css", function() {
return gulp.watch(["./css/main.css"], minifyCSS);
});
gulp.task("watch", function() {
return gulp.watch(["./js/main.js", "./css/main.css"], gulp.series(minifyJS, minifyCSS));
});
function minifyJS() {
return gulp.src("./js/main.js")
.pipe(minify({mangle: {keepClassName: true}}))
.pipe(rename("main.min.js"))
.pipe(gulp.dest("./js"));
}
function minifyCSS() {
return gulp.src("./css/main.css")
.pipe(csso({restructure: false}))

View File

@ -3080,20 +3080,37 @@ const releaseChannel = DiscordNative.globals.releaseChannel; // Schema 1
/* harmony default export */ __webpack_exports__["default"] = (new class DataStore {
constructor() {
this.data = {};
this.data = {
misc: {}
};
this.pluginData = {};
}
initialize() {
if (!fs.existsSync(path.resolve(this.BDFile, ".."))) fs.mkdirSync(path.resolve(this.BDFile, ".."));
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
this.data = require(this.BDFile); // if (data.hasOwnProperty("settings")) this.data = data;
if (!fs.existsSync(this.baseFolder)) fs.mkdirSync(this.baseFolder);
if (!fs.existsSync(this.dataFolder)) fs.mkdirSync(this.dataFolder);
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data.misc, null, 4));
const dataFiles = fs.readdirSync(this.dataFolder).filter(f => !fs.statSync(path.resolve(this.dataFolder, f)).isDirectory() && f.endsWith(".json"));
for (const file of dataFiles) {
this.data[file.split(".")[0]] = require(path.resolve(this.dataFolder, file));
} // this.data = __non_webpack_require__(this.BDFile);
// if (data.hasOwnProperty("settings")) this.data = data;
// if (!fs.existsSync(this.settingsFile)) return;
// let settings = __non_webpack_require__(this.settingsFile);
// fs.unlinkSync(this.settingsFile);
// if (settings.hasOwnProperty("settings")) settings = Object.assign({stable: {}, canary: {}, ptb: {}}, {[releaseChannel]: settings});
// else settings = Object.assign({stable: {}, canary: {}, ptb: {}}, settings);
// this.setBDData("settings", settings);
}
get baseFolder() {
return this._baseFolder || (this._baseFolder = path.resolve(data__WEBPACK_IMPORTED_MODULE_0__["Config"].dataPath, "data"));
}
get dataFolder() {
return this._dataFolder || (this._dataFolder = path.resolve(this.baseFolder, `${releaseChannel}`));
}
get BDFile() {
@ -3112,22 +3129,28 @@ const releaseChannel = DiscordNative.globals.releaseChannel; // Schema 1
// }
_getFile(key) {
if (key == "settings" || key == "plugins" || key == "themes") return path.resolve(this.dataFolder, `${key}.json`);
return path.resolve(this.dataFolder, `misc.json`);
}
getBDData(key) {
return this.data[key] || "";
return this.data.misc[key] || "";
}
setBDData(key, value) {
this.data[key] = value;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
this.data.misc[key] = value;
fs.writeFileSync(path.resolve(this.dataFolder, `misc.json`), JSON.stringify(this.data.misc, null, 4));
}
getData(key) {
return this.data[key] || "";
return this.data[key] || ""; // return JSON.parse(fs.readFileSync(path.resolve(this.dataFolder, `${file}.json`)));
}
setData(key, value) {
this.data[key] = value;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
this.data[key] = value; // fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
fs.writeFileSync(path.resolve(this.dataFolder, `${key}.json`), JSON.stringify(value, null, 4));
}
getPluginData(pluginName, key) {
@ -3814,7 +3837,20 @@ PluginModule.prototype.rawObserver = function (e) {
}
};
/* harmony default export */ __webpack_exports__["default"] = (new PluginModule());
/* harmony default export */ __webpack_exports__["default"] = (new PluginModule()); // makePlaceholderPlugin(data) {
// return {plugin: {
// start: () => {},
// getName: () => {return data.name || data.filename;},
// getAuthor: () => {return "???";},
// getDescription: () => {return data.message ? data.message : "This plugin was unable to be loaded. Check the author's page for updates.";},
// getVersion: () => {return "???";}
// },
// name: data.name || data.filename,
// filename: data.filename,
// source: data.source ? data.source : "",
// website: data.website ? data.website : ""
// };
// }
/***/ }),
@ -4028,7 +4064,6 @@ __webpack_require__.r(__webpack_exports__);
onSettingChange(collection, category, id, value) {
const before = this.config.filter(c => c.disabled).length;
this.state[collection][category][id] = value;
console.log(this.state);
_emitter__WEBPACK_IMPORTED_MODULE_4__["default"].dispatch("setting-updated", collection, category, id, value);
const after = this.config.filter(c => c.disabled).length;
this.saveSettings();

2
js/main.min.js vendored

File diff suppressed because one or more lines are too long

274
package-lock.json generated
View File

@ -1381,48 +1381,6 @@
"integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==",
"dev": true
},
"babel-helper-evaluate-path": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-helper-evaluate-path/-/babel-helper-evaluate-path-0.5.0.tgz",
"integrity": "sha512-mUh0UhS607bGh5wUMAQfOpt2JX2ThXMtppHRdRU1kL7ZLRWIXxoV2UIV1r2cAeeNeU1M5SB5/RSUgUxrK8yOkA==",
"dev": true
},
"babel-helper-flip-expressions": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-helper-flip-expressions/-/babel-helper-flip-expressions-0.4.3.tgz",
"integrity": "sha1-NpZzahKKwYvCUlS19AoizrPB0/0=",
"dev": true
},
"babel-helper-is-nodes-equiv": {
"version": "0.0.1",
"resolved": "http://registry.npmjs.org/babel-helper-is-nodes-equiv/-/babel-helper-is-nodes-equiv-0.0.1.tgz",
"integrity": "sha1-NOmzALFHnd2Y7HfqC76TQt/jloQ=",
"dev": true
},
"babel-helper-is-void-0": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-helper-is-void-0/-/babel-helper-is-void-0-0.4.3.tgz",
"integrity": "sha1-fZwBtFYee5Xb2g9u7kj1tg5nMT4=",
"dev": true
},
"babel-helper-mark-eval-scopes": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-helper-mark-eval-scopes/-/babel-helper-mark-eval-scopes-0.4.3.tgz",
"integrity": "sha1-0kSjvvmESHJgP/tG4izorN9VFWI=",
"dev": true
},
"babel-helper-remove-or-void": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-helper-remove-or-void/-/babel-helper-remove-or-void-0.4.3.tgz",
"integrity": "sha1-pPA7QAd6D/6I5F0HAQ3uJB/1rmA=",
"dev": true
},
"babel-helper-to-multiple-sequence-expressions": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-helper-to-multiple-sequence-expressions/-/babel-helper-to-multiple-sequence-expressions-0.5.0.tgz",
"integrity": "sha512-m2CvfDW4+1qfDdsrtf4dwOslQC3yhbgyBFptncp4wvtdrDHqueW7slsYv4gArie056phvQFhT2nRcGS4bnm6mA==",
"dev": true
},
"babel-loader": {
"version": "8.0.6",
"resolved": "https://registry.npmjs.org/babel-loader/-/babel-loader-8.0.6.tgz",
@ -1435,201 +1393,6 @@
"pify": "^4.0.1"
}
},
"babel-plugin-minify-builtins": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-builtins/-/babel-plugin-minify-builtins-0.5.0.tgz",
"integrity": "sha512-wpqbN7Ov5hsNwGdzuzvFcjgRlzbIeVv1gMIlICbPj0xkexnfoIDe7q+AZHMkQmAE/F9R5jkrB6TLfTegImlXag==",
"dev": true
},
"babel-plugin-minify-constant-folding": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-constant-folding/-/babel-plugin-minify-constant-folding-0.5.0.tgz",
"integrity": "sha512-Vj97CTn/lE9hR1D+jKUeHfNy+m1baNiJ1wJvoGyOBUx7F7kJqDZxr9nCHjO/Ad+irbR3HzR6jABpSSA29QsrXQ==",
"dev": true,
"requires": {
"babel-helper-evaluate-path": "^0.5.0"
}
},
"babel-plugin-minify-dead-code-elimination": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-dead-code-elimination/-/babel-plugin-minify-dead-code-elimination-0.5.0.tgz",
"integrity": "sha512-XQteBGXlgEoAKc/BhO6oafUdT4LBa7ARi55mxoyhLHNuA+RlzRmeMAfc31pb/UqU01wBzRc36YqHQzopnkd/6Q==",
"dev": true,
"requires": {
"babel-helper-evaluate-path": "^0.5.0",
"babel-helper-mark-eval-scopes": "^0.4.3",
"babel-helper-remove-or-void": "^0.4.3",
"lodash.some": "^4.6.0"
}
},
"babel-plugin-minify-flip-comparisons": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-flip-comparisons/-/babel-plugin-minify-flip-comparisons-0.4.3.tgz",
"integrity": "sha1-AMqHDLjxO0XAOLPB68DyJyk8llo=",
"dev": true,
"requires": {
"babel-helper-is-void-0": "^0.4.3"
}
},
"babel-plugin-minify-guarded-expressions": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-guarded-expressions/-/babel-plugin-minify-guarded-expressions-0.4.3.tgz",
"integrity": "sha1-zHCbRFP9IbHzAod0RMifiEJ845c=",
"dev": true,
"requires": {
"babel-helper-flip-expressions": "^0.4.3"
}
},
"babel-plugin-minify-infinity": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-infinity/-/babel-plugin-minify-infinity-0.4.3.tgz",
"integrity": "sha1-37h2obCKBldjhO8/kuZTumB7Oco=",
"dev": true
},
"babel-plugin-minify-mangle-names": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-mangle-names/-/babel-plugin-minify-mangle-names-0.5.0.tgz",
"integrity": "sha512-3jdNv6hCAw6fsX1p2wBGPfWuK69sfOjfd3zjUXkbq8McbohWy23tpXfy5RnToYWggvqzuMOwlId1PhyHOfgnGw==",
"dev": true,
"requires": {
"babel-helper-mark-eval-scopes": "^0.4.3"
}
},
"babel-plugin-minify-numeric-literals": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-numeric-literals/-/babel-plugin-minify-numeric-literals-0.4.3.tgz",
"integrity": "sha1-jk/VYcefeAEob/YOjF/Z3u6TwLw=",
"dev": true
},
"babel-plugin-minify-replace": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-replace/-/babel-plugin-minify-replace-0.5.0.tgz",
"integrity": "sha512-aXZiaqWDNUbyNNNpWs/8NyST+oU7QTpK7J9zFEFSA0eOmtUNMU3fczlTTTlnCxHmq/jYNFEmkkSG3DDBtW3Y4Q==",
"dev": true
},
"babel-plugin-minify-simplify": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-simplify/-/babel-plugin-minify-simplify-0.5.0.tgz",
"integrity": "sha512-TM01J/YcKZ8XIQd1Z3nF2AdWHoDsarjtZ5fWPDksYZNsoOjQ2UO2EWm824Ym6sp127m44gPlLFiO5KFxU8pA5Q==",
"dev": true,
"requires": {
"babel-helper-flip-expressions": "^0.4.3",
"babel-helper-is-nodes-equiv": "^0.0.1",
"babel-helper-to-multiple-sequence-expressions": "^0.5.0"
}
},
"babel-plugin-minify-type-constructors": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-minify-type-constructors/-/babel-plugin-minify-type-constructors-0.4.3.tgz",
"integrity": "sha1-G8bxW4f3qxCF1CszC3F2V6IVZQA=",
"dev": true,
"requires": {
"babel-helper-is-void-0": "^0.4.3"
}
},
"babel-plugin-transform-inline-consecutive-adds": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-inline-consecutive-adds/-/babel-plugin-transform-inline-consecutive-adds-0.4.3.tgz",
"integrity": "sha1-Mj1Ho+pjqDp6w8gRro5pQfrysNE=",
"dev": true
},
"babel-plugin-transform-member-expression-literals": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-member-expression-literals/-/babel-plugin-transform-member-expression-literals-6.9.4.tgz",
"integrity": "sha1-NwOcmgwzE6OUlfqsL/OmtbnQOL8=",
"dev": true
},
"babel-plugin-transform-merge-sibling-variables": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-merge-sibling-variables/-/babel-plugin-transform-merge-sibling-variables-6.9.4.tgz",
"integrity": "sha1-hbQi/DN3tEnJ0c3kQIcgNTJAHa4=",
"dev": true
},
"babel-plugin-transform-minify-booleans": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-minify-booleans/-/babel-plugin-transform-minify-booleans-6.9.4.tgz",
"integrity": "sha1-rLs+VqNVXdI5KOS1gtKFFi3SsZg=",
"dev": true
},
"babel-plugin-transform-property-literals": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-property-literals/-/babel-plugin-transform-property-literals-6.9.4.tgz",
"integrity": "sha1-mMHSHiVXNlc/k+zlRFn2ziSYXTk=",
"dev": true,
"requires": {
"esutils": "^2.0.2"
}
},
"babel-plugin-transform-regexp-constructors": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-regexp-constructors/-/babel-plugin-transform-regexp-constructors-0.4.3.tgz",
"integrity": "sha1-WLd3W2OvzzMyj66aX4j71PsLSWU=",
"dev": true
},
"babel-plugin-transform-remove-console": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-remove-console/-/babel-plugin-transform-remove-console-6.9.4.tgz",
"integrity": "sha1-uYA2DAZzhOJLNXpYjYB9PINSd4A=",
"dev": true
},
"babel-plugin-transform-remove-debugger": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-remove-debugger/-/babel-plugin-transform-remove-debugger-6.9.4.tgz",
"integrity": "sha1-QrcnYxyXl44estGZp67IShgznvI=",
"dev": true
},
"babel-plugin-transform-remove-undefined": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-remove-undefined/-/babel-plugin-transform-remove-undefined-0.5.0.tgz",
"integrity": "sha512-+M7fJYFaEE/M9CXa0/IRkDbiV3wRELzA1kKQFCJ4ifhrzLKn/9VCCgj9OFmYWwBd8IB48YdgPkHYtbYq+4vtHQ==",
"dev": true,
"requires": {
"babel-helper-evaluate-path": "^0.5.0"
}
},
"babel-plugin-transform-simplify-comparison-operators": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-simplify-comparison-operators/-/babel-plugin-transform-simplify-comparison-operators-6.9.4.tgz",
"integrity": "sha1-9ir+CWyrDh9ootdT/fKDiIRxzrk=",
"dev": true
},
"babel-plugin-transform-undefined-to-void": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-undefined-to-void/-/babel-plugin-transform-undefined-to-void-6.9.4.tgz",
"integrity": "sha1-viQcqBQEAwZ4t0hxcyK4nQyP4oA=",
"dev": true
},
"babel-preset-minify": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/babel-preset-minify/-/babel-preset-minify-0.5.0.tgz",
"integrity": "sha512-xj1s9Mon+RFubH569vrGCayA9Fm2GMsCgDRm1Jb8SgctOB7KFcrVc2o8K3YHUyMz+SWP8aea75BoS8YfsXXuiA==",
"dev": true,
"requires": {
"babel-plugin-minify-builtins": "^0.5.0",
"babel-plugin-minify-constant-folding": "^0.5.0",
"babel-plugin-minify-dead-code-elimination": "^0.5.0",
"babel-plugin-minify-flip-comparisons": "^0.4.3",
"babel-plugin-minify-guarded-expressions": "^0.4.3",
"babel-plugin-minify-infinity": "^0.4.3",
"babel-plugin-minify-mangle-names": "^0.5.0",
"babel-plugin-minify-numeric-literals": "^0.4.3",
"babel-plugin-minify-replace": "^0.5.0",
"babel-plugin-minify-simplify": "^0.5.0",
"babel-plugin-minify-type-constructors": "^0.4.3",
"babel-plugin-transform-inline-consecutive-adds": "^0.4.3",
"babel-plugin-transform-member-expression-literals": "^6.9.4",
"babel-plugin-transform-merge-sibling-variables": "^6.9.4",
"babel-plugin-transform-minify-booleans": "^6.9.4",
"babel-plugin-transform-property-literals": "^6.9.4",
"babel-plugin-transform-regexp-constructors": "^0.4.3",
"babel-plugin-transform-remove-console": "^6.9.4",
"babel-plugin-transform-remove-debugger": "^6.9.4",
"babel-plugin-transform-remove-undefined": "^0.5.0",
"babel-plugin-transform-simplify-comparison-operators": "^6.9.4",
"babel-plugin-transform-undefined-to-void": "^6.9.4",
"lodash.isplainobject": "^4.0.6"
}
},
"bach": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/bach/-/bach-1.2.0.tgz",
@ -3870,19 +3633,6 @@
}
}
},
"gulp-babel-minify": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/gulp-babel-minify/-/gulp-babel-minify-0.5.0.tgz",
"integrity": "sha512-6YrHnLXSizWHITQ3mcwmPbn0ygXIf8CCk7Dv345Cp10kPE9iqBRwfuU8GhG98Y5IwWGAVOWNp2vo6bLanbqCpA==",
"dev": true,
"requires": {
"@babel/core": "^7.1.0",
"babel-preset-minify": "^0.5.0",
"plugin-error": "^1.0.1",
"through2": "^2.0.3",
"vinyl-sourcemaps-apply": "^0.2.1"
}
},
"gulp-csso": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/gulp-csso/-/gulp-csso-3.0.1.tgz",
@ -4527,18 +4277,6 @@
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==",
"dev": true
},
"lodash.isplainobject": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=",
"dev": true
},
"lodash.some": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/lodash.some/-/lodash.some-4.6.0.tgz",
"integrity": "sha1-G7nzFO9ri63tE7VJFpsqlF62jk0=",
"dev": true
},
"loose-envify": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
@ -5352,18 +5090,6 @@
"find-up": "^3.0.0"
}
},
"plugin-error": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/plugin-error/-/plugin-error-1.0.1.tgz",
"integrity": "sha512-L1zP0dk7vGweZME2i+EeakvUNqSrdiI3F91TwEoYiGrAfUXmVv6fJIq4g82PAXxNsWOp0J7ZqQy/3Szz0ajTxA==",
"dev": true,
"requires": {
"ansi-colors": "^1.0.1",
"arr-diff": "^4.0.0",
"arr-union": "^3.1.0",
"extend-shallow": "^3.0.2"
}
},
"posix-character-classes": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz",

View File

@ -5,13 +5,10 @@
"main": "js/main.js",
"scripts": {
"build": "webpack --progress --colors",
"build_prod": "webpack --progress --colors --mode production -o js/main.min.js",
"watch": "webpack --progress --colors --watch",
"build-prod": "webpack --progress --colors --mode production -o js/main.min.js",
"test": "echo \"Error: no test specified\" && exit 1",
"minify": "gulp minify-js && gulp minify-css",
"minify-js": "gulp minify-js",
"minify-css": "gulp minify-css",
"watch": "gulp watch",
"watch-js": "gulp watch-js",
"minify": "gulp minify-css",
"watch-css": "gulp watch-css"
},
"repository": {
@ -31,7 +28,6 @@
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.6",
"gulp": "^4.0.0",
"gulp-babel-minify": "^0.5.0",
"gulp-csso": "^3.0.1",
"gulp-rename": "^1.4.0",
"webpack": "^4.29.6",

View File

@ -0,0 +1,149 @@
import Utilities from "./utilities";
import Settings from "./settingsmanager";
const path = require("path");
const fs = require("fs");
const Module = require("module").Module;
Module.globalPaths.push(path.resolve(require("electron").remote.app.getAppPath(), "node_modules"));
class MetaError extends Error {
constructor(message) {
super(message);
this.name = "MetaError";
}
}
class ContentError extends Error {
constructor(name, filename, message, error) {
super(message);
this.name = name;
this.file = filename;
this.error = error;
}
}
export default new class ContentManager {
get fileExtension() {return "";}
get extension() {return "";}
get contentFolder() {return "";}
get collection() {return "settings";}
get category() {return "content";}
get id() {return "autoReload";}
getContentModification(content) {return content;}
constructor() {
this.timeCache = {};
this.content = [];
this.originalRequire = Module._extensions[this.fileExtension];
Module._extensions[this.fileExtension] = this.getContentRequire();
Settings.on(this.collection, this.category, this.id, (enabled) => {
if (enabled) this.watchContent();
else this.unwatchContent();
});
if (Settings.get(this.collection, this.category, this.id)) this.watchContent();
}
watchContent() {
if (this.watcher) return;
this.watcher = fs.watch(this.contentFolder, {persistent: false}, async (eventType, filename) => {
if (!eventType || !filename || !filename.endsWith(this.extension)) return;
await new Promise(r => setTimeout(r, 50));
try {fs.statSync(path.resolve(this.contentFolder, filename));}
catch (err) {
if (err.code !== "ENOENT") return;
delete this.timeCache[filename];
this.unloadContent(filename);
}
if (!fs.statSync(path.resolve(this.contentFolder, filename)).isFile()) return;
const stats = fs.statSync(path.resolve(this.contentFolder, filename));
if (!stats || !stats.mtime || !stats.mtime.getTime()) return;
if (typeof(stats.mtime.getTime()) !== "number") return;
if (this.timeCache[filename] == stats.mtime.getTime()) return;
this.timeCache[filename] = stats.mtime.getTime();
if (eventType == "rename") this.loadContent(filename);
if (eventType == "change") this.reloadContent(filename);
});
}
unwatchContent() {
if (!this.watcher) return;
this.watcher.close();
delete this.watcher;
}
extractMeta(content) {
const meta = content.split("\n")[0];
const rawMeta = meta.substring(meta.lastIndexOf("//META") + 6, meta.lastIndexOf("*//"));
if (meta.indexOf("META") < 0) throw new MetaError("META was not found.");
if (!Utilities.testJSON(rawMeta)) throw new MetaError("META could not be parsed.");
const parsed = JSON.parse(rawMeta);
if (!parsed.name) throw new MetaError("META missing name data.");
return parsed;
}
getContentRequire() {
const self = this;
const baseFolder = this.contentFolder;
const originalRequire = this.originalRequire;
return function(module, filename) {
const possiblePath = path.resolve(baseFolder, path.basename(filename));
if (!fs.existsSync(possiblePath) || filename !== fs.realpathSync(possiblePath)) return Reflect.apply(originalRequire, this, arguments);
let content = fs.readFileSync(filename, "utf8");
content = Utilities.stripBOM(content);
const meta = self.extractMeta(content);
meta.filename = path.basename(filename);
content = self.getContentModification(content, meta);
module._compile(content, filename);
};
}
// Subclasses should use the return (if not ContentError) and push to this.contentList
loadContent(filename, type) {
if (typeof(filename) === "undefined" || typeof(type) === "undefined") return;
try {__non_webpack_require__(path.resolve(this.contentFolder, filename));}
catch (error) {return new ContentError(filename, filename, "Could not be compiled.", {message: error.message, stack: error.stack});}
return __non_webpack_require__(path.resolve(this.contentFolder, filename));
}
unloadContent(idOrFile) {
if (typeof(filename) === "undefined") return;
const content = this.content.find(c => c.id == idOrFile || c.filename == idOrFile);
if (!content) return false;
delete __non_webpack_require__.cache[__non_webpack_require__.resolve(path.resolve(this.contentFolder, content.file))];
this.content.splice(this.content.indexOf(content), 1);
return true;
}
isLoaded(idOrFile) {
const content = this.content.find(c => c.id == idOrFile || c.filename == idOrFile);
if (!content) return false;
return true;
}
reloadContent(filename, type) {
const didUnload = this.unloadContent(filename, type);
if (!didUnload) return didUnload;
return this.loadContent(filename, type);
}
loadNewContent() {
const files = fs.readdirSync(this.contentFolder);
const removed = this.content.filter(t => !files.includes(t.filename)).map(c => c.id);
const added = files.filter(f => !this.content.find(t => t.filename == f) && f.endsWith(this.extension) && fs.statSync(path.resolve(this.contentFolder, f)).isFile());
return {added, removed};
}
loadAllContent(type) {
const errors = [];
const files = fs.readdirSync(this.contentFolder);
for (const filename of files) {
if (!fs.statSync(path.resolve(this.contentFolder, filename)).isFile() || !filename.endsWith(this.extension)) continue;
const content = this.loadContent(filename, type);
if (content instanceof ContentError) errors.push(content);
}
return errors;
}
};

View File

@ -20,14 +20,19 @@ const releaseChannel = DiscordNative.globals.releaseChannel;
export default new class DataStore {
constructor() {
this.data = {};
this.data = {misc: {}};
this.pluginData = {};
}
initialize() {
if (!fs.existsSync(path.resolve(this.BDFile, ".."))) fs.mkdirSync(path.resolve(this.BDFile, ".."));
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
this.data = __non_webpack_require__(this.BDFile);
if (!fs.existsSync(this.baseFolder)) fs.mkdirSync(this.baseFolder);
if (!fs.existsSync(this.dataFolder)) fs.mkdirSync(this.dataFolder);
if (!fs.existsSync(this.BDFile)) fs.writeFileSync(this.BDFile, JSON.stringify(this.data.misc, null, 4));
const dataFiles = fs.readdirSync(this.dataFolder).filter(f => !fs.statSync(path.resolve(this.dataFolder, f)).isDirectory() && f.endsWith(".json"));
for (const file of dataFiles) {
this.data[file.split(".")[0]] = __non_webpack_require__(path.resolve(this.dataFolder, file));
}
// this.data = __non_webpack_require__(this.BDFile);
// if (data.hasOwnProperty("settings")) this.data = data;
// if (!fs.existsSync(this.settingsFile)) return;
// let settings = __non_webpack_require__(this.settingsFile);
@ -37,6 +42,8 @@ export default new class DataStore {
// this.setBDData("settings", settings);
}
get baseFolder() {return this._baseFolder || (this._baseFolder = path.resolve(Config.dataPath, "data"));}
get dataFolder() {return this._dataFolder || (this._dataFolder = path.resolve(this.baseFolder, `${releaseChannel}`));}
get BDFile() {return this._BDFile || (this._BDFile = path.resolve(Config.dataPath, "data", `${releaseChannel}.json`));}
// get settingsFile() {return this._settingsFile || (this._settingsFile = path.resolve(Config.dataPath, "bdsettings.json"));}
getPluginFile(pluginName) {return path.resolve(Config.dataPath, "plugins", pluginName + ".config.json");}
@ -50,22 +57,29 @@ export default new class DataStore {
// fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
// }
_getFile(key) {
if (key == "settings" || key == "plugins" || key == "themes") return path.resolve(this.dataFolder, `${key}.json`);
return path.resolve(this.dataFolder, `misc.json`);
}
getBDData(key) {
return this.data[key] || "";
return this.data.misc[key] || "";
}
setBDData(key, value) {
this.data[key] = value;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
this.data.misc[key] = value;
fs.writeFileSync(path.resolve(this.dataFolder, `misc.json`), JSON.stringify(this.data.misc, null, 4));
}
getData(key) {
return this.data[key] || "";
// return JSON.parse(fs.readFileSync(path.resolve(this.dataFolder, `${file}.json`)));
}
setData(key, value) {
this.data[key] = value;
fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
// fs.writeFileSync(this.BDFile, JSON.stringify(this.data, null, 4));
fs.writeFileSync(path.resolve(this.dataFolder, `${key}.json`), JSON.stringify(value, null, 4));
}
getPluginData(pluginName, key) {

View File

@ -195,4 +195,20 @@ PluginModule.prototype.rawObserver = function(e) {
}
};
export default new PluginModule();
export default new PluginModule();
// makePlaceholderPlugin(data) {
// return {plugin: {
// start: () => {},
// getName: () => {return data.name || data.filename;},
// getAuthor: () => {return "???";},
// getDescription: () => {return data.message ? data.message : "This plugin was unable to be loaded. Check the author's page for updates.";},
// getVersion: () => {return "???";}
// },
// name: data.name || data.filename,
// filename: data.filename,
// source: data.source ? data.source : "",
// website: data.website ? data.website : ""
// };
// }