Skip to content

Commit

Permalink
Merge pull request #1334 from IFRCGo/feature/regex-support-for-env
Browse files Browse the repository at this point in the history
Support alpha-* format for environments
  • Loading branch information
samshara authored Sep 4, 2024
2 parents 5c7ab88 + 24dca58 commit 519b303
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { defineConfig, Schema } from '@julr/vite-plugin-validate-env';

export default defineConfig({
APP_TITLE: Schema.string(),
APP_ENVIRONMENT: Schema.enum(['development', 'testing', 'staging', 'production'] as const),
APP_ENVIRONMENT: (key, value) => {
const regex = /^production|staging|testing|alpha-\d+|development$/;
const valid = !!value && (value.match(regex) !== null);
if (!valid) {
throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`);
}
return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development');
},
APP_API_ENDPOINT: Schema.string({ format: 'url', protocol: true, tld: false }),
APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true }),
APP_SHOW_ENV_BANNER: Schema.boolean.optional(),
Expand Down
7 changes: 5 additions & 2 deletions app/src/views/RootLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ export function Component() {
],
);

const environmentTexts = {
const environmentTexts: {
[key in typeof environment]?: string;
} = {
production: strings.environmentTextProduction,
development: strings.environmentTextDevelopment,
staging: strings.environmentTextStaging,
Expand All @@ -378,7 +380,8 @@ export function Component() {
<AlertContainer />
{environment !== 'production' && (
<div className={styles.banner}>
{environmentTexts[environment]}
{/* NOTE: We are not translating alpha server names */}
{environmentTexts[environment] ?? environment}
</div>
)}
</div>
Expand Down

0 comments on commit 519b303

Please sign in to comment.