all repos — stealth-developers @ b407bb1efbde3f4d5006a35ccea44d93d2480d89

src/config.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
import fs from "node:fs";
import { z } from "zod";

const discordSchema = z.object({
	token: z.string(),
	app_id: z.string(),
});

const mongodbSchema = z.object({
	uri: z.string(),
	database: z.string(),
});

const projectSchema = z.record(
	z.object({
		universe: z.string(),
		name: z.string(),
		displayName: z.string(),
		iconURL: z.string().optional(),
		codes: z
			.array(
				z.object({
					code: z.string(),
					expired: z.boolean().optional(),
					expiredAt: z.string().datetime().optional(),
					addedAt: z.string().datetime().optional(),
				}),
			)
			.optional(),
	}),
);

const googleCloudSchema = z.object({
	credentials: z.object({
		clientEmail: z.string(),
		privateKey: z.string(),
	}),
	projectId: z.string(),
});

const catChannelSchema = z.object({
	channelId: z.string(),
});

const forumWatcher = z.object({
	enabled: z.boolean().default(false),
	interval: z
		.number()
		.int()
		.min(1)
		.default(60)
		.describe("how often to check for new posts in seconds"),
	groupId: z.string(),
	groupName: z.string().transform((val) => val.replace(/\s+/g, "-")),
	channelId: z.string(),
	notificationChannelId: z.string(),
});

const robloxSchema = z.object({
	apiKey: z.string(),
	cookie: z.string().optional(),
	forumWatcher: forumWatcher.optional(),
});

const bloxlinkSchema = z.object({
	token: z.string(),
});

const schema = z.object({
	discord: discordSchema,
	mongodb: mongodbSchema,
	projects: projectSchema,
	bloxlink: bloxlinkSchema.optional(),
	roblox: robloxSchema.optional(),
	trelloBoardId: z.string().optional(),
	developerId: z.string(),
	terminology: z.string().default("project"),
	googleCloud: googleCloudSchema.optional(),
	catChannel: catChannelSchema.optional(),
});

function validateConfig() {
	const env = process.env.NODE_ENV || "DEV";

	const runtimeConfig = JSON.parse(
		fs.readFileSync(`.config.${env}.json`, "utf8"),
	);
	const conf = schema.safeParse(runtimeConfig);
	if (conf.success) return conf.data;

	console.error("invalid environment variables");
	for (const err of conf.error.errors)
		console.log(`  ${err.message}: ${err.path}`);
	process.exit(1);
}

export default { data: validateConfig() };