Skip to content

Commit

Permalink
Change: replace zod with @sinclair/typebox (#981)
Browse files Browse the repository at this point in the history
* Chore: temporary fix

* Chore: replace zod with @sinclair/typebox
  • Loading branch information
mtripg6666tdr authored Feb 10, 2023
1 parent 702e080 commit 5842f76
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 46 deletions.
27 changes: 12 additions & 15 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@mtripg6666tdr/async-lock": "^1.1.0",
"@mtripg6666tdr/eris-command-resolver": "^2.0.0",
"@mtripg6666tdr/m3u8stream": "^0.8.6",
"@sinclair/typebox": "^0.25.21",
"candyget": "^0.5.2",
"comment-json": "^4.2.3",
"dotenv": "^16.0.3",
Expand All @@ -29,8 +30,7 @@
"tslib": "^2.5.0",
"ytdl-core": "^4.11.2",
"ytpl": "^2.3.0",
"ytsr": "^3.8.0",
"zod": "^3.20.5"
"ytsr": "^3.8.0"
},
"devDependencies": {
"@commitlint/cli": "^17.4.2",
Expand Down
78 changes: 54 additions & 24 deletions src/Util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,68 @@
* If not, see <https://www.gnu.org/licenses/>.
*/

import type { Static } from "@sinclair/typebox";

import * as fs from "fs";
import * as path from "path";

import { Type } from "@sinclair/typebox";
import { TypeCompiler } from "@sinclair/typebox/compiler";
import { Value } from "@sinclair/typebox/value";
import CJSON from "comment-json";
import { z } from "zod";

/* eslint-disable newline-per-chained-call */
const GuildBGMContainer = z.object({
voiceChannelId: z.string().regex(/^\d+$/, {message: "channelId is not a snowflake"}),
allowEditQueue: z.boolean(),
enableQueueLoop: z.boolean(),
items: z.array(z.string()).nonempty({message: "items cannot be empty"}),
volume: z.number().min(5).max(200),
mode: z.union([z.literal("only"), z.literal("prior"), z.literal("normal")]),

const GuildBGMContainer = Type.Object({
voiceChannelId: Type.RegEx(/^\d+$/),
allowEditQueue: Type.Boolean(),
enableQueueLoop: Type.Boolean(),
items: Type.Array(Type.String({minLength: 1})),
volume: Type.Number({minimum: 5, maximum: 200}),
mode: Type.Union([
Type.Literal("only"),
Type.Literal("prior"),
Type.Literal("normal"),
]),
});

const Config = z.object({
adminId: z.string().regex(/^\d+$/, {message: "adminId is not a snowflake"}).nullable(),
debug: z.boolean(),
errorChannel: z.string().min(1).regex(/^\d+$/, {message: "errorChannel is not a snowflake"}).nullable(),
maintenance: z.boolean(),
proxy: z.string().url("proxy value must be resolvable as url").nullable(),
prefix: z.string().min(1).nullish().default(">"),
webserver: z.boolean().optional().default(true),
bgm: z.record(z.string().regex(/^\d+$/, {message: "bgm object key is not a snowflake"}), GuildBGMContainer).optional(),
noMessageContent: z.boolean().optional().default(false),
const Config = Type.Object({
adminId: Type.Union([
Type.RegEx(/^\d+$/),
Type.Null(),
], {default: false}),
debug: Type.Boolean({default: false}),
errorChannel: Type.Union([
Type.RegEx(/^\d+$/),
Type.Null(),
], {default: null}),
maintenance: Type.Boolean(),
proxy: Type.Union([
Type.RegEx(/https?:\/\/[\w!?/+\-_~;.,*&@#$%()'[\]]+/),
Type.Null(),
], {default: null}),
prefix: Type.Optional(Type.String({minLength: 1, default: ">"})),
webserver: Type.Optional(Type.Boolean({default: true})),
bgm: Type.Optional(Type.Record(Type.RegEx(/^\d+$/), GuildBGMContainer, {default: {}})),
noMessageContent: Type.Optional(Type.Boolean({default: false})),
});
/* eslint-enable newline-per-chained-call */

const checker = TypeCompiler.Compile(Config);

const rawConfig = fs.readFileSync(path.join(__dirname, "../../config.json"), {encoding: "utf-8"});

const config = Config.parse(CJSON.parse(rawConfig, null, true));
const config = CJSON.parse(rawConfig, null, true);

const errs = [...checker.Errors(config)];
if(errs.length > 0){
const er = new Error("Invalid config.json");
console.log(errs);
Object.defineProperty(er, "errors", {
value: errs,
});
throw er;
}

export default config;
export type GuildBGMContainerType = z.infer<typeof GuildBGMContainer>;
export default Object.assign(
Value.Create(Config),
config,
) as unknown as Static<typeof Config>;
export type GuildBGMContainerType = Static<typeof GuildBGMContainer>;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let server:http.Server = null;
if(Util.config.webserver){
server = createServer(bot.client, Number(process.env.PORT) || 8081, Util.logger.log);
}else{
logger("[Entry] Skipping to start server");
logger("Skipping to start server");
}

if(!Util.config.debug){
Expand Down
24 changes: 20 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,32 @@
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"outDir": "dist/",
"skipLibCheck": true
"allowJs": false,
"skipLibCheck": true,
"declaration": false,
"incremental": true,
"tsBuildInfoFile": "node_modules/.tsbuildinfo.json",
"lib": [
"ES2019",
"ES2020.Promise",
"ES2020.BigInt",
"ES2020.String",
],
"moduleResolution": "node",
"disableSizeLimit": true,
},
"typeAcquisition": {
"enable": false,
},
"include": [
"./src/**/*.ts"
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/node_modules",
"test",
"./util",
"./docs",
"./dist"
"./dist",
"**/.*/"
]
}

0 comments on commit 5842f76

Please sign in to comment.