Skip to content

Commit

Permalink
feat: create env.mjs with validation (#8)
Browse files Browse the repository at this point in the history
feat: create env.mjs with validation

feat: update config files, add git sha env var
  • Loading branch information
altaywtf authored Dec 10, 2023
1 parent ff25871 commit c9ca0ec
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
'import/no-default-export': 'off',
'import/no-duplicates': 'error',
'import/order': 'off',
'no-console': 'off',
},
settings: {
'import/resolver': {
Expand Down
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '@/components/theme-provider/styles.css';

export const metadata: Metadata = {
description: 'Welcome, welcome, welcome!',
title: 'pod1612',
title: 'beecast',
};

export default function RootLayout({ children }: PropsWithChildren) {
Expand Down
5 changes: 3 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from '@/env.mjs';
import { Button, Container, Flex, Heading, Text } from '@radix-ui/themes';

export default function Page() {
Expand All @@ -8,8 +9,8 @@ export default function Page() {
}}
>
<Flex direction="column" gap="2">
<Heading>Hello World</Heading>
<Text>Hello from Radix Themes 🤓</Text>
<Heading>Hello from Radix Themes 🤓</Heading>
<Text color="gray">Release: {env.GIT_SHA}</Text>
<Button>Click me</Button>
</Flex>
</Container>
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @ts-check

import { z } from 'zod';

const getGitSha = () => {
if (process.env.VERCEL_GIT_COMMIT_SHA) {
return process.env.VERCEL_GIT_COMMIT_SHA;
}

if (process.env.GITHUB_SHA) {
return process.env.GITHUB_SHA;
}

return 'dev';
};

const envSchema = z.object({
GIT_SHA: z.string().min(1).default(getGitSha()),
});

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

if (!envValidation.success) {
const message = '❌ Invalid environment variables';
console.error(message, JSON.stringify(envValidation.error.format(), null, 2));
throw new Error(envValidation.error.message);
}

export const env = envValidation.data;
File renamed without changes.
4 changes: 3 additions & 1 deletion next.config.js → next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import './env.mjs';

/** @type {import('next').NextConfig} */
const nextConfig = {};

module.exports = nextConfig;
export default nextConfig;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"next": "14.0.4",
"next-themes": "1.0.0-beta.0",
"react": "^18",
"react-dom": "^18"
"react-dom": "^18",
"zod": "^3.22.4"
},
"devDependencies": {
"@commitlint/cli": "^18.4.3",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
"include": [
".eslintrc.cjs",
".next/types/**/*.ts",
".storybook/**/*",
"*.config.js",
".storybook/**/*.ts",
".storybook/**/*.tsx",
"*.config.cjs",
"*.config.mjs",
"**/*.ts",
"**/*.tsx",
"env.mjs",
"next-env.d.ts"
],
"exclude": ["node_modules"]
Expand Down

0 comments on commit c9ca0ec

Please sign in to comment.