Skip to content

Commit

Permalink
fix: use the same type for environments (#3312)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandr Kazachenko <[email protected]>
  • Loading branch information
anxolin and shoom3301 authored Nov 2, 2023
1 parent 0e4319e commit bb6e26f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions apps/cowswap-frontend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ REACT_APP_MOCK=true
# Domain regex (to detect environment)
# REACT_APP_DOMAIN_REGEX_LOCAL="^(:?localhost:\d{2,5}|(?:127|192)(?:\.[0-9]{1,3}){3})"
# REACT_APP_DOMAIN_REGEX_PR="^pr\d+--cowswap\.review|^(swap-dev-git-[\w\d-]+|swap-\w{9}-)cowswap\.vercel\.app"
# REACT_APP_DOMAIN_REGEX_DEV="^(dev\.swap\.cow\.fi|swap-develop\.vercel\.app)"
# REACT_APP_DOMAIN_REGEX_DEVELOPMENT="^(dev\.swap\.cow\.fi|swap-develop\.vercel\.app)"
# REACT_APP_DOMAIN_REGEX_STAGING="^(staging\.swap\.cow\.fi|swap-staging\.vercel\.app)"
# REACT_APP_DOMAIN_REGEX_PROD="^(swap\.cow\.fi|swap-prod\.vercel\.app)$"
# REACT_APP_DOMAIN_REGEX_PRODUCTION="^(swap\.cow\.fi|swap-prod\.vercel\.app)$"
# REACT_APP_DOMAIN_REGEX_BARN="^(barn\.cow\.fi|swap-barn\.vercel\.app)$"
# REACT_APP_DOMAIN_REGEX_ENS="(:?^cowswap\.eth|ipfs)"

Expand Down
36 changes: 17 additions & 19 deletions libs/common-utils/src/environments.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { registerOnWindow } from './misc'

const DEFAULT_ENVIRONMENTS_REGEX: Record<Envs, string> = {
LOCAL: '^(:?localhost:\\d{2,5}|(?:127|192)(?:\\.[0-9]{1,3}){3})',
PR: '^(swap-dev-git-[\\w\\d-]+|swap-\\w{9}-)cowswap\\.vercel\\.app',
DEV: '^(dev.swap.cow.fi|swap-develop.vercel.app)',
STAGING: '^(staging.swap.cow.fi|swap-staging.vercel.app)',
PROD: '^(swap.cow.fi|swap-prod.vercel.app)$',
BARN: '^(barn.cow.fi|swap-barn.vercel.app)$',
ENS: '(:?^cowswap.eth|ipfs)',
const DEFAULT_ENVIRONMENTS_REGEX: Record<EnvironmentName, string> = {
local: '^(:?localhost:\\d{2,5}|(?:127|192)(?:\\.[0-9]{1,3}){3})',
pr: '^(swap-dev-git-[\\w\\d-]+|swap-\\w{9}-)cowswap\\.vercel\\.app',
development: '^(dev.swap.cow.fi|swap-develop.vercel.app)',
staging: '^(staging.swap.cow.fi|swap-staging.vercel.app)',
production: '^(swap.cow.fi|swap-prod.vercel.app)$',
barn: '^(barn.cow.fi|swap-barn.vercel.app)$',
ens: '(:?^cowswap.eth|ipfs)',
}

function getRegex(env: Envs) {
const regex = process.env[`REACT_APP_DOMAIN_REGEX_${env}`] || DEFAULT_ENVIRONMENTS_REGEX[env]
function getRegex(env: EnvironmentName) {
const regex = process.env[`REACT_APP_DOMAIN_REGEX_${env.toUpperCase()}`] || DEFAULT_ENVIRONMENTS_REGEX[env]
return new RegExp(regex, 'i')
}

type Envs = 'LOCAL' | 'PR' | 'DEV' | 'STAGING' | 'PROD' | 'BARN' | 'ENS'
export interface EnvironmentChecks {
isProd: boolean
isEns: boolean
Expand All @@ -27,20 +25,20 @@ export interface EnvironmentChecks {
}

export function checkEnvironment(host: string, path: string): EnvironmentChecks {
const ensRegex = getRegex('ENS')
const ensRegex = getRegex('ens')

return {
// Project environments
isLocal: getRegex('LOCAL').test(host),
isDev: getRegex('DEV').test(host),
isPr: getRegex('PR').test(host),
isStaging: getRegex('STAGING').test(host),
isProd: getRegex('PROD').test(host),
isLocal: getRegex('local').test(host),
isDev: getRegex('development').test(host),
isPr: getRegex('pr').test(host),
isStaging: getRegex('staging').test(host),
isProd: getRegex('production').test(host),
isEns: ensRegex.test(host) || ensRegex.test(path),

// Environment used for Backend workflow
// The latest stable version pointing to the DEV api
isBarn: getRegex('BARN').test(host),
isBarn: getRegex('barn').test(host),
}
}

Expand Down

0 comments on commit bb6e26f

Please sign in to comment.