Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
update internal utils to core-utils package (#489)
Browse files Browse the repository at this point in the history
update simples utils to core-utils package
  • Loading branch information
Sirherobrine23 committed Nov 7, 2022
1 parent 437f7e8 commit 1b057fe
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 567 deletions.
88 changes: 24 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"docs": "typedoc --readme none --out docs src/index.ts",
"dev": "nodemon",
"build": "tsc",
"test:partial": "mocha -r ts-node/register -r tsconfig-paths/register --exit",
"test:partial": "mocha -r ts-node/register --exit",
"test": "npm run test:partial -- 'tests/**/*.ts'"
},
"repository": {
Expand All @@ -32,16 +32,14 @@
"node": ">=16.0.0"
},
"dependencies": {
"@the-bds-maneger/core-utils": "^1.0.0",
"@the-bds-maneger/server_versions": "^4.2.0",
"adm-zip": "^0.5.9",
"cron": "^2.1.0",
"got": "^12.5.2",
"prismarine-nbt": "^2.2.1",
"tar": "^6.1.12"
},
"devDependencies": {
"@types/adm-zip": "^0.5.0",
"@types/cron": "^2.0.0",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.9",
"@types/tar": "^6.1.3",
Expand Down
28 changes: 13 additions & 15 deletions src/bedrock.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { platformManeger } from "@the-bds-maneger/server_versions";
import { manegerConfigProprieties } from "./configManipulate";
import { randomPort } from "./lib/randomPort";
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
import * as globalPlatfroms from "./globalPlatfroms";
import * as coreUtils from "@the-bds-maneger/core-utils";
import path from "node:path";
import fsOld from "node:fs";
import fs from "node:fs/promises";
import * as globalPlatfroms from "./globalPlatfroms";
import { platformManeger } from "@the-bds-maneger/server_versions";
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
import { commendExists } from "./lib/childPromisses";
import * as httpLarge from "@http/large";
import extendsFs, { exists, readdirrecursive } from "./lib/extendsFs";
import { randomPort } from "./lib/randomPort";
import { manegerConfigProprieties } from "./configManipulate";

// RegExp
export const saveFileFolder = /^(worlds|server\.properties|config|((permissions|allowlist|valid_known_packs)\.json)|(development_.*_packs))$/;
Expand All @@ -28,7 +26,7 @@ export async function installServer(version: string|boolean, platformOptions: bd
await fs.readdir(serverPath).then(files => Promise.all(files.filter(file => !saveFileFolder.test(file)).map(file => fs.rm(path.join(serverPath, file), {recursive: true, force: true}))));

const serverConfigProperties = (await fs.readFile(path.join(serverPath, "server.properties"), "utf8").catch(() => "")).trim();
await httpLarge.extractZip({url, folderTarget: serverPath});
await coreUtils.httpRequestLarge.extractZip({url, folderTarget: serverPath});
if (serverConfigProperties) await fs.writeFile(path.join(serverPath, "server.properties"), serverConfigProperties);
await fs.writeFile(path.join(serverRoot, "version_installed.json"), JSON.stringify({version: bedrockData.version, date: bedrockData.date, installDate: new Date()}));

Expand Down Expand Up @@ -57,9 +55,9 @@ export async function startServer(platformOptions: bdsPlatformOptions = {id: "de
let command = path.join(serverPath, "bedrock_server");
if ((["android", "linux"]).includes(process.platform) && process.arch !== "x64") {
args.push(command);
if (await commendExists("qemu-x86_64-static")) command = "qemu-x86_64-static";
else if (await commendExists("qemu-x86_64")) command = "qemu-x86_64";
else if (await commendExists("box64")) command = "box64";
if (await coreUtils.customChildProcess.commendExists("qemu-x86_64-static")) command = "qemu-x86_64-static";
else if (await coreUtils.customChildProcess.commendExists("qemu-x86_64")) command = "qemu-x86_64";
else if (await coreUtils.customChildProcess.commendExists("box64")) command = "box64";
else throw new Error("Cannot emulate x64 architecture. Check the documentents in \"https://github.com/The-Bds-Maneger/Bds-Maneger-Core/wiki/Server-Platforms#minecraft-bedrock-server-alpha\"");
}
const backendStart = new Date();
Expand Down Expand Up @@ -161,7 +159,7 @@ export type bedrockConfig = {
export async function serverConfig(platformOptions: bdsPlatformOptions = {id: "default"}) {
const { serverPath } = await pathControl("bedrock", platformOptions);
const fileProperties = path.join(serverPath, "server.properties");
if (!await extendsFs.exists(fileProperties)) await fs.cp(path.join(__dirname, "../configs/java/server.properties"), fileProperties);
if (!await coreUtils.extendFs.exists(fileProperties)) await fs.cp(path.join(__dirname, "../configs/java/server.properties"), fileProperties);
return manegerConfigProprieties<editConfig, bedrockConfig>({
configPath: fileProperties,
configManipulate: {
Expand Down Expand Up @@ -277,9 +275,9 @@ export type resourceManifest = {
export async function addResourcePacksToWorld(resourceId: string, platformOptions: bdsPlatformOptions = {id: "default"}) {
const { serverPath } = await pathControl("bedrock", platformOptions);
const serverConfigObject = (await serverConfig(platformOptions)).getConfig();
if (!await exists(path.join(serverPath, "worlds", serverConfigObject["level-name"], "world_resource_packs.json"))) await fs.writeFile(path.join(serverPath, "worlds", serverConfigObject["level-name"], "world_resource_packs.json"), "[]");
if (!await coreUtils.extendFs.exists(path.join(serverPath, "worlds", serverConfigObject["level-name"], "world_resource_packs.json"))) await fs.writeFile(path.join(serverPath, "worlds", serverConfigObject["level-name"], "world_resource_packs.json"), "[]");
const resourcesData: resourcePacks[] = JSON.parse(await fs.readFile(path.join(serverPath, "worlds", serverConfigObject["level-name"], "world_resource_packs.json"), "utf8"));
const manifests: resourceManifest[] = await Promise.all((await readdirrecursive([path.join(serverPath, "resource_packs"), path.join(serverPath, "worlds", serverConfigObject["level-name"], "resource_packs")])).filter((file: string) => file.endsWith("manifest.json")).map(async (file: string) => JSON.parse(await fs.readFile(file, "utf8"))));
const manifests: resourceManifest[] = await Promise.all((await coreUtils.extendFs.readdirrecursive([path.join(serverPath, "resource_packs"), path.join(serverPath, "worlds", serverConfigObject["level-name"], "resource_packs")])).filter((file: string) => file.endsWith("manifest.json")).map(async (file: string) => JSON.parse(await fs.readFile(file, "utf8"))));
const packInfo = manifests.find(pf => pf.header.uuid === resourceId);
if (!packInfo) throw new Error("UUID to texture not installed in the server");
if (resourcesData.includes({pack_id: resourceId})) throw new Error("Textura alredy installed in the World");
Expand Down
7 changes: 3 additions & 4 deletions src/export_import.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as coreUtils from "@the-bds-maneger/core-utils";
import net from "node:net";
import crypto from "node:crypto";
import fs from "node:fs/promises";
import tar from "tar";
import { bdsRoot } from "./platformPathManeger";
import { exists } from "./lib/extendsFs";
import { getExternalIP } from "@http/client";

export type payload = {
httpVersion?: string,
Expand Down Expand Up @@ -123,7 +122,7 @@ export class exportBds {
public async listen(port = 0) {
return new Promise<number>(done => {
this.#server.listen(port, async () => {
const externalIP = await getExternalIP();
const externalIP = await coreUtils.httpRequestClient.getExternalIP();
let address = this.#server.address()["address"], port = this.#server.address()["port"];
if (/::/.test(address)) address = `[${address}]`;
console.log("auth token '%s'", this.authToken);
Expand All @@ -143,7 +142,7 @@ export class exportBds {
}

export async function importBds(option: {host: string, port: number, authToken: string}) {
if (await exists(bdsRoot)) await fs.rename(bdsRoot, bdsRoot+"_backup_"+Date.now());
if (await coreUtils.extendFs.exists(bdsRoot)) await fs.rename(bdsRoot, bdsRoot+"_backup_"+Date.now());
const {socket} = await parsePayload(stringifyPayload(net.createConnection({host: option.host, port: option.port}), {request: {method: "GET", path: "/"}, header: {Authorization: option.authToken}}));
console.info("Connection sucess!");
const tar_extract = tar.extract({cwd: bdsRoot, noChmod: false, noMtime: false, preserveOwner: true});
Expand Down
12 changes: 2 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// HTTP
import * as httpSimples from "@http/simples";
import * as httpLarge from "@http/large";
import * as httpGithub from "@http/github";
import * as httpClient from "@http/client";
const httpRequest = {httpSimples, httpLarge, httpGithub, httpClient};

// Utils
import * as platformPathManeger from "./platformPathManeger"
import * as globalPlatfroms from "./globalPlatfroms";
Expand All @@ -21,7 +14,7 @@ import * as Spigot from "./spigot";
import * as Powernukkit from "./pwnuukit";
import * as PaperMC from "./paper";

export {platformPathManeger, globalPlatfroms, pluginManeger, export_import, httpRequest, PocketmineMP, pluginHooks, Powernukkit, PaperMC, Bedrock, Spigot, proxy, Java};
export {platformPathManeger, globalPlatfroms, pluginManeger, export_import, PocketmineMP, pluginHooks, Powernukkit, PaperMC, Bedrock, Spigot, proxy, Java};
export default {
Bedrock,
Java,
Expand All @@ -35,7 +28,6 @@ export default {
pluginManeger,
pluginHooks,
export_import,
proxy,
httpRequest
proxy
}
};
15 changes: 7 additions & 8 deletions src/java.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import path from "node:path";
import fs from "node:fs/promises";
import fsOld from "node:fs";
import os from "node:os";
import * as coreUtils from "@the-bds-maneger/core-utils";
import { platformManeger } from "@the-bds-maneger/server_versions";
import { actionV2, actionsV2 } from "./globalPlatfroms";
import { saveFile } from "@http/large";
import { pathControl, bdsPlatformOptions } from "./platformPathManeger";
import { manegerConfigProprieties } from "./configManipulate";
import { randomPort } from "./lib/randomPort";
import extendsFs from "./lib/extendsFs";
import fsOld from "node:fs";
import path from "node:path";
import fs from "node:fs/promises";
import os from "node:os";

export async function installServer(version: string|boolean, platformOptions: bdsPlatformOptions = {id: "default"}) {
const { serverPath, serverRoot, platformIDs, id } = await pathControl("java", platformOptions);
const javaDownload = await platformManeger.java.find(version);
await saveFile({url: javaDownload.url, filePath: path.join(serverPath, "server.jar")});
await coreUtils.httpRequestLarge.saveFile({url: javaDownload.url, filePath: path.join(serverPath, "server.jar")});
await fs.writeFile(path.join(serverRoot, "version_installed.json"), JSON.stringify({version: javaDownload.version, date: javaDownload.date, installDate: new Date()}));

if (platformIDs.length > 1) {
Expand Down Expand Up @@ -136,7 +135,7 @@ export type editConfig = Gamemode|Difficulty|serverPort|maxPlayers|allowList|ser
export async function serverConfig(platformOptions: bdsPlatformOptions = {id: "default"}) {
const { serverPath } = await pathControl("java", platformOptions);
const fileProperties = path.join(serverPath, "server.properties");
if (!await extendsFs.exists(fileProperties)) await fs.cp(path.join(__dirname, "../configs/java/server.properties"), fileProperties);
if (!await coreUtils.extendFs.exists(fileProperties)) await fs.cp(path.join(__dirname, "../configs/java/server.properties"), fileProperties);
return manegerConfigProprieties<editConfig>({
configPath: fileProperties,
configManipulate: {
Expand Down

0 comments on commit 1b057fe

Please sign in to comment.