Add new Bds Maneger Core CLI #194

Merged
Sirherobrine23 merged 12 commits from CLI into main 2021-09-25 06:20:18 +00:00
8 changed files with 1243 additions and 46 deletions
Showing only changes of commit b31ba78b93 - Show all commits

6
.gitignore vendored
View File

@ -9,16 +9,10 @@ Test/
# Node # Node
node_modules/ node_modules/
Bds_Maneger
.dccache .dccache
docs/ docs/
the-bds-maneger-core-*.tgz the-bds-maneger-core-*.tgz
# Bds Maneger Core Binaries
bds_maneger
BdsManager-bin*
Bds-Maneger-Core
# ** # **
.husky .husky
Servers Servers

View File

@ -9,6 +9,7 @@ process.env.IS_BDS_CLI = process.env.IS_BIN_BDS = true;
const argv = require("minimist")(process.argv.slice(2)); const argv = require("minimist")(process.argv.slice(2));
if (Object.getOwnPropertyNames(argv).length <= 1) argv.help = true if (Object.getOwnPropertyNames(argv).length <= 1) argv.help = true
// Bds Core Imports
const bds = require("../index"); const bds = require("../index");
const SystemInfo = require("../lib/BdsSystemInfo"); const SystemInfo = require("../lib/BdsSystemInfo");
const { bds_dir, GetServerVersion, GetPlatform, UpdatePlatform, GetServerPaths, GetPaths } = require("../lib/BdsSettings"); const { bds_dir, GetServerVersion, GetPlatform, UpdatePlatform, GetServerPaths, GetPaths } = require("../lib/BdsSettings");

86
bin/bds_maneger/menus.js Normal file
View File

@ -0,0 +1,86 @@
// Import Node Modules
const fs = require("fs");
// Import external modules
const inquirer = require("inquirer");
// Bds Core
const { getBdsConfig } = require("../../index");
const { GetPaths } = require("../../lib/BdsSettings");
const GetPlayers = (Platform = getBdsConfig().server.platform) => [...JSON.parse(fs.readFileSync(GetPaths("player"), "utf8"))[Platform]];
async function TpMenu() {
const { BdsRun } = require("../../src/BdsManegerServer");
const playerList = GetPlayers().map(player => player.Player && player.Action === "connect" ? player.Player : null).filter(a => a);
// Check if there are players online
if (playerList.length > 0) {
const Player = await inquirer.prompt([
{
type: "list",
name: "player",
message: "Select a player",
choices: playerList
}
]);
// Ask X, Y and Z Cordinates
const cords = await inquirer.prompt([
{
type: "input",
name: "x",
message: "X Cordinate",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return "Please enter a number";
}
},
{
type: "input",
name: "y",
message: "Y Cordinate",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return "Please enter a number";
}
},
{
type: "input",
name: "z",
message: "Z Cordinate",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return "Please enter a number";
}
}
]);
return BdsRun.tp(Player.player, {
x: parseInt(cords.x),
y: parseInt(cords.y),
z: parseInt(cords.z)
});
} else throw new Error("No players online");
}
async function Command() {
const { BdsRun } = require("../../src/BdsManegerServer");
const Command = await inquirer.prompt([
{
type: "input",
name: "command",
message: "Enter a command"
}
]);
return BdsRun.command(Command.command);
}
module.exports.Command = Command;
module.exports.TpMenu = TpMenu;
TpMenu();

77
bin/bds_maneger_v2.js Normal file
View File

@ -0,0 +1,77 @@
#!/usr/bin/env node
if (process.platform === "win32") process.title = "Bds Maneger CLI"; else process.title = "Bds-Manger-CLI";
process.env.IS_BDS_CLI = process.env.IS_BIN_BDS = true;
// External Modules
const cli_color = require("cli-color");
const inquirer = require("inquirer");
// Bin Args
const ProcessArgs = require("minimist")(process.argv.slice(2));
// Import Bds Core
const BdsCore = require("../index");
// Async functiona
async function Runner() {
// ESM Modules
const ora = (await import("ora")).default;
// Update Bds Core Platform
if (ProcessArgs.platform || ProcessArgs.p) {
const UpdatePla = ora("Updating Bds Platform").start();
try {
BdsCore.platform_update(ProcessArgs.platform || ProcessArgs.p);
UpdatePla.succeed(`Now the platform is the ${ProcessArgs.platform || ProcessArgs.p}`);
} catch (error) {
UpdatePla.fail(`Unable to update platform to ${ProcessArgs.platform || ProcessArgs.p}`);
process.exit(1);
}
}
// Download
if (ProcessArgs.download || ProcessArgs.d) {
const oraDownload = ora("Downloading...").start();
try {
const DownloadInfo = await BdsCore.download.v2(ProcessArgs.d || ProcessArgs.download, true);
const DownloadSucess = ["Downloaded Successfully"];
if (DownloadInfo.version) DownloadSucess.push(`Version: ${DownloadInfo.version}`);
if (DownloadInfo.data) DownloadSucess.push(`Data: ${DownloadInfo.data}`);
if (DownloadInfo.platform) DownloadSucess.push(`Bds Core Platform: ${DownloadInfo.platform}`);
oraDownload.succeed(DownloadSucess.join(", "))
} catch (error) {
oraDownload.fail(error.message);
process.exit(1);
}
}
// Start
const BdsCoreStart = BdsCore.start();
BdsCoreStart.log(data => process.stdout.write(cli_color.blueBright(data)));
BdsCoreStart.exit(code => {
console.log(cli_color.redBright(`Bds Core Exit with code ${code}, Uptimed: ${BdsCoreStart.uptime}`));
process.exit(code);
});
const BdsCliMenus = require("./bds_maneger/menus");
const InfinitCommands = async () => {
const CommandtoRun = (await inquirer.prompt([
{
type: "list",
name: "commands",
message: "Select a command",
choices: [
"tp"
]
}
])).commands;
if (CommandtoRun === "tp") {
BdsCliMenus.tp();
return await InfinitCommands();
} else {
BdsCliMenus.Command();
return await InfinitCommands();
}
}
InfinitCommands();
}
Runner();

View File

@ -50,7 +50,6 @@ var Config = {
bedrock: null, bedrock: null,
java: null, java: null,
pocketmine: null, pocketmine: null,
jsprismarine: null,
spigot: null, spigot: null,
}, },
Settings: { Settings: {
@ -155,6 +154,7 @@ function GetPaths(path = null){
function GetServerPaths(path = null){ function GetServerPaths(path = null){
if (!(path)) throw new Error("Set path to get"); if (!(path)) throw new Error("Set path to get");
if (!(ServersPaths[path])) throw new Error("Put a valid path: " + Object.getOwnPropertyNames(ServersPaths).join(", ")); if (!(ServersPaths[path])) throw new Error("Put a valid path: " + Object.getOwnPropertyNames(ServersPaths).join(", "));
if (path === "all") return ServersPaths
return ServersPaths[path] return ServersPaths[path]
} }
@ -275,6 +275,7 @@ if (!(existsSync(GetPaths("player")))) {
module.exports = { module.exports = {
bds_dir: bds_dir, bds_dir: bds_dir,
ServersPaths: ServersPaths,
GetJsonConfig, GetJsonConfig,
GetPaths, GetPaths,
GetServerPaths, GetServerPaths,

949
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -59,12 +59,14 @@
"express-prettify": "^0.1.1", "express-prettify": "^0.1.1",
"express-rate-limit": "^5.2.3", "express-rate-limit": "^5.2.3",
"googleapis": "^87.0.0", "googleapis": "^87.0.0",
"inquirer": "^8.1.5",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"minimist": "^1.2.5", "minimist": "^1.2.5",
"node-cron": "^3.0.0", "node-cron": "^3.0.0",
"node-fetch": "^3.0.0", "node-fetch": "^3.0.0",
"oci-sdk": "^2.0.0", "oci-sdk": "^2.0.0",
"open": "^8.0.0", "open": "^8.0.0",
"ora": "^6.0.1",
"properties-to-json": "^0.2.1", "properties-to-json": "^0.2.1",
"request-ip": "^2.1.3", "request-ip": "^2.1.3",
"telegraf": "^4.0.0", "telegraf": "^4.0.0",

View File

@ -1,5 +1,9 @@
const { writeFileSync, existsSync, readFileSync, readdirSync, rmSync } = require("fs"); const child_process = require("child_process");
const { join, resolve, basename } = require("path"); const fs = require("fs");
const os = require("os");
const path = require("path");
const { writeFileSync, existsSync, readFileSync, readdirSync, rmSync } = fs;
const { join, resolve, basename } = path;
var AdmZip = require("adm-zip"); var AdmZip = require("adm-zip");
const BdsInfo = require("../lib/BdsSystemInfo"); const BdsInfo = require("../lib/BdsSystemInfo");
const { GetServerPaths, GetServerVersion, UpdateServerVersion, GetPlatform } = require("../lib/BdsSettings"); const { GetServerPaths, GetServerVersion, UpdateServerVersion, GetPlatform } = require("../lib/BdsSettings");
@ -23,14 +27,14 @@ module.exports = function (version = true, force_install = false, callback = (er
// JSON Configs and others // JSON Configs and others
const ServerVersion = GetServerVersion(); const ServerVersion = GetServerVersion();
const CurrentPlatform = GetPlatform(); const CurrentPlatform = GetPlatform();
if (typeof version === "boolean" || /true|latest/gi.test(version)) version = Servers.latest[CurrentPlatform] if (typeof version === "boolean" || /true|latest/gi.test(`${version}`.toLocaleLowerCase())) version = Servers.latest[CurrentPlatform];
// Donwload // Donwload
console.log(`Installing version ${version}`); console.log(`Installing version ${version}`);
// Bedrock Installer Script // Bedrock Installer Script
if (CurrentPlatform === "bedrock") { if (CurrentPlatform === "bedrock") {
if (valid_platform.bedrock === true){ if (valid_platform.bedrock === true){
if (version === "latest") version = Servers.latest.bedrock
if (!(force_install === true) && ServerVersion.bedrock === version) { if (!(force_install === true) && ServerVersion.bedrock === version) {
console.warn("Jumping, installed version") console.warn("Jumping, installed version")
if (typeof callback === "function") callback(undefined, true); if (typeof callback === "function") callback(undefined, true);
@ -190,8 +194,9 @@ module.exports = function (version = true, force_install = false, callback = (er
// Unidentified platform // Unidentified platform
else { else {
promise_reject(Error("Bds maneger Config file error")); const Err = Error("Bds maneger Config file error");
if (typeof callback === "function") callback(err); promise_reject(Err);
if (typeof callback === "function") callback(Err);
} }
} catch (err) { } catch (err) {
promise_reject(err); promise_reject(err);
@ -208,6 +213,154 @@ module.exports = function (version = true, force_install = false, callback = (er
}); });
} }
// New Download Method
module.exports.v2 = async (version = true, force = true) => {
const CurrentPlatform = GetPlatform();
const valid_platform = (await BdsInfo()).valid_platform;
const LocalServersVersions = bds.BdsSettigs.GetServerVersion();
const { ServersPaths } = bds.BdsSettigs;
// Load Version List
const ServerDownloadJSON = await Request.json(Extra.Fetchs.servers);
// Check is latest version options or boolean
if (typeof version === "boolean" || /true|false|latest/.test(`${version}`.toLocaleLowerCase())) version = ServerDownloadJSON.latest[CurrentPlatform];
if (!version) throw Error("No version found");
const ReturnObject = {
version: version,
platform: CurrentPlatform,
url: "",
data: "",
skip: false
}
// Bedrock
if (CurrentPlatform === "bedrock") {
if (valid_platform.bedrock) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions !== version) {
// Add info to ReturnObject
ReturnObject.url = ServerDownloadJSON.bedrock[version][bds.arch][process.platform];
ReturnObject.data = ServerDownloadJSON.bedrock[version].data;
// Download and Add buffer to AdmZip
const BedrockZip = new AdmZip(await Request.buffer(ReturnObject.url));
// Create Backup Bedrock Config
const BedrockConfigFiles = {
proprieties: "",
whitelist: "",
permissions: "",
}
// Get Bedrock Config Files
if (fs.existsSync(path.join(ServersPaths.bedrock, "bedrock_server.properties"))) BedrockConfigFiles.proprieties = fs.readFileSync(path.join(ServersPaths.bedrock, "bedrock_server.properties"), "utf8");
if (fs.existsSync(path.join(ServersPaths.bedrock, "whitelist.json"))) BedrockConfigFiles.whitelist = fs.readFileSync(path.join(ServersPaths.bedrock, "whitelist.json"), "utf8");
if (fs.existsSync(path.join(ServersPaths.bedrock, "permissions.json"))) BedrockConfigFiles.permissions = fs.readFileSync(path.join(ServersPaths.bedrock, "permissions.json"), "utf8");
// Extract to Bedrock Dir
BedrockZip.extractAllTo(ServersPaths.bedrock, true);
// Write Bedrock Config Files
if (BedrockConfigFiles.proprieties) fs.writeFileSync(path.join(ServersPaths.bedrock, "bedrock_server.properties"), BedrockConfigFiles.proprieties, "utf8");
if (BedrockConfigFiles.whitelist) fs.writeFileSync(path.join(ServersPaths.bedrock, "whitelist.json"), BedrockConfigFiles.whitelist, "utf8");
if (BedrockConfigFiles.permissions) fs.writeFileSync(path.join(ServersPaths.bedrock, "permissions.json"), BedrockConfigFiles.permissions, "utf8");
// Update Server Version
bds.BdsSettigs.UpdateServerVersion(version, CurrentPlatform);
} else {
ReturnObject.skip = true;
}
} else {
throw Error("Bedrock not suported");
}
}
// Java
else if (CurrentPlatform === "java") {
if (valid_platform.java) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions !== version) {
// Add info to ReturnObject
ReturnObject.url = ServerDownloadJSON.java[version].url;
ReturnObject.data = ServerDownloadJSON.java[version].data;
// Download and write java file
fs.writeFileSync(path.join(ServersPaths.java, "MinecraftServerJava.jar"), await Request.buffer(ReturnObject.url), "binary");
// Write EULA
fs.writeFileSync(path.join(ServersPaths.java, "eula.txt"), "eula=true");
// Update Server Version
bds.BdsSettigs.UpdateServerVersion(version, CurrentPlatform);
} else {
ReturnObject.skip = true;
}
} else {
throw Error("Java not suported");
}
}
// Spigot
else if (CurrentPlatform === "spigot") {
if (valid_platform.spigot) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions !== version) {
// Add info to ReturnObject
const FindedSpigot = ServerDownloadJSON.spigot.findOne(spigot => spigot.version === version);
ReturnObject.url = FindedSpigot.url;
ReturnObject.data = FindedSpigot.data;
// Download and write java file
fs.writeFileSync(path.join(ServersPaths.spigot, "spigot.jar"), await Request.buffer(ReturnObject.url), "binary");
// Update Server Version
bds.BdsSettigs.UpdateServerVersion(version, CurrentPlatform);
} else {
ReturnObject.skip = true;
}
} else {
throw Error("Spigot not suported");
}
}
// Dragonfly
else if (CurrentPlatform === "dragonfly") {
if (valid_platform.dragonfly) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions !== version) {
// Add info to ReturnObject
ReturnObject.url = "https://github.com/df-mc/dragonfly/tree/master";
ReturnObject.data = "";
// Build Dragonfly
const TmpDragonflyDir = path.join(os.tmpdir(), `dragonfly_${Math.random().toString(36).substring(7)}`);
child_process.execFileSync("git", ["clone", "https://github.com/df-mc/dragonfly", "--depth", "1", TmpDragonflyDir]);
let DragonflyPackageOut = path.join(ServersPaths.dragonfly, "DragonFly");
if (process.platform === "win32") DragonflyPackageOut += ".exe";
child_process.execFileSync("go", ["build", "-o", DragonflyPackageOut], {cwd: TmpDragonflyDir});
// move Dragonfly to ServersPaths
fs.renameSync(DragonflyPackageOut, path.join(ServersPaths.dragonfly, path.basename(DragonflyPackageOut)));
// Remove Build Dir
fs.rmSync(TmpDragonflyDir, {recursive: true, force: true});
// Update Server Version
bds.BdsSettigs.UpdateServerVersion(Math.random().toString(), CurrentPlatform);
} else {
ReturnObject.skip = true;
}
} else {
throw Error("Dragonfly not suported");
}
}
// if the platform does not exist
else throw Error("No Valid Platform");
// Return info download
return ReturnObject;
}
// Php download and install
async function php_download() { async function php_download() {
const bds_dir_pocketmine = GetServerPaths("pocketmine"); const bds_dir_pocketmine = GetServerPaths("pocketmine");
const PHPBin = (await (await fetch(Extra.Fetchs.php)).json()); const PHPBin = (await (await fetch(Extra.Fetchs.php)).json());