-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.patch.ts
More file actions
42 lines (35 loc) · 1.09 KB
/
Copy pathenv.patch.ts
File metadata and controls
42 lines (35 loc) · 1.09 KB
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
import { config } from "dotenv";
import { expand } from "dotenv-expand";
import path from "node:path";
import { z } from "zod";
//console.log(process.cwd(), import.meta.url);
expand(
config({
path: path.resolve(process.cwd(), process.env.NODE_ENV === "test" ? ".env.test" : ".env")
})
);
const EnvSchema = z
.object({
NODE_ENV: z.string().default("development"),
PORT: z.coerce.number().default(4321),
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace", "silent"])
})
.superRefine((input, ctx) => {
if (input.NODE_ENV === "production") {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: "undefined",
message: "Must be set when NODE_ENV is 'production'"
});
}
});
export type envType = z.infer<typeof EnvSchema>;
// eslint-disable-next-line ts/no-redeclare
const { data: env, error } = EnvSchema.safeParse(process.env);
if (error) {
console.error("❌ Invalid env:");
console.error(JSON.stringify(error.flatten().fieldErrors, null, 2));
process.exit(1);
}
export default env!;