Add new Bds Maneger Core CLI #194

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

View File

@ -4,7 +4,6 @@ process.env.IS_BDS_CLI = process.env.IS_BIN_BDS = true;
// External Modules
const cli_color = require("cli-color");
const inquirer = require("inquirer");
const serverline = require("serverline");
// Bin Args
@ -46,9 +45,11 @@ async function Runner() {
}
}
if (!(ProcessArgs.start || ProcessArgs.s)) return;
// Start
const BdsCoreStart = BdsCore.start();
BdsCoreStart.log(data => process.stdout.write(cli_color.blueBright(data)));
BdsCoreStart.log(data => console.log(cli_color.blueBright(data.replace(/\n$/gi, ""))));
BdsCoreStart.exit(code => {
console.log(cli_color.redBright(`Bds Core Exit with code ${code}, Uptimed: ${BdsCoreStart.uptime}`));
process.exit(code);
@ -56,8 +57,10 @@ async function Runner() {
serverline.init();
serverline.setCompletion(["tp"]);
serverline.setPrompt("Command > ");
serverline.on('line', function(line) {
BdsCoreStart.command(line);
serverline.on("line", function(line) {
if (/^@/.test(line)) {
console.log("🤪It's not working yet!");
} else BdsCoreStart.command(line);
});
}
Runner();

View File

@ -5,7 +5,7 @@ const fs = require("fs");
const randomUUID = require("uuid").v4;
const { bds_dir } = require("./lib/BdsSettings");
if (typeof fetch === "undefined") global.fetch = require("node-fetch");
require("./lib/Requests")
const bds_core_package = resolve(__dirname, "package.json")
module.exports.package_path = bds_core_package

View File

@ -1,7 +1,6 @@
const { join, resolve, basename } = require("path");
const { existsSync, writeFileSync, mkdirSync, readFileSync } = require("fs");
const { homedir } = require("os");
const BdsInfo = require("./BdsSystemInfo");
const yaml = require("js-yaml");
// PATHs
@ -9,19 +8,6 @@ const home = homedir();
const bds_dir = join(home, "bds_core");
if (!(existsSync(bds_dir))) mkdirSync(bds_dir, {recursive: true})
BdsInfo().then(validation => {
const { valid_platform } = validation;
// Set default platform for bds maneger
var default_platformConfig;
if (valid_platform["bedrock"]) default_platformConfig = "bedrock";
else if (valid_platform["java"]) default_platformConfig = "java";
else if (valid_platform["pocketmine"]) default_platformConfig = "pocketmine";
else throw new Error("We cannot run any platforms on this system/device");
UpdatePlatform(default_platformConfig);
}).catch(err => {
console.log(err);
process.exit(1);
});
// Config Base to Bds Maneger Core and others Projects
var Config = {
@ -115,12 +101,12 @@ var Config = {
// Config
const ConfigPath = join(resolve(homedir(), "bds_core"), "BdsConfig.yaml")
function SaveConfig(){writeFileSync(ConfigPath, yaml.dump(Config));}
if (existsSync(ConfigPath)) Config = {
...Config,
...yaml.load(readFileSync(ConfigPath, "utf8"))
}; else writeFileSync(ConfigPath, yaml.dump(Config))
process.on("exit", () => SaveConfig())
const SaveConfig = () => writeFileSync(ConfigPath, yaml.dump(Config));
process.on("exit", () => SaveConfig());
if (existsSync(ConfigPath)) Config = yaml.load(readFileSync(ConfigPath, "utf8"));
else writeFileSync(ConfigPath, yaml.dump(Config))
// Paths
if (!(existsSync(Config.paths["backups"]))) mkdirSync(Config.paths["backups"], {recursive: true})
@ -168,21 +154,20 @@ function UpdateServerVersion(version = null, platform = Config.server.platform){
}
// Update the entire Bds Manager Core platform
function UpdatePlatform(platform = Config.server.platform){
function UpdatePlatform(platform = "null"){
platform = platform.toLocaleLowerCase();
if (/bedrock/.test(platform)) {
Config.server.platform = "bedrock";
SaveConfig()
} else if (/java/.test(platform)) {
Config.server.platform = "java";
SaveConfig()
} else if (/pocketmine/.test(platform)) {
Config.server.platform = "pocketmine";
SaveConfig()
} else if (/spigot/.test(platform)) {
Config.server.platform = "spigot";
SaveConfig()
} else throw new Error("platform no Exists")
} else if (/dragonfly/.test(platform)) {
Config.server.platform = "dragonfly";
} else throw new Error("platform no exists");
SaveConfig();
return platform
}

View File

@ -35,7 +35,7 @@ async function CheckSystemAsync() {
if (valid_platform["bedrock"] === false) {
if (commadExist("qemu-x86_64-static")) {
console.warn("The Minecraft Bedrock Server is only being validated because you can use 'qemu-x86_64-static'");
// console.warn("The Minecraft Bedrock Server is only being validated because you can use 'qemu-x86_64-static'");
valid_platform["bedrock"] = true
require_qemu = true
}

View File

@ -72,6 +72,7 @@ function start() {
else if (GetPlatform() === "pocketmine") {
// Start PocketMine-MP
SetupCommands.command = path.join(path.resolve(GetServerPaths("pocketmine"), "bin", "php7", "bin"), "php");
if (process.platform === "win32") SetupCommands.command = path.join(path.resolve(GetServerPaths("pocketmine"), "bin/php"), "php.exe");
SetupCommands.args.push("./PocketMine-MP.phar");
SetupCommands.cwd = GetServerPaths("pocketmine");
}

View File

@ -3,7 +3,7 @@ const fs = require("fs");
const os = require("os");
const path = require("path");
const { writeFileSync, existsSync, readFileSync, readdirSync, rmSync } = fs;
const { join, resolve, basename } = path;
const { join, resolve } = path;
var AdmZip = require("adm-zip");
const BdsInfo = require("../lib/BdsSystemInfo");
const { GetServerPaths, GetServerVersion, UpdateServerVersion, GetPlatform } = require("../lib/BdsSettings");
@ -214,7 +214,7 @@ module.exports = function (version = true, force_install = false, callback = (er
}
// New Download Method
module.exports.v2 = async (version = true, force = true) => {
module.exports.v2 = async (version = true) => {
const CurrentPlatform = GetPlatform();
const valid_platform = (await BdsInfo()).valid_platform;
const LocalServersVersions = bds.BdsSettigs.GetServerVersion();
@ -238,7 +238,7 @@ module.exports.v2 = async (version = true, force = true) => {
// Bedrock
if (CurrentPlatform === "bedrock") {
if (valid_platform.bedrock) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions.bedrock !== version) {
if (LocalServersVersions.bedrock !== version) {
// Add info to ReturnObject
ReturnObject.url = ServerDownloadJSON.bedrock[version][bds.arch][process.platform];
ReturnObject.data = ServerDownloadJSON.bedrock[version].data;
@ -279,7 +279,7 @@ module.exports.v2 = async (version = true, force = true) => {
// Java
else if (CurrentPlatform === "java") {
if (valid_platform.java) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions.java !== version) {
if (LocalServersVersions.java !== version) {
// Add info to ReturnObject
ReturnObject.url = ServerDownloadJSON.java[version].url;
ReturnObject.data = ServerDownloadJSON.java[version].data;
@ -304,7 +304,7 @@ module.exports.v2 = async (version = true, force = true) => {
// Spigot
else if (CurrentPlatform === "spigot") {
if (valid_platform.spigot) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions.spigot !== version) {
if (LocalServersVersions.spigot !== version) {
// Add info to ReturnObject
const FindedSpigot = ServerDownloadJSON.spigot.findOne(spigot => spigot.version === version);
ReturnObject.url = FindedSpigot.url;
@ -326,7 +326,7 @@ module.exports.v2 = async (version = true, force = true) => {
// Dragonfly
else if (CurrentPlatform === "dragonfly") {
if (valid_platform.dragonfly) {
if (!(force === true && typeof force === "boolean") && LocalServersVersions.dragonfly !== version) {
if (LocalServersVersions.dragonfly !== version) {
// Add info to ReturnObject
ReturnObject.url = "https://github.com/df-mc/dragonfly/tree/master";
ReturnObject.data = "";
@ -355,7 +355,29 @@ module.exports.v2 = async (version = true, force = true) => {
}
// Pocketmine-MP
else if (CurrentPlatform === "pocketmine") {throw new Error("We are still creating for Pocketmine-MP");}
else if (CurrentPlatform === "pocketmine") {
if (valid_platform.pocketmine) {
if (LocalServersVersions.pocketmine !== version) {
// Add info to ReturnObject
ReturnObject.url = ServerDownloadJSON.pocketmine[version].url;
ReturnObject.data = ServerDownloadJSON.pocketmine[version].data;
// Download PHP Bin
await php_download();
// Download php file and save
const PocketmineBufferPhp = await Request.buffer(ReturnObject.url);
fs.writeFileSync(path.join(ServersPaths.pocketmine, "PocketMine-MP.phar"), PocketmineBufferPhp, "binary");
// Update Server Version
bds.BdsSettigs.UpdateServerVersion(version, CurrentPlatform);
} else {
ReturnObject.skip = true;
}
} else {
throw Error("Pocketmine-MP not suported");
}
}
// if the platform does not exist
else throw Error("No Valid Platform");
@ -379,17 +401,13 @@ async function php_download() {
// Remove Old php Binary if it exists
if (existsSync(phpFolder)) {
console.log("Removing old PHP files.");
rmSync(phpFolder, { recursive: true });
}
console.log(`Downloading ${urlPHPBin}`);
const ZipBuffer = Buffer.from((await (await fetch(urlPHPBin)).arrayBuffer()));
console.log(`${basename(urlPHPBin)} downloaded`);
console.log(`Extracting ${basename(urlPHPBin)}`);
const zipExtractBin = new AdmZip(ZipBuffer);
zipExtractBin.extractAllTo(bds_dir_pocketmine, false)
console.log("Successfully extracting the binaries")
if (process.platform === "win32") return resolve();
let phpConfigInit = readFileSync(join(phpFolder, "php7", "bin", "php.ini"), "utf8");
if (!(existsSync(phpExtensiosnsDir))) return true;