diff --git a/nuxt.config.ts b/nuxt.config.ts index 4ab8bff4f..8f8dc9235 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -1,5 +1,4 @@ -import './src/dotenv.ts'; - +import dotenv from './server/dotenv.ts'; import { loadSuml } from './server/loader.ts'; import fs from 'node:fs'; import path from 'path'; @@ -9,8 +8,6 @@ import replacePlugin from '@rollup/plugin-replace'; import yamlPlugin from '@rollup/plugin-yaml'; import { sentryVitePlugin } from '@sentry/vite-plugin'; import type { RouteMeta } from 'vue-router'; -import { buildList } from './src/helpers.ts'; -import buildLocaleList from './src/buildLocaleList.ts'; import type { Config } from './locale/config.ts'; import type { Translations } from './locale/translations.ts'; import mdPlugin from './plugins/rollup/md.ts'; @@ -28,11 +25,12 @@ declare module 'nuxt/app' { } } +dotenv(); + const config = loadSuml('config') as Config; const translations = loadSuml('translations') as Translations; const locale = config.locale; -const locales = buildLocaleList(locale); const title = translations.title; const description = translations.description; const colour = '#C71585'; @@ -49,26 +47,6 @@ const version = fs.existsSync(versionFile) ? fs.readFileSync(versionFile).toStri const publicKeyFile = `${__dirname}/keys/public.pem`; const publicKey = fs.existsSync(publicKeyFile) ? fs.readFileSync(publicKeyFile).toString('utf-8') : undefined; -const allLocalesUrls = buildList(function*() { - if (process.env.NODE_ENV === 'development') { - if (process.env.BASE_URL) { - yield process.env.BASE_URL; - } - yield 'http://pronouns.test:3000'; - yield 'http://localhost:3000'; - } else if (process.env.ENV === 'test') { - if (process.env.BASE_URL) { - yield process.env.BASE_URL; - } - } else { - yield 'https://pronouns.page'; - for (const localeDescription of Object.values(locales)) { - yield localeDescription.url; - } - } -}); -process.env.ALL_LOCALES_URLS = allLocalesUrls.join(','); - const esBuildOptions = { supported: { 'top-level-await': true, @@ -166,7 +144,7 @@ export default defineNuxtConfig({ env: process.env.ENV, baseUrl: process.env.BASE_URL, homeUrl: process.env.HOME_URL || 'https://pronouns.page', - allLocalesUrls, + allLocalesUrls: process.env.ALL_LOCALES_URLS!.split(','), version, publicKey, turnstileSiteKey: process.env.TURNSTILE_SITEKEY, diff --git a/run-wrapper.sh b/run-wrapper.sh index 8cc8a22ba..7dc53231e 100755 --- a/run-wrapper.sh +++ b/run-wrapper.sh @@ -7,11 +7,7 @@ cd "$(dirname "$0")" || exit nvm_bin=~/.nvm/versions/node/"$(<.nvmrc)"/bin export PATH=$nvm_bin:$PATH if [ "$1" = "start" ]; then - # load .env variables and start server - set -a - source .env - set +a - exec node .output/server/index.mjs + exec node .output/server/index.mjs -- "$(dirname "$0")" "$PORT" else bin_file=./node_modules/.bin/"$1" if [ -f "$bin_file" ]; then diff --git a/server/dotenv.ts b/server/dotenv.ts index b9ef7b548..b3ca56ddb 100644 --- a/server/dotenv.ts +++ b/server/dotenv.ts @@ -1,9 +1,37 @@ import dotenv from 'dotenv'; +import buildLocaleList from '../src/buildLocaleList.ts'; +import { buildList } from '../src/helpers.ts'; +import type { Config } from '../locale/config.ts'; +import { rootDir } from './paths.ts'; +import { loadSuml } from './loader.ts'; -const __dirname = new URL('.', import.meta.url).pathname; +export default () => { + dotenv.config({ path: `${rootDir}/.env` }); + if (process.env.__INCLUDE) { + dotenv.config({ path: `${rootDir}/${process.env.__INCLUDE}` }); + } + process.env.CLOUDFRONT = `https://${process.env.AWS_CLOUDFRONT_ID}.cloudfront.net`; -dotenv.config({ path: `${__dirname}/../.env` }); -if (process.env.__INCLUDE) { - dotenv.config({ path: `${__dirname}/../${process.env.__INCLUDE}` }); -} -process.env.CLOUDFRONT = `https://${process.env.AWS_CLOUDFRONT_ID}.cloudfront.net`; + const config = loadSuml('config') as Config; + const locales = buildLocaleList(config.locale); + + const allLocalesUrls = buildList(function*() { + if (process.env.NODE_ENV === 'development') { + if (process.env.BASE_URL) { + yield process.env.BASE_URL; + } + yield 'http://pronouns.test:3000'; + yield 'http://localhost:3000'; + } else if (process.env.ENV === 'test') { + if (process.env.BASE_URL) { + yield process.env.BASE_URL; + } + } else { + yield 'https://pronouns.page'; + for (const localeDescription of Object.values(locales)) { + yield localeDescription.url; + } + } + }); + process.env.ALL_LOCALES_URLS = allLocalesUrls.join(','); +}; diff --git a/server/globals.ts b/server/globals.ts index 9a1aba246..0264220e7 100644 --- a/server/globals.ts +++ b/server/globals.ts @@ -1,8 +1,11 @@ // load this module before other modules which require these global variables +import dotenv from './dotenv.ts'; import { loadSuml } from './loader.ts'; import type { Config } from '../locale/config.ts'; import type { Translations } from '../locale/translations.ts'; +dotenv(); + export const config = loadSuml('config') as Config; global.config = config; global.translations = loadSuml('translations') as Translations; diff --git a/server/index.ts b/server/index.ts index a6417f010..371da67a0 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,5 +1,3 @@ -import './setup.ts'; - import express from 'express'; import type { Request, Response, NextFunction } from 'express'; import { useBase } from 'h3'; diff --git a/server/setup.ts b/server/setup.ts index ab37a1fcf..f66954f6b 100644 --- a/server/setup.ts +++ b/server/setup.ts @@ -1,6 +1,8 @@ import * as Sentry from '@sentry/node'; -import '../src/dotenv.ts'; +import dotenv from './dotenv.ts'; + +dotenv(); Sentry.init({ dsn: process.env.SENTRY_DSN,