Skip to content

Commit

Permalink
Merge pull request #6 from decentraland/feat/support-gatsby-default-env
Browse files Browse the repository at this point in the history
feat: support gatsby on getDefaultEnv
  • Loading branch information
2fd committed Oct 13, 2022
2 parents e266bc4 + adb8585 commit c0608ac
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ describe('when getting default env', () => {
)
})
})
describe('and GATSBY_DCL_DEFAULT_ENV is invalid', () => {
it('should throw', () => {
expect(() =>
getDefaultEnv({ GATSBY_DCL_DEFAULT_ENV: 'invalid' })
).toThrow()
})
})
describe('and GATSBY_DCL_DEFAULT_ENV is valid', () => {
it('should return GATSBY_DCL_DEFAULT_ENV as default env', () => {
expect(getDefaultEnv({ GATSBY_DCL_DEFAULT_ENV: 'dev' })).toBe(
Env.DEVELOPMENT
)
})
})
})

describe('and both DCL_DEFAULT_ENV and REACT_APP_DCL_DEFAULT_ENV are defined', () => {
Expand All @@ -100,6 +114,52 @@ describe('when getting default env', () => {
})
})
})

describe('and both DCL_DEFAULT_ENV and GATSBY_DCL_DEFAULT_ENV are defined', () => {
describe('and both have the same value', () => {
it('should return that value as default env', () => {
expect(
getDefaultEnv({
DCL_DEFAULT_ENV: 'dev',
GATSBY_DCL_DEFAULT_ENV: 'dev',
})
).toBe(Env.DEVELOPMENT)
})
})
describe('and they have different values', () => {
it('should throw', () => {
expect(() =>
getDefaultEnv({
DCL_DEFAULT_ENV: 'dev',
GATSBY_DCL_DEFAULT_ENV: 'prod',
})
).toThrow()
})
})
})

describe('and both REACT_APP_DCL_DEFAULT_ENV and GATSBY_DCL_DEFAULT_ENV are defined', () => {
describe('and both have the same value', () => {
it('should return that value as default env', () => {
expect(
getDefaultEnv({
REACT_APP_DCL_DEFAULT_ENV: 'dev',
GATSBY_DCL_DEFAULT_ENV: 'dev',
})
).toBe(Env.DEVELOPMENT)
})
})
describe('and they have different values', () => {
it('should throw', () => {
expect(() =>
getDefaultEnv({
REACT_APP_DCL_DEFAULT_ENV: 'dev',
GATSBY_DCL_DEFAULT_ENV: 'prod',
})
).toThrow()
})
})
})
})

describe('when getting env', () => {
Expand Down
26 changes: 25 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function parseEnvVar(envVar: string) {
export function getDefaultEnv(
envVars: Record<string, string | undefined> = {}
): Env {
const { DCL_DEFAULT_ENV, REACT_APP_DCL_DEFAULT_ENV } = envVars
const { DCL_DEFAULT_ENV, REACT_APP_DCL_DEFAULT_ENV, GATSBY_DCL_DEFAULT_ENV } = envVars

if (
DCL_DEFAULT_ENV &&
Expand All @@ -59,6 +59,26 @@ export function getDefaultEnv(
)
}

if (
DCL_DEFAULT_ENV &&
GATSBY_DCL_DEFAULT_ENV &&
DCL_DEFAULT_ENV !== GATSBY_DCL_DEFAULT_ENV
) {
throw new Error(
'You have defined both DCL_DEFAULT_ENV and GATSBY_DCL_DEFAULT_ENV with different values'
)
}

if (
REACT_APP_DCL_DEFAULT_ENV &&
GATSBY_DCL_DEFAULT_ENV &&
REACT_APP_DCL_DEFAULT_ENV !== GATSBY_DCL_DEFAULT_ENV
) {
throw new Error(
'You have defined both REACT_APP_DCL_DEFAULT_ENV and GATSBY_DCL_DEFAULT_ENV with different values'
)
}

if (DCL_DEFAULT_ENV) {
return parseEnvVar(DCL_DEFAULT_ENV)
}
Expand All @@ -67,6 +87,10 @@ export function getDefaultEnv(
return parseEnvVar(REACT_APP_DCL_DEFAULT_ENV)
}

if (GATSBY_DCL_DEFAULT_ENV) {
return parseEnvVar(GATSBY_DCL_DEFAULT_ENV)
}

return Env.PRODUCTION
}

Expand Down

0 comments on commit c0608ac

Please sign in to comment.