-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
75 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,4 +134,7 @@ dist | |
*.d.ts | ||
.idea | ||
|
||
|
||
# default config file | ||
config.json | ||
!src/types/*.d.ts |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,82 @@ | ||
import { open } from "node:fs/promises"; | ||
import { open, access, constants, FileHandle } from "node:fs/promises"; | ||
import Logger from "./Logger.js"; | ||
|
||
export default class Config { | ||
public port: number = 25565; | ||
public logLevel: Logger.Level = Logger.Level.INFO; | ||
|
||
export interface Config { | ||
/** | ||
* Port to listen on | ||
*/ | ||
port: number; | ||
|
||
/** | ||
* The level to display logs at | ||
*/ | ||
logLevel: Logger.Level; | ||
|
||
/** | ||
* Kick reason for when the server is shutting down | ||
*/ | ||
public shutdownKickReason: string = "Server closed"; | ||
shutdownKickReason: ChatComponent; | ||
} | ||
|
||
export class ConfigLoader { | ||
/** | ||
* Get a Config instance from a json file | ||
* @param file The file to read from | ||
* @returns a promise that resolves to a Config instance | ||
* @throws {Error & {CODE: "EACCESS"}} failed to read config | ||
* @throws {SyntaxError} failed to parse config | ||
*/ | ||
public static async fromFile(file: string): Promise<Config> { | ||
let config: Config; | ||
if (!(await ConfigLoader.exists(file))) { | ||
await ConfigLoader.createDefault(file); | ||
const config = ConfigLoader.getDefault(); | ||
new Logger("Config", config.logLevel).warn("Config does not exist, creating default '%s'", file); | ||
return config; | ||
} | ||
const fd: FileHandle = await open(file, "r"); | ||
const data: string = await fd.readFile("utf-8"); | ||
fd.close(); | ||
|
||
return JSON.parse(data) as Config; | ||
} | ||
|
||
/** | ||
* Get a default config instance | ||
* @returns a default config instance | ||
*/ | ||
public static getDefault(): Config { | ||
return { | ||
port: 25565, | ||
logLevel: Logger.Level.INFO, | ||
shutdownKickReason: { | ||
text: "Server closed" | ||
} | ||
}; | ||
|
||
} | ||
|
||
/** | ||
* Checks if a config exists | ||
* @param file The file to check | ||
* @returns a promise that resolves to a boolean | ||
*/ | ||
public static async exists(file: string): Promise<boolean> { | ||
try { | ||
const fd = await open(file, "r"); | ||
const data = await fd.readFile("utf-8"); | ||
config = JSON.parse(data) as Config; | ||
fd.close(); | ||
await access(file, constants.F_OK); | ||
return true; | ||
} catch { | ||
config = new Config(); | ||
new Logger("Config", config.logLevel).error("Failed to read config file, using default config"); | ||
return false; | ||
} | ||
return config; | ||
} | ||
|
||
/** | ||
* Create the default config file | ||
*/ | ||
public static async createDefault(file: string): Promise<void> { | ||
const fd = await open(file, "w"); | ||
await fd.writeFile(JSON.stringify(ConfigLoader.getDefault(), null, 4)); | ||
fd.close(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters