export async function checkEnv(envPath: string): Promise<void> {
if(await (Bun.file(envPath)).exists() === false) {
logger.error("Cannot access .env configuration file, creating a new one with default configuration...");
let defaultJWTSecret = randomBytes(512).toString("hex");
await Bun.write(envPath, `# The string, with which all of the JSONWebTokens are encrypted\n# Defaults to 512 random bytes if left blank\n# WARNING: IF YOU CHANGE THE JWTSECRET, THE ENTIRE DATABASE WILL BE INVALIDATED, THIS WILL LEAD TO UNEXPECTED BEHAVIOUR\n# IN THAT CASE PLEASE USE THE -d FLAG TO CLEAR THE DATABASE\nJWTSECRET=${defaultJWTSecret}\n# Set this to valid Port (1-65535)\nPORT=9495`);
process.env.PORT = "9495";
process.env.JWTSECRET = defaultJWTSecret;
logger.info("Done!");
}
const parsedEnv = envSchema.safeParse(Bun.env);
//@ts-ignore Opened issue at https://github.com/colinhacks/zod/issues/2883
if(parsedEnv.success === false) {
JSON.parse(parsedEnv.error.message).forEach((errorMessage: string) => {
logger.error(errorMessage);
process.exit(1);
});
}
console.log(parsedEnv);
}
However this throws "Type 'string' is not assignable to type 'number'", because i extend the Node.js process.env Type like this:
declare global {
namespace NodeJS {
interface ProcessEnv extends z.infer<typeof envSchema> {}
}
}
I can't overwrite "PORT" as in my zod schema the "PORT" is a number (which is parsed from a string) with a string because i just extended it with the result of the .env parsing. Is there any way i can extend the process.env AFTER i complete the parsing?