-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Centralize configuration in one place. (#312)
* Move all (most) process.env references into config.ts. * Add zod checks for config. * Remove ts-node-dev. Doesn't seem to be needed, creates some weird situations where processes don't exist (e.g. buildProd). * Refine the buildProd script. * fix: Prettier config in bisonapp is different from prettier config in packages/create-bison-app/template. So if you save a file while editing bisonapp and then create a bison app from it, it may have lint errors. Resolved this by copying the prettier config from packages/create-bison-app/template into the root folder.
- Loading branch information
1 parent
808a48e
commit 5f810b4
Showing
21 changed files
with
181 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
# Overwrite envs here to mimic production | ||
# Include NODE_ENV for package script withEnv to work as expected | ||
|
||
APP_ENV="production" | ||
NEXT_PUBLIC_APP_ENV="production" | ||
NODE_ENV="development" # production // change if you truly want PROD | ||
PORT=3000 | ||
BASE_URL="http://localhost:3000" | ||
SHOULD_MIGRATE=0 | ||
|
||
DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.dev.name %>?schema=public" | ||
|
||
NEXTAUTH_SECRET="bisonProd" | ||
NEXTAUTH_URL="http://localhost:3000" | ||
NEXTAUTH_URL="http://localhost:3000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
# Copy to .env.test.local to override | ||
# Include NODE_ENV for package script withEnv to work as expected | ||
|
||
APP_ENV="test" | ||
NEXT_PUBLIC_APP_ENV="test" | ||
NODE_ENV="test" | ||
PORT=3001 | ||
BASE_URL="http://localhost:3001" | ||
SHOULD_MIGRATE=0 | ||
|
||
DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.test.name %>?schema=public" | ||
|
||
NEXTAUTH_SECRET="bisonTest" | ||
NEXTAUTH_URL="http://localhost:3001" | ||
NEXTAUTH_URL="http://localhost:3001" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
# ENV vars here override .env.test when running locally | ||
# Include NODE_ENV for package script withEnv to work as expected | ||
|
||
APP_ENV="test" | ||
NEXT_PUBLIC_APP_ENV="test" | ||
NODE_ENV="test" | ||
PORT=3001 | ||
BASE_URL="http://localhost:3001" | ||
SHOULD_MIGRATE=0 | ||
|
||
DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.test.name %>?schema=public" | ||
|
||
NEXTAUTH_SECRET="bisonTest" | ||
NEXTAUTH_URL="http://localhost:3001" | ||
NEXTAUTH_URL="http://localhost:3001" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { z } from 'zod'; | ||
|
||
import { notEmpty } from './lib/type-witchcraft'; | ||
|
||
const stages = ['production', 'development', 'test'] as const; | ||
|
||
type Stage = (typeof stages)[number]; | ||
|
||
function getStage(stages: Stage[]) { | ||
if (!stages.length) return 'development'; | ||
|
||
for (const stage of stages) { | ||
// if any of the provided stages is not production, assume we aren't in production | ||
if (stage !== 'production') { | ||
return stage; | ||
} | ||
} | ||
|
||
return stages[0]; | ||
} | ||
|
||
function isStage(potentialStage: string): potentialStage is Stage { | ||
return stages.includes(potentialStage as Stage); | ||
} | ||
|
||
function envToBoolean(value: string | undefined, defaultValue = false): boolean { | ||
if (value === undefined || value === '') { | ||
return defaultValue; | ||
} | ||
|
||
// Explicitly test for true instead of false because we don't want to turn | ||
// something on by accident. | ||
return ['1', 'true'].includes(value.trim().toLowerCase()) ? true : false; | ||
} | ||
|
||
export function isProduction() { | ||
return stage === 'production'; | ||
} | ||
|
||
export function isDevelopment() { | ||
return stage === 'development'; | ||
} | ||
|
||
export function isTesting() { | ||
return stage === 'test'; | ||
} | ||
|
||
export function isLocal() { | ||
return isDevelopment() || isTesting(); | ||
} | ||
|
||
// a bit more versatile form of boolean coercion than zod provides | ||
const coerceBoolean = z | ||
.string() | ||
.optional() | ||
.transform((value) => envToBoolean(value)) | ||
.pipe(z.boolean()); | ||
|
||
const configSchema = z.object({ | ||
stage: z.enum(stages), | ||
ci: z.object({ | ||
isCi: coerceBoolean, | ||
isPullRequest: coerceBoolean, | ||
}), | ||
database: z.object({ | ||
url: z.string(), | ||
shouldMigrate: coerceBoolean, | ||
}), | ||
}); | ||
|
||
const stage = getStage( | ||
[process.env.NODE_ENV, process.env.NEXT_PUBLIC_APP_ENV].filter(notEmpty).filter(isStage) | ||
); | ||
|
||
// NOTE: Remember that only env variables that start with NEXT_PUBLIC or are | ||
// listed in next.config.js will be available on the client. | ||
export const config = configSchema.parse({ | ||
stage, | ||
ci: { | ||
isCi: process.env.CI, | ||
isPullRequest: process.env.IS_PULL_REQUEST, | ||
}, | ||
database: { | ||
url: process.env.DATABASE_URL, | ||
shouldMigrate: process.env.SHOULD_MIGRATE, | ||
}, | ||
git: { | ||
commit: process.env.FC_GIT_COMMIT_SHA || process.env.RENDER_GIT_COMMIT, | ||
}, | ||
auth: { | ||
secret: process.env.NEXTAUTH_SECRET, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { | ||
return value !== null && value !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.