Plugin Maneger #276

Merged
Sirherobrine23 merged 5 commits from PluginManeger into main 2022-01-04 13:39:34 +00:00
3 changed files with 105 additions and 6 deletions
Showing only changes of commit 44523d9f50 - Show all commits

60
src/PluginManeger.js Normal file
View File

@@ -0,0 +1,60 @@
const request = require("./lib/Requests");
const BdsSettings = require("./lib/BdsSettings");
const path = require("path");
const fs = require("fs");
const js_yaml = require("js-yaml");
const RawGithubUrl = "https://raw.githubusercontent.com/The-Bds-Maneger/Plugins_Repository/main";
async function PluginManeger(BdsPlatform = BdsSettings.GetPlatform()) {
const GetPluginsPath = async () => {
return (await request.GithubTree("The-Bds-Maneger/Plugins_Repository", "main")).tree.filter(Tree => Tree.path.startsWith(BdsPlatform));
}
if ((await GetPluginsPath()).length === 0) throw new Error(`Bds Platform ${BdsPlatform} not found`);
const GetPlugin = async (pluginName = "") => {
if (!pluginName) throw new Error("Plugin name not found");
const RepositoryPlugins = (await GetPluginsPath());
if (RepositoryPlugins[0] === undefined) throw new Error(`Bds Platform ${BdsPlatform} not found`);
const PluginPath = `${BdsPlatform}/${pluginName.charAt(0).toLocaleLowerCase()}/${pluginName}`;
const PluginArray = RepositoryPlugins.filter(Plugin => Plugin.path.startsWith(PluginPath));
if (PluginArray[0] === undefined) throw new Error(`Plugin ${pluginName} not found`);
const ConfigFile = js_yaml.load(await request.text(`${RawGithubUrl}/${PluginPath}/${path.basename(PluginArray.filter(A => /config\.y[a]ml$/gi.test(A.path))[0].path)}`));
console.log(ConfigFile);
if (fs.existsSync(path.resolve(__dirname, `./PluginManeger/revision/${ConfigFile.revision.trim()}/Config`))) throw new Error(`Plugin ${pluginName} is outdated`);
return require(`./PluginManeger/revision/${ConfigFile.revision.trim()}/Config`).Parse(ConfigFile);
}
const InstallPlugin = async (PluginName = "", Version = "latest") => {
const Config = await GetPlugin(PluginName);
const Plugin = Config.versions.filter(Version => Version.version === Version)[0];
if (Plugin === undefined) throw new Error(`Plugin ${PluginName} version ${Version} not found`);
if (BdsPlatform === "pocketmine") {
if (Config.type === "plugin") {
const PluginPath = path.join(BdsSettings.GetServerPaths("pocketmine"), "plugins");
fs.writeFileSync(path.join(PluginPath, `${PluginName}.phar`), await request.buffer(Plugin.url));
} else if (Config.type === "resourcepack") {
const PluginPath = path.join(BdsSettings.GetServerPaths("pocketmine"), "resourcepacks");
fs.writeFileSync(path.join(PluginPath, `${PluginName}.zip`), await request.buffer(Plugin.url));
} else throw new Error(`Plugin ${PluginName} type (${Config.type}) not supported`);
} else if (BdsPlatform === "bedrock") {
if (Config.type === "texture") {
if (Plugin.type === "texture_addon") {
throw new Error("not implemented");
} else if (Plugin.type === "addon") {
throw new Error("not implemented");
} else if (Plugin.type === "texture") {
throw new Error("not implemented");
} else throw new Error(`Plugin ${PluginName} type (${Config.type}) not supported`);
}
}
}
return {
GetPlugin,
InstallPlugin,
};
}
module.exports = {
PluginManeger
};

View File

@@ -0,0 +1,7 @@
function Parse(Config = {}) {
return Config;
}
module.exports = {
Parse
};

View File

@@ -1,10 +1,12 @@
if (typeof global.fetch !== "function") {
global.fetch = (...args) => import("node-fetch").then(m => m.default(...args));
import("node-fetch").then(m => global.fetch = m.default);
if (typeof global !== "undefined") {
if (typeof global.fetch === "undefined") {
global.fetch = (...args) => import("node-fetch").then(Fetch => Fetch.default(...args));
import("node-fetch").then(Fetch => global.fetch = Fetch.default);
}
}
async function BufferHTTP(url = "", args = {}) {
const res = await fetch(url, {
const Fetch = (await import("node-fetch")).default;
const res = await Fetch(url, {
mode: "cors",
...args
});
@@ -24,6 +26,33 @@ async function TextHTTP(url = "", args = {}) {
return (await BufferHTTP(url, args)).toString();
}
async function GithubTree(repo = "The-Bds-Maneger/Plugins_Repository", branch = "main") {
let res = {
"sha": "db0e9588de141e033b62bb581ac65b89f5c57f5b",
"url": "https://api.github.com/repos/The-Bds-Maneger/Plugins_Repository/git/trees/db0e9588de141e033b62bb581ac65b89f5c57f5b",
"tree": [
{
"path": ".gitignore",
"mode": "100644",
"type": "blob",
"sha": "40b878db5b1c97fc77049537a71bb2e249abe5dc",
"size": 13,
"url": "https://api.github.com/repos/The-Bds-Maneger/Plugins_Repository/git/blobs/40b878db5b1c97fc77049537a71bb2e249abe5dc"
},
{
"path": "repository",
"mode": "040000",
"type": "tree",
"sha": "1dd3854f47e8c38200788f718058fac981a27227",
"url": "https://api.github.com/repos/The-Bds-Maneger/Plugins_Repository/git/trees/1dd3854f47e8c38200788f718058fac981a27227"
},
],
"truncated": false
}
res = await JsonHTTP(`https://api.github.com/repos/${repo}/git/trees/${branch}?recursive=true`);
return res;
}
// Export Bds Request
module.exports = {
// JSON
@@ -36,5 +65,8 @@ module.exports = {
// Buffer
BUFFER: BufferHTTP,
buffer: BufferHTTP
buffer: BufferHTTP,
// Others
GithubTree
}