From 5a6ac2477357a68da6b7bc53d16985fb2596ab38 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Sat, 5 Oct 2024 19:43:02 +0000 Subject: [PATCH] Better config file support --- .gitignore | 3 ++- package.json | 3 ++- src/app/[...file]/page.tsx | 6 +++--- src/app/config.ts | 25 +++++++++++++++++++++++++ src/app/page.tsx | 4 ++-- yarn.lock | 5 +++++ 6 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/app/config.ts diff --git a/.gitignore b/.gitignore index fd3dbb5..5ff9804 100644 --- a/.gitignore +++ b/.gitignore @@ -25,8 +25,9 @@ npm-debug.log* yarn-debug.log* yarn-error.log* -# local env files +# local conig files .env*.local +config.toml # vercel .vercel diff --git a/package.json b/package.json index dbb3438..32e40f0 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "prism-react-renderer": "^2.4.0", "react": "^18", "react-dom": "^18", - "react-markdown": "^9.0.1" + "react-markdown": "^9.0.1", + "toml": "^3.0.0" }, "devDependencies": { "@types/node": "^20", diff --git a/src/app/[...file]/page.tsx b/src/app/[...file]/page.tsx index cde757a..d1489bd 100644 --- a/src/app/[...file]/page.tsx +++ b/src/app/[...file]/page.tsx @@ -2,6 +2,7 @@ import type { Metadata } from 'next'; import Image from 'next/image'; import * as style from './page.css'; +import config from '../config.ts'; import Content from './components/content'; import Copy from './components/copy'; import download_image_dark from './download_dark.svg'; @@ -33,9 +34,8 @@ export async function generateMetadata( export default async function Page({ params, searchParams }: Props) { - const enable_repointing = process.env.ENABLE_REPOINTING == 'true'; const path = get_path(params.file); - const root = get_root(enable_repointing, searchParams); + const root = get_root(config.enable_repointing, searchParams); const full = `${root}${path}`; const download_ttys = [ @@ -69,7 +69,7 @@ export default async function Page({
{download_ttys.map((text, index) => )}
- {(!enable_repointing && searchParams['root'] != undefined) ? + {(!config.enable_repointing && searchParams['root'] != undefined) ?

Error: this instance of Motto does not have repointing enabled

: } diff --git a/src/app/config.ts b/src/app/config.ts new file mode 100644 index 0000000..8a1345c --- /dev/null +++ b/src/app/config.ts @@ -0,0 +1,25 @@ +import fs from 'fs'; +import toml from 'toml'; + +type ConfigType = { + root_url: string, + enable_repointing: boolean, +}; + +function load_config(): ConfigType { + const config: ConfigType = { + // Default values + root_url: "", + enable_repointing: false, + }; + + const config_from_file = toml.parse(fs.readFileSync(`${process.cwd()}/config.toml`)); + for (const key in config) { + if (key in config_from_file) { + config[key] = config_from_file[key]; + } + } + return config; +} + +export default load_config(); diff --git a/src/app/page.tsx b/src/app/page.tsx index 136339a..996fc19 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,7 @@ import * as style from './page.css'; +import config from './config.ts'; export default function App() { - const supports_repointing = process.env.ENABLE_REPOINTING == 'true'; return (
@@ -10,7 +10,7 @@ export default function App() { Motto is a static file server beautifier. Click here for more information.

This instance is currently pointed to {process.env.ROOT_URL} { - supports_repointing ? + config.enable_repointing ? <>but supports repointing. : <>and does not support repointing. }

diff --git a/yarn.lock b/yarn.lock index 97da2a9..ccc6fee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3802,6 +3802,11 @@ to-regex-range@^5.0.1: dependencies: is-number "^7.0.0" +toml@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee" + integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w== + trim-lines@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/trim-lines/-/trim-lines-3.0.1.tgz#d802e332a07df861c48802c04321017b1bd87338"