From e902da9c4d3205d05aef79dd8fbe4f1eb9b3afc3 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Fri, 13 Dec 2024 04:26:18 -0800 Subject: [PATCH] Refactor --- packages/studiocms_core/src/index.ts | 8 +++ packages/studiocms_renderers/src/index.ts | 60 ++++++++++++++++--- .../studiocms_renderers/src/integration.ts | 57 ------------------ packages/studiocms_robotstxt/src/index.ts | 8 +++ 4 files changed, 68 insertions(+), 65 deletions(-) delete mode 100644 packages/studiocms_renderers/src/integration.ts diff --git a/packages/studiocms_core/src/index.ts b/packages/studiocms_core/src/index.ts index 940f1e7c1..a32e2a3f3 100644 --- a/packages/studiocms_core/src/index.ts +++ b/packages/studiocms_core/src/index.ts @@ -9,6 +9,14 @@ import i18nDTSOutput from './stubs/i18n-dts'; import libDtsFileOutput from './stubs/lib'; import sdkDtsFile from './stubs/sdk'; +/** + * **StudioCMS Core Integration** + * + * @param options StudioCMS Configuration + * @returns AstroIntegration + * + * @see [StudioCMS Docs](https://docs.studiocms.dev) for more information on how to use StudioCMS. + */ export function studioCMSCore(options: StudioCMSConfig): AstroIntegration { return { name: pkgName, diff --git a/packages/studiocms_renderers/src/index.ts b/packages/studiocms_renderers/src/index.ts index 9e1127440..267e5e725 100644 --- a/packages/studiocms_renderers/src/index.ts +++ b/packages/studiocms_renderers/src/index.ts @@ -1,14 +1,58 @@ -import { - type StudioCMSRendererConfig, - StudioCMSRendererConfigSchema, -} from '@studiocms/core/schemas/renderer'; -import integration from './integration'; +import { runtimeLogger } from '@inox-tools/runtime-logger'; +import { stringify } from '@studiocms/core/lib'; +import type { StudioCMSRendererConfig } from '@studiocms/core/schemas/renderer'; +import type { AstroIntegration } from 'astro'; +import { addVirtualImports, createResolver } from 'astro-integration-kit'; +import { name as pkgName } from '../package.json'; +import { rendererDTS } from './stubs/renderer'; +import { rendererConfigDTS } from './stubs/renderer-config'; +import { rendererAstroMarkdownDTS } from './stubs/renderer-markdownConfig'; /** - * StudioCMS Renderers Integration + * **StudioCMS Renderers Integration** + * + * @param options StudioCMS Renderer Configuration + * @returns AstroIntegration + * + * @see [StudioCMS Docs](https://docs.studiocms.dev) for more information on how to use StudioCMS. */ -const studioCMSRenderers = integration; +export function studioCMSRenderers(options: StudioCMSRendererConfig): AstroIntegration { + // Create resolver relative to this file + const { resolve } = createResolver(import.meta.url); + + // Import the Renderer Component + const RendererComponent = resolve('./components/StudioCMSRenderer.astro'); + return { + name: pkgName, + hooks: { + 'astro:config:setup': (params) => { + // Setup the runtime logger + runtimeLogger(params, { name: 'studiocms-renderer' }); + + // Add Virtual Imports + addVirtualImports(params, { + name: pkgName, + imports: { + 'studiocms:renderer': `export { default as StudioCMSRenderer } from '${RendererComponent}';`, + 'studiocms:renderer/config': `export default ${stringify(options)}`, + 'studiocms:renderer/astroMarkdownConfig': `export default ${stringify(params.config.markdown)}`, + }, + }); + }, + 'astro:config:done': ({ injectTypes }) => { + // Inject Types for Renderer + injectTypes(rendererDTS(RendererComponent)); + + // Inject Types for Renderer Config + injectTypes(rendererConfigDTS()); + + // Inject Types for Astro Markdown Config + injectTypes(rendererAstroMarkdownDTS()); + }, + }, + }; +} export default studioCMSRenderers; -export { StudioCMSRendererConfigSchema, type StudioCMSRendererConfig }; +export type { StudioCMSRendererConfig }; diff --git a/packages/studiocms_renderers/src/integration.ts b/packages/studiocms_renderers/src/integration.ts deleted file mode 100644 index d9cd60da9..000000000 --- a/packages/studiocms_renderers/src/integration.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { runtimeLogger } from '@inox-tools/runtime-logger'; -import { stringify } from '@studiocms/core/lib'; -import { StudioCMSRendererConfigSchema } from '@studiocms/core/schemas/renderer'; -import { addVirtualImports, createResolver, defineIntegration } from 'astro-integration-kit'; -import { name } from '../package.json'; -import { rendererDTS } from './stubs/renderer'; -import { rendererConfigDTS } from './stubs/renderer-config'; -import { rendererAstroMarkdownDTS } from './stubs/renderer-markdownConfig'; - -/** - * StudioCMS Renderers Integration - */ -export default defineIntegration({ - name, - optionsSchema: StudioCMSRendererConfigSchema, - setup({ name, options }) { - // Create resolver relative to this file - const { resolve } = createResolver(import.meta.url); - - // Import the Renderer Component - const RendererComponent = resolve('./components/StudioCMSRenderer.astro'); - - return { - hooks: { - 'astro:config:setup': (params) => { - // Destructure the params - const { - config: { markdown: astroMarkdown }, - } = params; - - // Setup the runtime logger - runtimeLogger(params, { name: 'studiocms-renderer' }); - - // Add Virtual Imports - addVirtualImports(params, { - name, - imports: { - 'studiocms:renderer': `export { default as StudioCMSRenderer } from '${RendererComponent}';`, - 'studiocms:renderer/config': `export default ${stringify(options)}`, - 'studiocms:renderer/astroMarkdownConfig': `export default ${stringify(astroMarkdown)}`, - }, - }); - }, - 'astro:config:done': ({ injectTypes }) => { - // Inject Types for Renderer - injectTypes(rendererDTS(RendererComponent)); - - // Inject Types for Renderer Config - injectTypes(rendererConfigDTS()); - - // Inject Types for Astro Markdown Config - injectTypes(rendererAstroMarkdownDTS()); - }, - }, - }; - }, -}); diff --git a/packages/studiocms_robotstxt/src/index.ts b/packages/studiocms_robotstxt/src/index.ts index 2fa6fc8a7..efa3a8321 100644 --- a/packages/studiocms_robotstxt/src/index.ts +++ b/packages/studiocms_robotstxt/src/index.ts @@ -154,6 +154,14 @@ const defaultConfig: RobotsConfig = { ], }; +/** + * **Robots.txt Integration** + * + * A simple integration to generate a `robots.txt` file for your Astro project. + * + * @param astroConfig Robots Configuration + * @returns AstroIntegration + */ export default function createRobotsIntegration(astroConfig: RobotsConfig): AstroIntegration { let config: AstroConfig; let finalSiteMapHref: string;