From 3a9ee8aa0bac86d8a99f9c737fdff62b35b24d0a Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Mon, 4 Mar 2024 10:25:19 -0800 Subject: [PATCH] add verbose helper function --- packages/astro-ghostcms/README.md | 2 +- packages/astro-ghostcms/src/astro-ghostcms.ts | 77 ++++++++----------- .../astro-ghostcms/src/schemas/userconfig.ts | 2 +- playgrounds/astro-playground/astro.config.mjs | 2 +- 4 files changed, 35 insertions(+), 48 deletions(-) diff --git a/packages/astro-ghostcms/README.md b/packages/astro-ghostcms/README.md index fb9eb08c..cba98a86 100644 --- a/packages/astro-ghostcms/README.md +++ b/packages/astro-ghostcms/README.md @@ -70,7 +70,7 @@ export default defineConfig({ disableDefault404: false, // Allows the user to disable the default `/404 page, to be able to create their own under `/src/pages/404.astro`. enableRSSFeed: true, // Allows the user to Enable or disable RSS Feed Generation. Default: true enableOGImages: true, // Allows the user to Enable or disable OG Image Generation. Default: true - fullConsoleLogs: false, // Show the full Log output from All parts of Astro-GhostCMS + verbose: false, // Show the full Log output from All parts of Astro-GhostCMS Integrations: { // This allows user config passthrough from Astro-GhostCMS to the Included Integrations robotsTxt: { diff --git a/packages/astro-ghostcms/src/astro-ghostcms.ts b/packages/astro-ghostcms/src/astro-ghostcms.ts index 44c1b8c3..d9e6f5b3 100644 --- a/packages/astro-ghostcms/src/astro-ghostcms.ts +++ b/packages/astro-ghostcms/src/astro-ghostcms.ts @@ -25,6 +25,7 @@ const ENV = loadEnv("all", process.cwd(), "CONTENT_API"); // Import User Configuration Zod Schema import { GhostUserConfigSchema } from "./schemas/userconfig"; +import type { string } from "astro/zod"; /** Astro-GhostCMS Integration * @description This integration allows you to use GhostCMS as a headless CMS for your Astro project @@ -47,6 +48,8 @@ export default defineIntegration({ injectRoute, logger, }) => { + // Set up verbose logging + const verbose = options.verbose; // Configure Loggers const GhostLogger = logger.fork(c.bold(c.blue("👻 Astro-GhostCMS"))); const GhostENVLogger = logger.fork( @@ -54,23 +57,35 @@ export default defineIntegration({ "ENV Check", )}`, ); + // Configure Integration Loggers & verbose logging const GhostIntegrationLogger = logger.fork( `${c.bold(c.blue("👻 Astro-GhostCMS"))}${c.gray("/")}${c.blue( "Integrations", )}`, ); + const intLogInfo = (message:string) => { + if (verbose) { + GhostIntegrationLogger.info(message); + } + }; + + // Configure Route Logger & verbose logging const GhostRouteLogger = logger.fork( `${c.bold(c.blue("👻 Astro-GhostCMS"))}${c.gray("/")}${c.blue( "Router", )}`, ); + const routeLogInfo = (message:string) => { + if (verbose) { + GhostRouteLogger.info(message); + } + }; + // Setup Watch Integration for Hot Reload during DEV watchIntegration(resolve()); GhostLogger.info("Initializing @matthiesenxyz/astro-ghostcms..."); - // Set up verbose logging - const verbose = options.fullConsoleLogs; // Check for GhostCMS environment variables GhostENVLogger.info( @@ -136,77 +151,49 @@ export default defineIntegration({ verbose, }), ); - } else if (verbose) { - GhostIntegrationLogger.info(c.gray("Theme Provider is disabled")); + } else { + intLogInfo(c.gray("Theme Provider is disabled")); } // Satori OG Images if (options.enableOGImages) { addIntegration(ghostOGImages({ verbose })); - } else if (verbose) { - GhostIntegrationLogger.info( - c.gray("OG Image Provider is disabled"), - ); + } else { + intLogInfo(c.gray("OG Image Provider is disabled")); } // RSS Feed if (options.enableRSSFeed) { addIntegration(ghostRSS({ verbose })); - } else if (verbose) { - GhostIntegrationLogger.info(c.gray("RSS Feed is disabled")); + } else { + intLogInfo(c.gray("RSS Feed is disabled")); } // @ASTROJS/SITEMAP if (!hasIntegration("@astrojs/sitemap")) { - if (verbose) { - GhostIntegrationLogger.info( - c.bold( - c.magenta(`Adding ${c.blue("@astrojs/sitemap")} integration`), - ), - ); - } + intLogInfo(c.bold(c.magenta(`Adding ${c.blue("@astrojs/sitemap")} integration`))); addIntegration(sitemap(options.Integrations?.sitemap)); - } else if (verbose) { - GhostIntegrationLogger.info( - c.gray( - "@astrojs/sitemap integration already exists, skipping...", - ), - ); + } else { + intLogInfo(c.gray("@astrojs/sitemap integration already exists, skipping...")); } // ASTRO-ROBOTS-TXT if (!hasIntegration("astro-robots-txt")) { - if (verbose) { - GhostIntegrationLogger.info( - c.bold( - c.magenta(`Adding ${c.blue("astro-robots-txt")} integration`), - ), - ); - } + intLogInfo(c.bold(c.magenta(`Adding ${c.blue("astro-robots-txt")} integration`))); addIntegration(robotsTxt(options.Integrations?.robotsTxt)); - } else if (verbose) { - GhostIntegrationLogger.info( - c.gray( - "astro-robots-txt integration already exists, skipping...", - ), - ); + } else { + intLogInfo(c.gray("astro-robots-txt integration already exists, skipping...")); } // Set up default 404 page if (!options.disableDefault404) { - if (verbose) { - GhostRouteLogger.info( - c.bold(c.cyan("Setting up default 404 page")), - ); - } + routeLogInfo(c.bold(c.cyan("Setting up default 404 page"))); injectRoute({ pattern: "/404", entrypoint: `${name}/404.astro`, prerender: true, }); - } else if (verbose) { - GhostRouteLogger.info( - c.gray("Default 404 page is disabled, Skipping..."), - ); + } else { + routeLogInfo(c.gray("Default 404 page is disabled, Skipping...")); } // Add virtual imports for user configuration diff --git a/packages/astro-ghostcms/src/schemas/userconfig.ts b/packages/astro-ghostcms/src/schemas/userconfig.ts index d3f59944..23455f06 100644 --- a/packages/astro-ghostcms/src/schemas/userconfig.ts +++ b/packages/astro-ghostcms/src/schemas/userconfig.ts @@ -40,7 +40,7 @@ export const GhostUserConfigSchema = z.object({ /** Allows the user to turn on/off Full Console Logs * @default true */ - fullConsoleLogs: z.boolean().optional().default(false), + verbose: z.boolean().optional().default(false), /** OPTIONAL - Integrations Configuration * This option allows the user to configure the included integrations * Options shown are the availble options diff --git a/playgrounds/astro-playground/astro.config.mjs b/playgrounds/astro-playground/astro.config.mjs index faf7fac6..a24919e3 100644 --- a/playgrounds/astro-playground/astro.config.mjs +++ b/playgrounds/astro-playground/astro.config.mjs @@ -14,7 +14,7 @@ export default defineConfig({ ThemeProvider: { theme: "@matthiesenxyz/astro-ghostcms-brutalbyelian", }, - fullConsoleLogs: false, + verbose: false, }), ], });