From 0964d03a17c8ab373f272f2ab3dfe9bdfb7bc5bc Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 9 Nov 2023 09:32:46 -0500 Subject: [PATCH] More use of isProduction/isDevelopment getters --- server/commands/userInviter.ts | 2 +- server/emails/mailer.tsx | 5 ++--- server/emails/templates/SigninEmail.tsx | 2 +- server/index.ts | 2 +- server/middlewares/passport.ts | 2 +- server/models/Team.ts | 4 +--- server/onerror.ts | 6 ++---- server/presenters/env.ts | 2 +- server/queues/processors/DebounceProcessor.ts | 2 +- server/queues/tasks/ExportJSONTask.ts | 4 ++-- server/queues/tasks/RevisionCreatedNotificationsTask.ts | 2 +- server/routes/api/index.ts | 2 +- server/routes/api/teams/teams.ts | 2 +- server/routes/api/users/users.ts | 4 ++-- server/storage/redis.ts | 5 ++--- server/utils/startup.ts | 4 ++-- 16 files changed, 22 insertions(+), 28 deletions(-) diff --git a/server/commands/userInviter.ts b/server/commands/userInviter.ts index 376409919..a7f114092 100644 --- a/server/commands/userInviter.ts +++ b/server/commands/userInviter.ts @@ -89,7 +89,7 @@ export default async function userInviter({ teamUrl: team.url, }).schedule(); - if (env.ENVIRONMENT === "development") { + if (env.isDevelopment) { Logger.info( "email", `Sign in immediately: ${ diff --git a/server/emails/mailer.tsx b/server/emails/mailer.tsx index 4d4f6f09c..f53e44b6b 100644 --- a/server/emails/mailer.tsx +++ b/server/emails/mailer.tsx @@ -8,8 +8,7 @@ import Logger from "@server/logging/Logger"; import { trace } from "@server/logging/tracing"; import { baseStyles } from "./templates/components/EmailLayout"; -const useTestEmailService = - env.ENVIRONMENT === "development" && !env.SMTP_USERNAME; +const useTestEmailService = env.isDevelopment && !env.SMTP_USERNAME; type SendMailOptions = { to: string; @@ -192,7 +191,7 @@ export class Mailer { name: env.SMTP_NAME, host: env.SMTP_HOST, port: env.SMTP_PORT, - secure: env.SMTP_SECURE ?? env.ENVIRONMENT === "production", + secure: env.SMTP_SECURE ?? env.isProduction, auth: env.SMTP_USERNAME ? { user: env.SMTP_USERNAME, diff --git a/server/emails/templates/SigninEmail.tsx b/server/emails/templates/SigninEmail.tsx index f3be4b8d2..3db5f2952 100644 --- a/server/emails/templates/SigninEmail.tsx +++ b/server/emails/templates/SigninEmail.tsx @@ -41,7 +41,7 @@ signin page at: ${teamUrl} } protected render({ token, client, teamUrl }: Props) { - if (env.ENVIRONMENT === "development") { + if (env.isDevelopment) { logger.debug("email", `Sign-In link: ${this.signinLink(token, client)}`); } diff --git a/server/index.ts b/server/index.ts index a2d7f8493..863ca4224 100644 --- a/server/index.ts +++ b/server/index.ts @@ -54,7 +54,7 @@ async function master() { await checkEnv(); await checkPendingMigrations(); - if (env.TELEMETRY && env.ENVIRONMENT === "production") { + if (env.TELEMETRY && env.isProduction) { void checkUpdates(); setInterval(checkUpdates, 24 * 3600 * 1000); } diff --git a/server/middlewares/passport.ts b/server/middlewares/passport.ts index 0b207ffe3..37f45893c 100644 --- a/server/middlewares/passport.ts +++ b/server/middlewares/passport.ts @@ -52,7 +52,7 @@ export default function createMiddleware(providerName: string) { ); } - if (env.ENVIRONMENT === "development") { + if (env.isDevelopment) { throw err; } diff --git a/server/models/Team.ts b/server/models/Team.ts index 7fb9e81cc..7c46ed606 100644 --- a/server/models/Team.ts +++ b/server/models/Team.ts @@ -160,9 +160,7 @@ class Team extends ParanoidModel { * @return {boolean} Whether to show email login options */ get emailSigninEnabled(): boolean { - return ( - this.guestSignin && (!!env.SMTP_HOST || env.ENVIRONMENT === "development") - ); + return this.guestSignin && (!!env.SMTP_HOST || env.isDevelopment); } get url() { diff --git a/server/onerror.ts b/server/onerror.ts index a59272419..2abe66cfc 100644 --- a/server/onerror.ts +++ b/server/onerror.ts @@ -20,8 +20,6 @@ import { } from "@server/errors"; import { requestErrorHandler } from "@server/logging/sentry"; -const isDev = env.ENVIRONMENT === "development"; -const isProd = env.ENVIRONMENT === "production"; let errorHtmlCache: Buffer | undefined; export default function onerror(app: Koa) { @@ -159,11 +157,11 @@ function wrapInNativeError(err: any): Error { } function readErrorFile(): Buffer { - if (isDev) { + if (env.isDevelopment) { return fs.readFileSync(path.join(__dirname, "error.dev.html")); } - if (isProd) { + if (env.isProduction) { return ( errorHtmlCache ?? (errorHtmlCache = fs.readFileSync( diff --git a/server/presenters/env.ts b/server/presenters/env.ts index 30fc606a2..4c8218aca 100644 --- a/server/presenters/env.ts +++ b/server/presenters/env.ts @@ -27,7 +27,7 @@ export default function present( MAXIMUM_IMPORT_SIZE: env.MAXIMUM_IMPORT_SIZE, PDF_EXPORT_ENABLED: false, DEFAULT_LANGUAGE: env.DEFAULT_LANGUAGE, - EMAIL_ENABLED: !!env.SMTP_HOST || env.ENVIRONMENT === "development", + EMAIL_ENABLED: !!env.SMTP_HOST || env.isDevelopment, GOOGLE_ANALYTICS_ID: env.GOOGLE_ANALYTICS_ID, RELEASE: process.env.SOURCE_COMMIT || process.env.SOURCE_VERSION || undefined, diff --git a/server/queues/processors/DebounceProcessor.ts b/server/queues/processors/DebounceProcessor.ts index 9b496b087..28d1b1375 100644 --- a/server/queues/processors/DebounceProcessor.ts +++ b/server/queues/processors/DebounceProcessor.ts @@ -18,7 +18,7 @@ export default class DebounceProcessor extends BaseProcessor { { // speed up revision creation in development, we don't have all the // time in the world. - delay: (env.ENVIRONMENT === "development" ? 0.5 : 5) * 60 * 1000, + delay: (env.isProduction ? 5 : 0.5) * 60 * 1000, } ); break; diff --git a/server/queues/tasks/ExportJSONTask.ts b/server/queues/tasks/ExportJSONTask.ts index 53be31092..1f8894be5 100644 --- a/server/queues/tasks/ExportJSONTask.ts +++ b/server/queues/tasks/ExportJSONTask.ts @@ -50,7 +50,7 @@ export default class ExportJSONTask extends ExportTask { zip.file( `metadata.json`, - env.ENVIRONMENT === "development" + env.isDevelopment ? JSON.stringify(metadata, null, 2) : JSON.stringify(metadata) ); @@ -142,7 +142,7 @@ export default class ExportJSONTask extends ExportTask { zip.file( `${serializeFilename(collection.name)}.json`, - env.ENVIRONMENT === "development" + env.isDevelopment ? JSON.stringify(output, null, 2) : JSON.stringify(output) ); diff --git a/server/queues/tasks/RevisionCreatedNotificationsTask.ts b/server/queues/tasks/RevisionCreatedNotificationsTask.ts index 354e59833..fa1adeaa6 100644 --- a/server/queues/tasks/RevisionCreatedNotificationsTask.ts +++ b/server/queues/tasks/RevisionCreatedNotificationsTask.ts @@ -102,7 +102,7 @@ export default class RevisionCreatedNotificationsTask extends BaseTask) => { const { transaction } = ctx.state; diff --git a/server/routes/api/users/users.ts b/server/routes/api/users/users.ts index 70187eb40..9cc467035 100644 --- a/server/routes/api/users/users.ts +++ b/server/routes/api/users/users.ts @@ -27,7 +27,7 @@ import pagination from "../middlewares/pagination"; import * as T from "./schema"; const router = new Router(); -const emailEnabled = !!(env.SMTP_HOST || env.ENVIRONMENT === "development"); +const emailEnabled = !!(env.SMTP_HOST || env.isDevelopment); router.post( "users.list", @@ -457,7 +457,7 @@ router.post( user.incrementFlag(UserFlag.InviteSent); await user.save({ transaction }); - if (env.ENVIRONMENT === "development") { + if (env.isDevelopment) { logger.info( "email", `Sign in immediately: ${ diff --git a/server/storage/redis.ts b/server/storage/redis.ts index 83e6e14c4..900025361 100644 --- a/server/storage/redis.ts +++ b/server/storage/redis.ts @@ -11,7 +11,7 @@ type RedisAdapterOptions = RedisOptions & { const defaultOptions: RedisOptions = { maxRetriesPerRequest: 20, enableReadyCheck: false, - showFriendlyErrorStack: env.ENVIRONMENT === "development", + showFriendlyErrorStack: env.isDevelopment, retryStrategy(times: number) { Logger.warn(`Retrying redis connection: attempt ${times}`); @@ -40,8 +40,7 @@ export default class RedisAdapter extends Redis { * For debugging. The connection name is based on the services running in * this process. Note that this does not need to be unique. */ - const connectionNamePrefix = - env.ENVIRONMENT === "development" ? process.pid : "outline"; + const connectionNamePrefix = env.isDevelopment ? process.pid : "outline"; const connectionName = `${connectionNamePrefix}:${env.SERVICES.replace(/,/g, "-")}` + (connectionNameSuffix ? `:${connectionNameSuffix}` : ""); diff --git a/server/utils/startup.ts b/server/utils/startup.ts index 2f760c655..b922d83c3 100644 --- a/server/utils/startup.ts +++ b/server/utils/startup.ts @@ -61,14 +61,14 @@ export async function checkEnv() { } }); - if (env.ENVIRONMENT === "production") { + if (env.isProduction) { Logger.info( "lifecycle", chalk.green(` Is your team enjoying Outline? Consider supporting future development by sponsoring the project:\n\nhttps://github.com/sponsors/outline `) ); - } else if (env.ENVIRONMENT === "development") { + } else if (env.isDevelopment) { Logger.warn( `Running Outline in ${chalk.bold( "development mode"