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

Commit

Permalink
fix Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Apr 29, 2022
1 parent 1e3dc08 commit 83f28f4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dev": "nodemon",
"release": "standard-version",
"build": "run-s build:*",
"test": "npm run build && ts-node test/index.ts",
"test": "ts-node test/index.ts",
"build:cjs": "tsc --outDir dist/cjs --module commonjs",
"build:esm": "tsc --outDir dist/esm --module es6 && node -e 'const fs = require(\"fs\");const path = require(\"path\");const read = (pathRe) => {for (const fileFolde of fs.readdirSync(pathRe)) {const filePath = path.join(pathRe, fileFolde);if (fs.statSync(filePath).isDirectory()) read(filePath);else {console.log(filePath, \"-->\", filePath.replace(/\\.js$/, \".mjs\"));fs.renameSync(filePath, filePath.replace(/\\.js$/, \".mjs\"));}}};read(\"dist/esm\");'"
},
Expand Down
31 changes: 14 additions & 17 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import fs from "fs";
import os from "os";
import { EventEmitter } from "events";
import event from "events";
import crypto from "crypto";
import node_cron from "cron";
import * as platformManeger from "./platform";
Expand Down Expand Up @@ -34,19 +34,15 @@ type startServerOptions = {
gitBackup?: bdsBackup.gitBackupOption;
};

class logListen {
private _logListen: EventEmitter = new EventEmitter();
public on(type: "err"|"out"|"all", call: (data: string) => void) {
this._logListen.on(type, call);
this._logListen.on("all", call);
}
public once(type: "all"|"err"|"out", call: (data: string) => void) {
this._logListen.once(type, call);
}
public emit(type: "err"|"out", data: string) {
this._logListen.emit(type, data);
this._logListen.emit("all", data);
function logListen() {
const EventListen = new event();
const on = (type: "err"|"out"|"all", call: (data: string) => void) => EventListen.on(type, call);
const once = (type: "all"|"err"|"out", call: (data: string) => void) => EventListen.once(type, call);
const emit = (type: "err"|"out", data: string) => {
EventListen.emit(type, data);
EventListen.emit("all", data);
}
return {on, once, emit};
}

export type BdsSession = {
Expand Down Expand Up @@ -130,7 +126,7 @@ export async function Start(Platform: bdsTypes.Platform, options?: startServerOp
const StartDate = new Date();

// Log events
const logsEvent = new logListen();
const logsEvent = logListen();
const onLog = {on: logsEvent.on, once: logsEvent.once};

// Storage tmp lines
Expand All @@ -142,9 +138,7 @@ export async function Start(Platform: bdsTypes.Platform, options?: startServerOp
const localCall = tempLog[to];
tempLog[to] = "";
const filtedLog = localCall.replace(/\r\n/gi, "\n").replace(/\n$/gi, "").split(/\n/gi).filter(a => a !== undefined);
filtedLog.forEach(data => {
logsEvent.emit(to, data);
});
filtedLog.forEach(data => logsEvent.emit(to, data));
}
return;
}
Expand Down Expand Up @@ -339,6 +333,9 @@ export async function Start(Platform: bdsTypes.Platform, options?: startServerOp

if (Platform === "bedrock") {
Seesion.seed = (await platformManeger.bedrock.config.getConfig()).worldSeed;
onLog.on("all", lineData => {
if (/\[.*\]\s+Server\s+started\./.test(lineData)) Seesion.started = true;
});
}

// Return Session
Expand Down
1 change: 1 addition & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ const functionToRun: Array<(done: (done?: Error) => any) => Promise<void>> = [do
}
})().then(() => process.exit(0)).catch((error) => {
console.error("Error: %s", String(error));
console.trace(error);
process.exit(1);
});
4 changes: 2 additions & 2 deletions test/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import bdscore from "../src/index";
export default async function start(done: (error?: Error) => void) {
console.log("Starting...");
const Server = await bdscore.Server.Start("bedrock");
Server.logRegister("all", console.log);
Server.log.on("all", console.log);
Server.onExit(code => {
if (code !== 0) {
done(new Error(`Server exited with code ${code}`));
Expand All @@ -14,7 +14,7 @@ export default async function start(done: (error?: Error) => void) {
while (true) {
if (Server.started) {
console.log("Server started");
Server.stop();
Server.commands.stop();
break;
}
await new Promise(resolve => setTimeout(resolve, 5*100));
Expand Down

0 comments on commit 83f28f4

Please sign in to comment.