2.3.0 Dev Tools (#180)

* 2.3.0 - Added new Dev Tools app
This commit is contained in:
Giancarlo Buomprisco
2025-02-21 13:29:42 +07:00
committed by GitHub
parent 59dfc0ad91
commit c185bcfa11
36 changed files with 3747 additions and 67 deletions

View File

@@ -0,0 +1,721 @@
'use client';
import { Fragment, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { envVariables } from '@/app/variables/lib/env-variables-model';
import { EnvModeSelector } from '@/components/env-mode-selector';
import {
ChevronDown,
ChevronUp,
ChevronsUpDownIcon,
Copy,
Eye,
EyeOff,
InfoIcon,
} from 'lucide-react';
import { Alert, AlertDescription, AlertTitle } from '@kit/ui/alert';
import { Badge } from '@kit/ui/badge';
import { Button } from '@kit/ui/button';
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuTrigger,
} from '@kit/ui/dropdown-menu';
import { Heading } from '@kit/ui/heading';
import { If } from '@kit/ui/if';
import { Input } from '@kit/ui/input';
import { toast } from '@kit/ui/sonner';
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@kit/ui/tooltip';
import { cn } from '@kit/ui/utils';
import { AppEnvState, EnvVariableState } from '../lib/types';
export function AppEnvironmentVariablesManager({
state,
}: React.PropsWithChildren<{
state: AppEnvState;
}>) {
return (
<div className="flex flex-1 flex-col gap-y-4">
<Heading level={5}>Application: {state.appName}</Heading>
<div className={'flex flex-col space-y-4'}>
<EnvList appState={state} />
</div>
</div>
);
}
function EnvList({ appState }: { appState: AppEnvState }) {
const [expandedVars, setExpandedVars] = useState<Record<string, boolean>>({});
const [showValues, setShowValues] = useState<Record<string, boolean>>({});
const [search, setSearch] = useState('');
const searchParams = useSearchParams();
const secretVars = searchParams.get('secret') === 'true';
const publicVars = searchParams.get('public') === 'true';
const privateVars = searchParams.get('private') === 'true';
const overriddenVars = searchParams.get('overridden') === 'true';
const invalidVars = searchParams.get('invalid') === 'true';
const toggleExpanded = (key: string) => {
setExpandedVars((prev) => ({
...prev,
[key]: !prev[key],
}));
};
const toggleShowValue = (key: string) => {
setShowValues((prev) => ({
...prev,
[key]: !prev[key],
}));
};
const copyToClipboard = async (text: string) => {
try {
await navigator.clipboard.writeText(text);
} catch (err) {
console.error('Failed to copy:', err);
}
};
const renderValue = (value: string, isVisible: boolean) => {
if (!isVisible) {
return '••••••••';
}
return value || '(empty)';
};
const renderVariable = (varState: EnvVariableState) => {
const isExpanded = expandedVars[varState.key] ?? false;
const isClientBundledValue = varState.key.startsWith('NEXT_PUBLIC_');
// public variables are always visible
const isValueVisible = showValues[varState.key] ?? isClientBundledValue;
// grab model is it's a kit variable
const model = envVariables.find(
(variable) => variable.name === varState.key,
);
const allVariables = Object.values(appState.variables).reduce(
(acc, variable) => ({
...acc,
[variable.key]: variable.effectiveValue,
}),
{},
);
const validation = model?.validate
? model.validate({
value: varState.effectiveValue,
variables: allVariables,
mode: appState.mode,
})
: {
success: true,
error: undefined,
};
const canExpand = varState.definitions.length > 1 || !validation.success;
return (
<div key={varState.key} className="animate-in fade-in rounded-lg border">
<div className="p-4">
<div className="flex items-start justify-between">
<div className="flex-1 flex-col gap-y-1">
<div className="flex items-center gap-2">
<span className="font-mono text-sm font-semibold">
{varState.key}
</span>
{varState.isOverridden && (
<Badge variant="warning">Overridden</Badge>
)}
</div>
<If condition={model}>
{(model) => (
<div className="flex items-center gap-2">
<span className="text-muted-foreground text-xs font-normal">
{model.description}
</span>
</div>
)}
</If>
<div className="mt-2 flex items-center gap-2">
<div className="bg-muted text-muted-foreground flex-1 rounded px-2 py-2 font-mono text-xs">
{renderValue(varState.effectiveValue, isValueVisible)}
</div>
<If condition={!isClientBundledValue}>
<Button
variant="ghost"
size={'icon'}
onClick={() => toggleShowValue(varState.key)}
>
{isValueVisible ? <EyeOff size={16} /> : <Eye size={16} />}
</Button>
</If>
<Button
variant="ghost"
onClick={() => copyToClipboard(varState.effectiveValue)}
size={'icon'}
>
<Copy size={16} />
</Button>
</div>
</div>
{canExpand && (
<Button
size={'icon'}
variant="ghost"
className="ml-4 rounded p-1 hover:bg-gray-100"
onClick={() => toggleExpanded(varState.key)}
>
{isExpanded ? (
<ChevronUp className="h-4 w-4" />
) : (
<ChevronDown className="h-4 w-4" />
)}
</Button>
)}
</div>
<div className="mt-2 flex gap-x-2">
<Badge
variant="outline"
className={cn({
'text-orange-500': !isClientBundledValue,
'text-green-500': isClientBundledValue,
})}
>
{isClientBundledValue ? `Public variable` : `Private variable`}
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<InfoIcon className="ml-2 h-3 w-3" />
</TooltipTrigger>
<TooltipContent>
{isClientBundledValue
? `This variable will be bundled into the client side. If this is a private variable, do not use "NEXT_PUBLIC".`
: `This variable is private and will not be bundled client side, so you cannot access it from React components rendered client side`}
</TooltipContent>
</Tooltip>
</TooltipProvider>
</Badge>
<If condition={model?.secret}>
<Badge variant="outline" className={'text-destructive'}>
Secret Variable
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<InfoIcon className="ml-2 h-3 w-3" />
</TooltipTrigger>
<TooltipContent>
This is a secret key. Keep it safe!
</TooltipContent>
</Tooltip>
</TooltipProvider>
</Badge>
</If>
<Badge
variant={'outline'}
className={cn({
'text-destructive':
varState.effectiveSource === '.env.production',
})}
>
{varState.effectiveSource}
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<InfoIcon className="ml-2 h-3 w-3" />
</TooltipTrigger>
<TooltipContent>
{varState.effectiveSource === '.env.local'
? `These variables are specific to this machine and are not committed`
: varState.effectiveSource === '.env.development'
? `These variables are only being used during development`
: varState.effectiveSource === '.env'
? `These variables are shared under all modes`
: `These variables are only used in production mode`}
</TooltipContent>
</Tooltip>
</TooltipProvider>
</Badge>
<If condition={varState.isOverridden}>
<Badge variant="warning">
Overridden in {varState.effectiveSource}
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<InfoIcon className="ml-2 h-3 w-3" />
</TooltipTrigger>
<TooltipContent>
This variable was overridden by a variable in{' '}
{varState.effectiveSource}
</TooltipContent>
</Tooltip>
</TooltipProvider>
</Badge>
</If>
<If condition={!validation.success}>
<Badge variant="destructive">
Invalid Value
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<InfoIcon className="ml-2 h-3 w-3" />
</TooltipTrigger>
<TooltipContent>
This variable has an invalid value. Drop down to view the
errors.
</TooltipContent>
</Tooltip>
</TooltipProvider>
</Badge>
</If>
</div>
</div>
{isExpanded && canExpand && (
<div className="flex flex-col gap-y-2 border-t bg-gray-50 p-4">
<If condition={!validation.success}>
<div className={'flex flex-col space-y-2'}>
<Heading level={6} className="Errors">
Errors
</Heading>
<Alert variant="destructive">
<AlertTitle>Invalid Value</AlertTitle>
<AlertDescription>
The value for {varState.key} is invalid:
<pre>
<code>{JSON.stringify(validation, null, 2)}</code>
</pre>
</AlertDescription>
</Alert>
</div>
</If>
<If condition={varState.definitions.length > 1}>
<div className={'flex flex-col space-y-2'}>
<Heading level={6} className="text-sm font-medium">
Override Chain
</Heading>
<div className="space-y-2">
{varState.definitions.map((def) => (
<div
key={`${def.key}-${def.source}`}
className="flex items-center gap-2"
>
<Badge
variant={'outline'}
className={cn({
'text-destructive': def.source === '.env.production',
})}
>
{def.source}
</Badge>
<div className="font-mono text-sm">
{renderValue(def.value, isValueVisible)}
</div>
</div>
))}
</div>
</div>
</If>
</div>
)}
</div>
);
};
const filterVariable = (varState: EnvVariableState) => {
const model = envVariables.find(
(variable) => variable.name === varState.key,
);
if (
!search &&
!secretVars &&
!publicVars &&
!privateVars &&
!invalidVars &&
!overriddenVars
) {
return true;
}
const isSecret = model?.secret;
const isPublic = varState.key.startsWith('NEXT_PUBLIC_');
const isPrivate = !isPublic;
const isInSearch = search
? varState.key.toLowerCase().includes(search.toLowerCase())
: true;
if (isPublic && publicVars && isInSearch) {
return true;
}
if (isSecret && secretVars && isInSearch) {
return true;
}
if (isPrivate && privateVars && isInSearch) {
return true;
}
if (overriddenVars && varState.isOverridden && isInSearch) {
return true;
}
if (invalidVars) {
const allVariables = Object.values(appState.variables).reduce(
(acc, variable) => ({
...acc,
[variable.key]: variable.effectiveValue,
}),
{},
);
const hasError =
model && model.validate
? !model.validate({
value: varState.effectiveValue,
variables: allVariables,
mode: appState.mode,
}).success
: false;
if (hasError && isInSearch) return true;
}
return false;
};
const groups = Object.values(appState.variables)
.filter(filterVariable)
.reduce(
(acc, variable) => {
const group = acc.find((group) => group.category === variable.category);
if (!group) {
acc.push({
category: variable.category,
variables: [variable],
});
} else {
group.variables.push(variable);
}
return acc;
},
[] as Array<{ category: string; variables: Array<EnvVariableState> }>,
);
return (
<div className="flex flex-col gap-y-8">
<div className="flex items-center">
<div className="flex w-full space-x-2">
<div>
<EnvModeSelector mode={appState.mode} />
</div>
<div>
<FilterSwitcher
filters={{
secret: secretVars,
public: publicVars,
overridden: overriddenVars,
private: privateVars,
invalid: invalidVars,
}}
/>
</div>
<Input
className={'w-full'}
placeholder="Search variables"
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="secondary"
onClick={() => {
const report = createReportFromEnvState(appState);
const promise = copyToClipboard(report);
toast.promise(promise, {
loading: 'Copying report...',
success:
'Report copied to clipboard. Please paste it in your ticket.',
error: 'Failed to copy report to clipboard',
});
}}
>
Copy to Clipboard
</Button>
</TooltipTrigger>
<TooltipContent>
Create a report from the environment variables. Useful for
creating support tickets.
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
</div>
<div className="flex flex-col gap-1">
<Summary appState={appState} />
{groups.map((group) => (
<div
key={group.category}
className="flex flex-col gap-y-2.5 border-b border-dashed py-8 last:border-b-0"
>
<div>
<span className={'text-sm font-bold uppercase'}>
{group.category}
</span>
</div>
<div className="flex flex-col space-y-4">
{group.variables.map((item) => {
return (
<Fragment key={item.key}>{renderVariable(item)}</Fragment>
);
})}
</div>
</div>
))}
<If condition={groups.length === 0}>
<div className="flex h-full flex-1 flex-col items-center justify-center gap-y-4 py-16">
<div className="text-muted-foreground text-sm">
No variables found
</div>
</div>
</If>
</div>
</div>
);
}
function createReportFromEnvState(state: AppEnvState) {
let report = ``;
for (const key in state.variables) {
const variable = state.variables[key];
const variableReport = `${key}: ${JSON.stringify(variable, null, 2)}`;
``;
report += variableReport + '\n';
}
return report;
}
function FilterSwitcher(props: {
filters: {
secret: boolean;
public: boolean;
overridden: boolean;
private: boolean;
invalid: boolean;
};
}) {
const router = useRouter();
const secretVars = props.filters.secret;
const publicVars = props.filters.public;
const overriddenVars = props.filters.overridden;
const privateVars = props.filters.private;
const invalidVars = props.filters.invalid;
const handleFilterChange = (key: string, value: boolean) => {
const searchParams = new URLSearchParams(window.location.search);
const path = window.location.pathname;
if (key === 'all' && value) {
searchParams.delete('secret');
searchParams.delete('public');
searchParams.delete('overridden');
searchParams.delete('private');
searchParams.delete('invalid');
} else {
if (!value) {
searchParams.delete(key);
} else {
searchParams.set(key, 'true');
}
}
router.push(`${path}?${searchParams.toString()}`);
};
const buttonLabel = () => {
const filters = [];
if (secretVars) filters.push('Secret');
if (publicVars) filters.push('Public');
if (overriddenVars) filters.push('Overridden');
if (privateVars) filters.push('Private');
if (invalidVars) filters.push('Invalid');
if (filters.length === 0) return 'Filter variables';
return filters.join(', ');
};
const allSelected =
!secretVars && !publicVars && !overriddenVars && !invalidVars;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="font-normal">
{buttonLabel()}
<ChevronsUpDownIcon className="text-muted-foreground ml-1 h-3 w-3" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuCheckboxItem
checked={allSelected}
onCheckedChange={() => {
handleFilterChange('all', true);
}}
>
All
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={secretVars}
onCheckedChange={() => {
handleFilterChange('secret', !secretVars);
}}
>
Secret
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={privateVars}
onCheckedChange={() => {
handleFilterChange('private', !privateVars);
}}
>
Private
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={publicVars}
onCheckedChange={() => {
handleFilterChange('public', !publicVars);
}}
>
Public
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={invalidVars}
onCheckedChange={() => {
handleFilterChange('invalid', !invalidVars);
}}
>
Invalid
</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem
checked={overriddenVars}
onCheckedChange={() => {
handleFilterChange('overridden', !overriddenVars);
}}
>
Overridden
</DropdownMenuCheckboxItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
function Summary({ appState }: { appState: AppEnvState }) {
const varsArray = Object.values(appState.variables);
const overridden = varsArray.filter((variable) => variable.isOverridden);
const allVariables = varsArray.reduce(
(acc, variable) => ({
...acc,
[variable.key]: variable.effectiveValue,
}),
{},
);
const errors = varsArray.filter((variable) => {
const model = envVariables.find((v) => variable.key === v.name);
const validation =
model && model.validate
? model.validate({
value: variable.effectiveValue,
variables: allVariables,
mode: appState.mode,
})
: {
success: true,
};
return !validation.success;
});
return (
<div className="flex flex-col space-y-4">
<div className="flex items-center gap-x-2">
<Badge variant={errors.length === 0 ? 'success' : 'destructive'}>
{errors.length} Errors
</Badge>
<Badge variant={overridden.length === 0 ? 'success' : 'warning'}>
{overridden.length} Overridden Variables
</Badge>
</div>
</div>
);
}

View File

@@ -0,0 +1,244 @@
import 'server-only';
import { envVariables } from '@/app/variables/lib/env-variables-model';
import fs from 'fs/promises';
import path from 'path';
import {
AppEnvState,
EnvFileInfo,
EnvMode,
EnvVariableState,
ScanOptions,
} from './types';
// Define precedence order for each mode
const ENV_FILE_PRECEDENCE: Record<EnvMode, string[]> = {
development: [
'.env',
'.env.development',
'.env.local',
'.env.development.local',
],
production: [
'.env',
'.env.production',
'.env.local',
'.env.production.local',
],
};
function getSourcePrecedence(source: string, mode: EnvMode): number {
return ENV_FILE_PRECEDENCE[mode].indexOf(source);
}
export async function scanMonorepoEnv(
options: ScanOptions,
): Promise<EnvFileInfo[]> {
const {
rootDir = path.resolve(process.cwd(), '../..'),
apps = ['web'],
mode,
} = options;
const envTypes = ENV_FILE_PRECEDENCE[mode];
const appsDir = path.join(rootDir, 'apps');
const results: EnvFileInfo[] = [];
try {
const appDirs = await fs.readdir(appsDir);
for (const appName of appDirs) {
if (apps.length > 0 && !apps.includes(appName)) {
continue;
}
const appDir = path.join(appsDir, appName);
const stat = await fs.stat(appDir);
if (!stat.isDirectory()) {
continue;
}
const appInfo: EnvFileInfo = {
appName,
filePath: appDir,
variables: [],
};
for (const envType of envTypes) {
const envPath = path.join(appDir, envType);
try {
const content = await fs.readFile(envPath, 'utf-8');
const vars = parseEnvFile(content, envType);
appInfo.variables.push(...vars);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
console.warn(`Error reading ${envPath}:`, error);
}
}
}
if (appInfo.variables.length > 0) {
results.push(appInfo);
}
}
} catch (error) {
console.error('Error scanning monorepo:', error);
throw error;
}
return results;
}
function parseEnvFile(content: string, source: string) {
const variables: Array<{ key: string; value: string; source: string }> = [];
const lines = content.split('\n');
for (const line of lines) {
// Skip comments and empty lines
if (line.trim().startsWith('#') || !line.trim()) {
continue;
}
// Match KEY=VALUE pattern, handling quotes
const match = line.match(/^([^=]+)=(.*)$/);
if (match) {
const [, key = '', rawValue] = match;
let value = rawValue ?? '';
// Remove quotes if present
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
// Handle escaped quotes within the value
value = value
.replace(/\\"/g, '"')
.replace(/\\'/g, "'")
.replace(/\\\\/g, '\\');
variables.push({
key: key.trim(),
value: value.trim(),
source,
});
}
}
return variables;
}
export function processEnvDefinitions(
envInfo: EnvFileInfo,
mode: EnvMode,
): AppEnvState {
const variableMap: Record<string, EnvVariableState> = {};
// First pass: Collect all definitions
for (const variable of envInfo.variables) {
if (!variable) {
continue;
}
const model = envVariables.find((v) => variable.key === v.name);
if (!variableMap[variable.key]) {
variableMap[variable.key] = {
key: variable.key,
definitions: [],
effectiveValue: variable.value,
effectiveSource: variable.source,
isOverridden: false,
category: model ? model.category : 'Custom',
};
}
const varState = variableMap[variable.key];
if (!varState) {
continue;
}
varState.definitions.push({
key: variable.key,
value: variable.value,
source: variable.source,
});
}
// Second pass: Determine effective values and override status
for (const key in variableMap) {
const varState = variableMap[key];
if (!varState) {
continue;
}
// Sort definitions by mode-specific precedence
varState.definitions.sort(
(a, b) =>
getSourcePrecedence(a.source, mode) -
getSourcePrecedence(b.source, mode),
);
if (varState.definitions.length > 1) {
const lastDef = varState.definitions[varState.definitions.length - 1];
if (!lastDef) {
continue;
}
const highestPrecedence = getSourcePrecedence(lastDef.source, mode);
varState.isOverridden = true;
varState.effectiveValue = lastDef.value;
varState.effectiveSource = lastDef.source;
// Check for conflicts at highest precedence
const conflictingDefs = varState.definitions.filter(
(def) => getSourcePrecedence(def.source, mode) === highestPrecedence,
);
if (conflictingDefs.length > 1) {
varState.effectiveSource = `${varState.effectiveSource}`;
}
}
}
return {
appName: envInfo.appName,
filePath: envInfo.filePath,
mode,
variables: variableMap,
};
}
export async function getEnvState(
options: ScanOptions,
): Promise<AppEnvState[]> {
const envInfos = await scanMonorepoEnv(options);
return envInfos.map((info) => processEnvDefinitions(info, options.mode));
}
// Utility function to get list of env files for current mode
export function getEnvFilesForMode(mode: EnvMode): string[] {
return ENV_FILE_PRECEDENCE[mode];
}
export async function getVariable(key: string, mode: EnvMode) {
// Get the processed environment state for all apps (you can limit to 'web' via options)
const envStates = await getEnvState({ mode, apps: ['web'] });
// Find the state for the "web" app.
const webState = envStates.find((state) => state.appName === 'web');
// Return the effectiveValue based on override status.
return webState?.variables[key]?.effectiveValue ?? '';
}

View File

@@ -0,0 +1,770 @@
import { EnvMode } from '@/app/variables/lib/types';
import { z } from 'zod';
export type EnvVariableModel = {
name: string;
description: string;
secret?: boolean;
category: string;
test?: (value: string) => Promise<boolean>;
validate?: (props: {
value: string;
variables: Record<string, string>;
mode: EnvMode;
}) => z.SafeParseReturnType<unknown, unknown>;
};
export const envVariables: EnvVariableModel[] = [
{
name: 'NEXT_PUBLIC_SITE_URL',
description:
'The URL of your site, used for generating absolute URLs. Must include the protocol.',
category: 'Site Configuration',
validate: ({ value, mode }) => {
if (mode === 'development') {
return z
.string()
.url({
message: `The NEXT_PUBLIC_SITE_URL variable must be a valid URL`,
})
.safeParse(value);
}
return z
.string()
.url({
message: `The NEXT_PUBLIC_SITE_URL variable must be a valid URL`,
})
.startsWith(
'https',
`The NEXT_PUBLIC_SITE_URL variable must start with https`,
)
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_PRODUCT_NAME',
description:
"Your product's name, used consistently across the application interface.",
category: 'Site Configuration',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_PRODUCT_NAME variable must be at least 1 character`,
)
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_SITE_TITLE',
description:
"The site's title tag content, crucial for SEO and browser display.",
category: 'Site Configuration',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_SITE_TITLE variable must be at least 1 character`,
)
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_SITE_DESCRIPTION',
description:
"Your site's meta description, important for SEO optimization.",
category: 'Site Configuration',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_SITE_DESCRIPTION variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_DEFAULT_LOCALE',
description: 'Sets the default language for your application.',
category: 'Localization',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_DEFAULT_LOCALE variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_AUTH_PASSWORD',
description: 'Enables or disables password-based authentication.',
category: 'Authentication',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_AUTH_MAGIC_LINK',
description: 'Enables or disables magic link authentication.',
category: 'Authentication',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_CAPTCHA_SITE_KEY',
description: 'Your Cloudflare Captcha site key for form protection.',
category: 'Security',
validate: ({ value }) => {
return z.string().optional().safeParse(value);
},
},
{
name: 'CAPTCHA_SECRET_TOKEN',
description:
'Your Cloudflare Captcha secret token for backend verification.',
category: 'Security',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(
1,
`The CAPTCHA_SECRET_TOKEN variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_USER_NAVIGATION_STYLE',
description:
'Controls user navigation layout. Options: sidebar, header, or custom.',
category: 'Navigation',
validate: ({ value }) => {
return z
.enum(['sidebar', 'header', 'custom'])
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_HOME_SIDEBAR_COLLAPSED',
description: 'Sets the default state of the home sidebar.',
category: 'Navigation',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_TEAM_NAVIGATION_STYLE',
description:
'Controls team navigation layout. Options: sidebar, header, or custom.',
category: 'Navigation',
validate: ({ value }) => {
return z
.enum(['sidebar', 'header', 'custom'])
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_TEAM_SIDEBAR_COLLAPSED',
description: 'Sets the default state of the team sidebar.',
category: 'Navigation',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_SIDEBAR_COLLAPSIBLE_STYLE',
description:
'Defines sidebar collapse behavior. Options: offscreen, icon, or none.',
category: 'Navigation',
validate: ({ value }) => {
return z.enum(['offscreen', 'icon', 'none']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_DEFAULT_THEME_MODE',
description:
'Controls the default theme appearance. Options: light, dark, or system.',
category: 'Theme',
validate: ({ value }) => {
return z.enum(['light', 'dark', 'system']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_THEME_TOGGLE',
description: 'Controls visibility of the theme toggle feature.',
category: 'Theme',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_SIDEBAR_TRIGGER',
description: 'Controls visibility of the sidebar trigger feature.',
category: 'Navigation',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_DELETION',
description: 'Allows users to delete their personal accounts.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_BILLING',
description: 'Enables billing features for personal accounts.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS',
description: 'Master switch for team account functionality.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_CREATION',
description: 'Controls ability to create new team accounts.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_DELETION',
description: 'Allows team account deletion.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_BILLING',
description: 'Enables billing features for team accounts.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_NOTIFICATIONS',
description: 'Controls the notification system.',
category: 'Notifications',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_REALTIME_NOTIFICATIONS',
description: 'Enables real-time notifications using Supabase Realtime.',
category: 'Notifications',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_SUPABASE_URL',
description: 'Your Supabase project URL.',
category: 'Supabase',
validate: ({ value, mode }) => {
if (mode === 'development') {
return z
.string()
.url({
message: `The NEXT_PUBLIC_SUPABASE_URL variable must be a valid URL`,
})
.safeParse(value);
}
return z
.string()
.url({
message: `The NEXT_PUBLIC_SUPABASE_URL variable must be a valid URL`,
})
.startsWith(
'https',
`The NEXT_PUBLIC_SUPABASE_URL variable must start with https`,
)
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_SUPABASE_ANON_KEY',
description: 'Your Supabase anonymous API key.',
category: 'Supabase',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_SUPABASE_ANON_KEY variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'SUPABASE_SERVICE_ROLE_KEY',
description: 'Your Supabase service role key (keep this secret!).',
category: 'Supabase',
secret: true,
validate: ({ value, variables }) => {
return z
.string()
.min(
1,
`The SUPABASE_SERVICE_ROLE_KEY variable must be at least 1 character`,
)
.optional()
.refine(
(value) => {
return value !== variables['NEXT_PUBLIC_SUPABASE_ANON_KEY'];
},
{
message: `The SUPABASE_SERVICE_ROLE_KEY variable must be different from NEXT_PUBLIC_SUPABASE_ANON_KEY`,
},
)
.safeParse(value);
},
},
{
name: 'SUPABASE_DB_WEBHOOK_SECRET',
description: 'Secret key for Supabase webhook verification.',
category: 'Supabase',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(
1,
`The SUPABASE_DB_WEBHOOK_SECRET variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_BILLING_PROVIDER',
description:
'Your chosen billing provider. Options: stripe or lemon-squeezy.',
category: 'Billing',
validate: ({ value }) => {
return z.enum(['stripe', 'lemon-squeezy']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY',
description: 'Your Stripe publishable key.',
category: 'Billing',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'STRIPE_SECRET_KEY',
description: 'Your Stripe secret key.',
category: 'Billing',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(1, `The STRIPE_SECRET_KEY variable must be at least 1 character`)
.optional()
.safeParse(value);
},
},
{
name: 'STRIPE_WEBHOOK_SECRET',
description: 'Your Stripe webhook secret.',
category: 'Billing',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(
1,
`The STRIPE_WEBHOOK_SECRET variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'LEMON_SQUEEZY_SECRET_KEY',
description: 'Your Lemon Squeezy secret key.',
category: 'Billing',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(
1,
`The LEMON_SQUEEZY_SECRET_KEY variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'LEMON_SQUEEZY_STORE_ID',
description: 'Your Lemon Squeezy store ID.',
category: 'Billing',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The LEMON_SQUEEZY_STORE_ID variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'LEMON_SQUEEZY_SIGNING_SECRET',
description: 'Your Lemon Squeezy signing secret.',
category: 'Billing',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(
1,
`The LEMON_SQUEEZY_SIGNING_SECRET variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'MAILER_PROVIDER',
description: 'Your email service provider. Options: nodemailer or resend.',
category: 'Email',
validate: ({ value }) => {
return z.enum(['nodemailer', 'resend']).optional().safeParse(value);
},
},
{
name: 'EMAIL_SENDER',
description: 'Default sender email address.',
category: 'Email',
validate: ({ value }) => {
return z
.string()
.min(1, `The EMAIL_SENDER variable must be at least 1 character`)
.safeParse(value);
},
},
{
name: 'CONTACT_EMAIL',
description: 'Email address for contact form submissions.',
category: 'Email',
validate: ({ value }) => {
return z
.string()
.email()
.min(1, `The CONTACT_EMAIL variable must be at least 1 character`)
.safeParse(value);
},
},
{
name: 'RESEND_API_KEY',
description: 'Your Resend API key.',
category: 'Email',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(1, `The RESEND_API_KEY variable must be at least 1 character`)
.safeParse(value);
},
},
{
name: 'EMAIL_HOST',
description: 'SMTP host for Nodemailer configuration.',
category: 'Email',
validate: ({ value }) => {
return z.string().safeParse(value);
},
},
{
name: 'EMAIL_PORT',
description: 'SMTP port for Nodemailer configuration.',
category: 'Email',
validate: ({ value }) => {
return z.coerce
.number()
.min(1, `The EMAIL_PORT variable must be at least 1 character`)
.max(65535, `The EMAIL_PORT variable must be at most 65535`)
.safeParse(value);
},
},
{
name: 'EMAIL_USER',
description: 'SMTP user for Nodemailer configuration.',
category: 'Email',
validate: ({ value }) => {
return z
.string()
.min(1, `The EMAIL_USER variable must be at least 1 character`)
.safeParse(value);
},
},
{
name: 'EMAIL_PASSWORD',
description: 'SMTP password for Nodemailer configuration.',
category: 'Email',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(1, `The EMAIL_PASSWORD variable must be at least 1 character`)
.safeParse(value);
},
},
{
name: 'EMAIL_TLS',
description: 'Whether to use TLS for SMTP connection.',
category: 'Email',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'CMS_CLIENT',
description: 'Your chosen CMS system. Options: wordpress or keystatic.',
category: 'CMS',
validate: ({ value }) => {
return z.enum(['wordpress', 'keystatic']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND',
description: 'Your Keystatic storage kind. Options: local, cloud, github.',
category: 'CMS',
validate: ({ value }) => {
return z.enum(['local', 'cloud', 'github']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_KEYSTATIC_STORAGE_REPO',
description: 'Your Keystatic storage repo.',
category: 'CMS',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_KEYSTATIC_STORAGE_REPO variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'KEYSTATIC_GITHUB_TOKEN',
description: 'Your Keystatic GitHub token.',
category: 'CMS',
secret: true,
validate: ({ value }) => {
return z
.string()
.min(
1,
`The KEYSTATIC_GITHUB_TOKEN variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'KEYSTATIC_PATH_PREFIX',
description: 'Your Keystatic path prefix.',
category: 'CMS',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The KEYSTATIC_PATH_PREFIX variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH',
description: 'Your Keystatic content path.',
category: 'CMS',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_KEYSTATIC_CONTENT_PATH variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'WORDPRESS_API_URL',
description: 'WordPress API URL when using WordPress as CMS.',
category: 'CMS',
validate: ({ value }) => {
return z
.string()
.url({
message: `The WORDPRESS_API_URL variable must be a valid URL`,
})
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_LOCALES_PATH',
description: 'The path to your locales folder.',
category: 'Localization',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_LOCALES_PATH variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_LANGUAGE_PRIORITY',
description: 'The priority setting as to how infer the language.',
category: 'Localization',
validate: ({ value }) => {
return z.enum(['user', 'application']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_ENABLE_VERSION_UPDATER',
description:
'Enables the version updater to poll the latest version and notify the user.',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: `ENABLE_REACT_COMPILER`,
description: 'Enables the React compiler [experimental]',
category: 'Features',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_MONITORING_PROVIDER',
description: 'The monitoring provider to use.',
category: 'Monitoring',
validate: ({ value }) => {
return z.enum(['baselime', 'sentry']).optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_BASELIME_KEY',
description: 'The Baselime key to use.',
category: 'Monitoring',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_BASELIME_KEY variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'STRIPE_ENABLE_TRIAL_WITHOUT_CC',
description: 'Enables trial plans without credit card.',
category: 'Billing',
validate: ({ value }) => {
return z.coerce.boolean().optional().safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_VERSION_UPDATER_REFETCH_INTERVAL_SECONDS',
description: 'The interval in seconds to check for updates.',
category: 'Features',
validate: ({ value }) => {
return z.coerce
.number()
.min(
1,
`The NEXT_PUBLIC_VERSION_UPDATER_REFETCH_INTERVAL_SECONDS variable must be at least 1 character`,
)
.max(
86400,
`The NEXT_PUBLIC_VERSION_UPDATER_REFETCH_INTERVAL_SECONDS variable must be at most 86400`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_THEME_COLOR',
description: 'The default theme color.',
category: 'Theme',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_THEME_COLOR variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
{
name: 'NEXT_PUBLIC_THEME_COLOR_DARK',
description: 'The default theme color for dark mode.',
category: 'Theme',
validate: ({ value }) => {
return z
.string()
.min(
1,
`The NEXT_PUBLIC_THEME_COLOR_DARK variable must be at least 1 character`,
)
.optional()
.safeParse(value);
},
},
];

View File

@@ -0,0 +1,40 @@
export type EnvMode = 'development' | 'production';
export type ScanOptions = {
apps?: string[];
rootDir?: string;
mode: EnvMode;
};
export type EnvDefinition = {
key: string;
value: string;
source: string;
};
export type EnvVariableState = {
key: string;
category: string;
definitions: EnvDefinition[];
effectiveValue: string;
isOverridden: boolean;
effectiveSource: string;
};
export type AppEnvState = {
appName: string;
filePath: string;
mode: EnvMode;
variables: Record<string, EnvVariableState>;
};
export type EnvFileInfo = {
appName: string;
filePath: string;
variables: Array<{
key: string;
value: string;
source: string;
}>;
};

View File

@@ -0,0 +1,55 @@
import { use } from 'react';
import {
processEnvDefinitions,
scanMonorepoEnv,
} from '@/app/variables/lib/env-scanner';
import { EnvMode } from '@/app/variables/lib/types';
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
import { Page, PageBody, PageHeader } from '@kit/ui/page';
import { AppEnvironmentVariablesManager } from './components/app-environment-variables-manager';
type VariablesPageProps = {
searchParams: Promise<{ mode?: EnvMode }>;
};
export const metadata = {
title: 'Environment Variables',
};
export default function VariablesPage({ searchParams }: VariablesPageProps) {
const { mode = 'development' } = use(searchParams);
const apps = use(scanMonorepoEnv({ mode }));
return (
<Page style={'custom'}>
<PageHeader
displaySidebarTrigger={false}
description={
<AppBreadcrumbs
values={{
variables: 'Environment Variables',
}}
/>
}
/>
<PageBody>
<div className={'flex flex-col space-y-4 pb-16'}>
{apps.map((app) => {
const appEnvState = processEnvDefinitions(app, mode);
return (
<AppEnvironmentVariablesManager
key={app.appName}
state={appEnvState}
/>
);
})}
</div>
</PageBody>
</Page>
);
}