Skip to content

Commit

Permalink
feat: migrate env + next config to esm (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 17, 2022
1 parent 4d7a3d0 commit 59f8ad4
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/installers/envVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export const envVariblesInstaller: Installer = async ({

switch (true) {
case usingAuth && usingPrisma:
envFile = "env-prisma-auth.js";
envFile = "env-prisma-auth.mjs";
break;
case usingAuth:
envFile = "env-auth.js";
envFile = "env-auth.mjs";
break;
case usingPrisma:
envFile = "env-prisma.js";
envFile = "env-prisma.mjs";
break;
}

if (!envFile) return;

const envSchemaSrc = path.join(envAssetDir, envFile);
const envSchemaDest = path.join(projectDir, "src/server/env-schema.js");
const envSchemaDest = path.join(projectDir, "src/server/env-schema.mjs");

await fs.copy(envSchemaSrc, envSchemaDest, { overwrite: true });
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const { z } = require("zod");
import { z } from "zod";

const envSchema = z.object({
export const envSchema = z.object({
NEXTAUTH_SECRET: z.string(),
NEXTAUTH_URL: z.string().url(),
DISCORD_CLIENT_ID: z.string(),
DISCORD_CLIENT_SECRET: z.string(),
});

module.exports.envSchema = envSchema;
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { z } = require("zod");
import { z } from "zod";

const envSchema = z.object({
export const envSchema = z.object({
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
NEXTAUTH_SECRET: z.string(),
NEXTAUTH_URL: z.string().url(),
DISCORD_CLIENT_ID: z.string(),
DISCORD_CLIENT_SECRET: z.string(),
});

module.exports.envSchema = envSchema;
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { z } = require("zod");
import { z } from "zod";

const envSchema = z.object({
export const envSchema = z.object({
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
});

module.exports.envSchema = envSchema;
2 changes: 1 addition & 1 deletion template/addons/next-auth/api-handler-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CredentialsProvider from "next-auth/providers/credentials";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "../../../server/db/client";
import { env } from "../../../server/env";
import { env } from "../../../server/env.mjs";

export const authOptions: NextAuthOptions = {
// Include user.id on session
Expand Down
2 changes: 1 addition & 1 deletion template/addons/next-auth/api-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
import CredentialsProvider from "next-auth/providers/credentials";
import { env } from "../../../server/env";
import { env } from "../../../server/env.mjs";

export const authOptions: NextAuthOptions = {
// Include user.id on session
Expand Down
2 changes: 1 addition & 1 deletion template/addons/prisma/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/server/db/client.ts
import { PrismaClient } from "@prisma/client";
import { env } from "../env";
import { env } from "../env.mjs";

declare global {
var prisma: PrismaClient | undefined;
Expand Down
8 changes: 0 additions & 8 deletions template/base/next.config.js

This file was deleted.

17 changes: 17 additions & 0 deletions template/base/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { env } from "./src/server/env.mjs";

/**
* Don't be scared of the generics here.
* All they do is to give us autocompletion when using this.
*
* @template {import('next').NextConfig} T
* @param {T} config - A generic parameter that flows through to the return type
* @constraint {{import('next').NextConfig}}
*/
function defineNextConfig(config) {
return config;
}

export default defineNextConfig({
reactStrictMode: true,
});
7 changes: 0 additions & 7 deletions template/base/src/server/env-schema.js

This file was deleted.

5 changes: 5 additions & 0 deletions template/base/src/server/env-schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from "zod";

export const envSchema = z.object({
// Specify your environment variables schema here
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @ts-check
/**
* This file is included in `/next.config.js` which ensures the app isn't built with invalid env vars.
* It has to be a `.js`-file to be imported there.
* This file is included in `/next.config.mjs` which ensures the app isn't built with invalid env vars.
* It has to be a `.mjs`-file to be imported there.
*/
const { envSchema } = require("./env-schema");
import { envSchema } from "./env-schema.mjs";

const env = envSchema.safeParse(process.env);
const _env = envSchema.safeParse(process.env);

const formatErrors = (
/** @type {import('zod').ZodFormattedError<Map<string,string>,string>} */
Expand All @@ -18,12 +18,12 @@ const formatErrors = (
})
.filter(Boolean);

if (!env.success) {
if (!_env.success) {
console.error(
"❌ Invalid environment variables:\n",
...formatErrors(env.error.format()),
...formatErrors(_env.error.format()),
);
process.exit(1);
}

module.exports.env = env.data;
export const env = _env.data;

0 comments on commit 59f8ad4

Please sign in to comment.