From 967443a6ac04ad0fd76c43211db91fcdfe720528 Mon Sep 17 00:00:00 2001 From: Alex Stephen <1325798+rambleraptor@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:19:09 -0400 Subject: [PATCH] Add config from aep repo (#16) * add repo link through config loader * config --- astro.config.mjs | 22 ++++++++++++++++++- scripts/generate.ts | 6 ++++- scripts/src/config.ts | 51 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 scripts/src/config.ts diff --git a/astro.config.mjs b/astro.config.mjs index 0747da7..4ac96f9 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -5,6 +5,7 @@ import tailwind from "@astrojs/tailwind"; let sidebar = JSON.parse(fs.readFileSync("generated/sidebar.json")); let linter_sidebar = JSON.parse(fs.readFileSync("generated/linter_sidebar.json")); let redirects = JSON.parse(fs.readFileSync("generated/redirects.json")); +let config = JSON.parse(fs.readFileSync("generated/config.json")); // https://astro.build/config @@ -17,8 +18,27 @@ export default defineConfig({ './src/tailwind.css', ], social: { - github: 'https://github.com/withastro/starlight' + github: config.urls.repo, }, + // Google Analytics tag. + head: [ + { + tag: 'script', + attrs: { + src: ``, + async: true, + }, + }, + { + tag: 'script', + content: ` + window.dataLayer = window.dataLayer || []; + function gtag(){dataLayer.push(arguments);} + gtag('js', new Date()); + gtag('config', ${config.site.ga_tag}); + `, + }, + ], sidebar: sidebar.concat(linter_sidebar) }), tailwind({ diff --git a/scripts/generate.ts b/scripts/generate.ts index 6cea820..7d92c0d 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { load, dump } from "js-yaml"; -import { glob } from 'glob'; +import loadConfigFiles from './src/config'; interface AEP { title: string; @@ -450,6 +450,10 @@ function buildRedirects(aeps: AEP[]): object { return Object.fromEntries(aeps.map((aep) => [`/${aep.id}`, `/${aep.slug}`])); } +// Build config. +let config = loadConfigFiles("hero.yaml", "urls.yaml", "site.yaml"); +writeSidebar(config, "config.json"); + // Build out AEPs. let aeps = await assembleAEPs(); diff --git a/scripts/src/config.ts b/scripts/src/config.ts new file mode 100644 index 0000000..9251c6d --- /dev/null +++ b/scripts/src/config.ts @@ -0,0 +1,51 @@ +import fs from 'fs'; +import yaml from 'js-yaml'; +import path from 'path'; +import { z } from "zod"; + +const AEP_LOC = process.env.AEP_LOCATION!; + +const Config = z.object({ + hero: z.object({ + buttons: z.array(z.object({ + text: z.string(), + href: z.string(), + })), + shortcuts: z.array(z.object({ + title: z.string(), + description: z.string(), + button: z.object({ + text: z.string(), + href: z.string(), + }), + })), + }), + site: z.object({ + ga_tag: z.string(), + }), + urls: z.object({ + site: z.string(), + repo: z.string(), + }), +}); + +type Config = z.infer; + +function loadConfigFiles(...fileNames: string[]): Config { + const config = {}; + + fileNames.forEach((fileName) => { + try { + const filePath = path.join(AEP_LOC, "config", fileName); + const fileContents = fs.readFileSync(filePath, 'utf8'); + const parsedYaml = yaml.load(fileContents); + config[fileName.replace('.yaml', '')] = parsedYaml; + } catch (error) { + console.error(`Error loading ${fileName}:`, error); + } + }); + + return Config.parse(config); +} + +export default loadConfigFiles;