From f099a423fe46e192b9fbbfa07bfdfb7e56849be5 Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Wed, 4 May 2022 13:56:46 -0500 Subject: [PATCH] Add getNetlifyConfig & getNetlifyRedirect helpers --- helpers/config.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/helpers/config.js b/helpers/config.js index 81774bd..19306c7 100644 --- a/helpers/config.js +++ b/helpers/config.js @@ -1,8 +1,49 @@ +import TOML from '@iarna/toml' +import fs from 'fs-extra' + import nuxtConfig from '~/nuxt.config.js' export const nuxtHead = nuxtConfig.head +export async function getNetlifyConfig () { + const configPath = './netlify.toml' + const tomlContent = await fs.readFile(configPath, 'utf-8') + const netlifyConfig = TOML.parse(tomlContent) + + // console.log('netlifyConfig', netlifyConfig) + // console.log('tomlContent', tomlContent) + + return netlifyConfig +} + +export async function getNetlifyRedirect ( path ) { + // Check if the path is valid + // by checking if it starts with a slash + // and does not end with a slash + // if ( !path.startsWith('/') || path.endsWith('/') ) { + // throw new Error(`Invalid Netlify redirect path: ${ path }`) + // } + + const netlifyConfig = await getNetlifyConfig() + const redirects = netlifyConfig.redirects + + for ( const redirect of redirects ) { + // Check if the from path is valid + // by checking if it starts with a slash + // and does not end with a slash + if ( !redirect.from.startsWith('/') || redirect.from.endsWith('/') ) { + throw new Error(`Invalid Netlify redirect.from path: ${ redirect.from }`) + } + + if ( redirect.from === path ) { + return redirect + } + } + + return null +} + export function makeTitle ( listing ) { return `Does ${ listing.name } work on Apple Silicon? - ${ nuxtHead.title }` }