-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add environment variables validation (#147)
- Loading branch information
Showing
16 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { Installer } from "./index.js"; | ||
import path from "path"; | ||
import fs from "fs-extra"; | ||
import { PKG_ROOT } from "../consts.js"; | ||
|
||
export const envVariblesInstaller: Installer = async ({ | ||
projectDir, | ||
packages, | ||
}) => { | ||
const usingAuth = packages?.nextAuth.inUse; | ||
const usingPrisma = packages?.prisma.inUse; | ||
|
||
const envAssetDir = path.join(PKG_ROOT, "template/addons/env"); | ||
|
||
let envFile = ""; | ||
|
||
switch (true) { | ||
case usingAuth && usingPrisma: | ||
envFile = "env-prisma-auth.js"; | ||
break; | ||
case usingAuth: | ||
envFile = "env-auth.js"; | ||
break; | ||
case usingPrisma: | ||
envFile = "env-prisma.js"; | ||
break; | ||
} | ||
|
||
if (!envFile) return; | ||
|
||
const envSchemaSrc = path.join(envAssetDir, envFile); | ||
const envSchemaDest = path.join(projectDir, "src/server/env-schema.js"); | ||
|
||
await fs.copy(envSchemaSrc, envSchemaDest, { overwrite: true }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { z } = require("zod"); | ||
|
||
const envSchema = z.object({ | ||
NEXTAUTH_SECRET: z.string(), | ||
NEXTAUTH_URL: z.string().url(), | ||
GITHUB_ID: z.string(), | ||
GITHUB_SECRET: z.string(), | ||
}); | ||
|
||
module.exports.envSchema = envSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { z } = require("zod"); | ||
|
||
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(), | ||
GITHUB_ID: z.string(), | ||
GITHUB_SECRET: z.string(), | ||
}); | ||
|
||
module.exports.envSchema = envSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { z } = require("zod"); | ||
|
||
const envSchema = z.object({ | ||
DATABASE_URL: z.string().url(), | ||
NODE_ENV: z.enum(["development", "test", "production"]), | ||
}); | ||
|
||
module.exports.envSchema = envSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
const { env } = require("./src/server/env"); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
reactStrictMode: true, | ||
} | ||
}; | ||
|
||
module.exports = nextConfig | ||
module.exports = nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { z } = require("zod"); | ||
|
||
const envSchema = z.object({ | ||
// Specify your environment variables schema here | ||
}); | ||
|
||
module.exports.envSchema = envSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @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. | ||
*/ | ||
const { envSchema } = require("./env-schema"); | ||
|
||
const env = envSchema.safeParse(process.env); | ||
|
||
const formatErrors = ( | ||
/** @type {import('zod').ZodFormattedError<Map<string,string>,string>} */ | ||
errors, | ||
) => | ||
Object.entries(errors) | ||
.map(([name, value]) => { | ||
if (value && "_errors" in value) | ||
return `${name}: ${value._errors.join(", ")}\n`; | ||
}) | ||
.filter(Boolean); | ||
|
||
if (!env.success) { | ||
console.error( | ||
"❌ Invalid environment variables:\n", | ||
...formatErrors(env.error.format()), | ||
); | ||
process.exit(1); | ||
} | ||
|
||
module.exports.env = env.data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters