-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Centralize configuration in one place. #312
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
47ce5a5
feat: Centralize configuration in one place.
cullylarson 1bac408
Rename a function.
cullylarson fceace7
Simplify.
cullylarson 9801b2b
Fix lint issue.
cullylarson cd33407
Remove staging stage.
cullylarson 7b04cd1
Refine buildProd and get rid of ts-node-dev.
cullylarson e993e52
Fix lint mismatch.
cullylarson b5bf7c3
Merge branch 'canary' into cully/centralize-config
cullylarson 7a636ec
Hopefully pass CI check.
cullylarson 4a219fe
Merge branch 'canary' into cully/centralize-config
cullylarson c1869b1
Add the stage to the health endpoint.
cullylarson e5f4050
Remove BASE_URL from env files.
cullylarson 200dd03
Replace APP_ENV with NEXT_PUBLIC_APP_ENV.
cullylarson 9b2786e
Add the SHOULD_MIGRATE env variable.
cullylarson 9b34be9
Refine when APP_ENV is used.
cullylarson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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, | ||
cullylarson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,17 @@ | ||
export {}; | ||
const spawn = require('child_process').spawn; | ||
import { spawn } from 'child_process'; | ||
import { config } from '@/config'; | ||
|
||
const DEFAULT_BUILD_COMMAND = `yarn build:prisma && yarn build:next`; | ||
|
||
/** | ||
* This builds the production app. | ||
* if the current branch should be migrated, it runs migrations first. | ||
*/ | ||
function buildProd() { | ||
let buildCommand = DEFAULT_BUILD_COMMAND; | ||
const shouldMigrate = process.env.NODE_ENV === 'production'; | ||
const buildCommand = config.database.shouldMigrate | ||
? `yarn db:deploy && ${DEFAULT_BUILD_COMMAND}` | ||
: DEFAULT_BUILD_COMMAND; | ||
|
||
if (shouldMigrate) { | ||
buildCommand = `yarn db:deploy && ${buildCommand}`; | ||
} | ||
const child = spawn(buildCommand, { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
||
const child = spawn(buildCommand, { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
||
child.on('exit', function (code: number) { | ||
process.exit(code); | ||
}); | ||
} | ||
|
||
if (require.main === module) { | ||
buildProd(); | ||
} | ||
|
||
module.exports = { buildProd }; | ||
child.on('exit', function (code: number) { | ||
process.exit(code); | ||
}); |
5 changes: 4 additions & 1 deletion
5
packages/create-bison-app/template/server/middleware/timing.ts
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,9 @@ | ||
// https://prettier.io/docs/en/options.html | ||
module.exports = { | ||
trailingComma: 'es5', | ||
bracketSpacing: true, | ||
printWidth: 100, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
arrowParens: 'always', | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to default
shouldMigrate
totrue
orprocess.env.NODE_ENV === 'production'
? I think this might trip people up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's set explicitly by the
SHOULD_MIGRATE
environment variable. Which, I realized I forgot to add to the .env files.