From 3ab141cdea2cc71015342ce03161912e0bcba845 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:47:33 +0000 Subject: [PATCH 1/2] new integration: json schema for data collections --- packages/json-schema/integration.ts | 74 +++++++++++++++++++++++++++++ packages/json-schema/package.json | 12 +++++ packages/json-schema/utils.ts | 7 +++ pnpm-lock.yaml | 20 ++++++++ 4 files changed, 113 insertions(+) create mode 100644 packages/json-schema/integration.ts create mode 100644 packages/json-schema/package.json create mode 100644 packages/json-schema/utils.ts diff --git a/packages/json-schema/integration.ts b/packages/json-schema/integration.ts new file mode 100644 index 0000000..5f61cb1 --- /dev/null +++ b/packages/json-schema/integration.ts @@ -0,0 +1,74 @@ +import fs from "node:fs" +import { fileURLToPath } from "node:url" +import { createServer} from "vite" +import { + createContentTypesGenerator, + createNodeLogger, + createVite, + createSettings, + globalContentConfigObserver, + resolveConfig, + type ContentConfig +} from "./utils.ts" +import { zodToJsonSchema } from "zod-to-json-schema" +import type { AstroIntegration } from "astro" + +export interface Options {} + +export default function(_?: Partial): AstroIntegration { + return { + name: "astro-integration-json-schema", + hooks: { + async "astro:config:setup" ({ config }) { + const { collections } = await getCollectionDefinitions() + for (const key in collections) { + const collection = collections[key] + if (collection.type !== "data") continue + const schema = zodToJsonSchema(collection.schema) + const json = JSON.stringify(schema, null, 4) + fs.writeFileSync(new URL(`./.astro/${key}.schema.json`, config.root), json) + } + } + } + } +} + +async function getCollectionDefinitions(): Promise { + const logger = createNodeLogger({ logLevel: "silent" }) + const { astroConfig } = await resolveConfig({}, "sync") + + const settings = await createSettings(astroConfig, fileURLToPath(astroConfig.root)) + + const tempViteServer = await createServer( + await createVite( + { + server: { middlewareMode: true, hmr: false, watch: { ignored: ["**"] } }, + optimizeDeps: { disabled: true }, + ssr: { external: [] }, + logLevel: "silent", + }, + { settings, logger, mode: "build", command: "build", fs } + ) + ) + try { + const contentTypesGenerator = await createContentTypesGenerator({ + contentConfigObserver: globalContentConfigObserver, + logger: logger, + fs, + settings, + viteServer: tempViteServer, + }) + + await contentTypesGenerator.init() + + const contentConfig = globalContentConfigObserver.get() + if (contentConfig.status === "loaded") { + return contentConfig.config + } + else throw new Error("Could not load content/config.ts", { cause: contentConfig }) + } catch (e) { + console.error(e) + } finally { + await tempViteServer.close() + } +} \ No newline at end of file diff --git a/packages/json-schema/package.json b/packages/json-schema/package.json new file mode 100644 index 0000000..0be94ad --- /dev/null +++ b/packages/json-schema/package.json @@ -0,0 +1,12 @@ +{ + "name": "astro-json-schema", + "version": "0.0.0", + "exports": { + ".": "./integration.ts" + }, + "dependencies": { + "astro": "4", + "vite": "5", + "zod-to-json-schema": "3" + } +} \ No newline at end of file diff --git a/packages/json-schema/utils.ts b/packages/json-schema/utils.ts new file mode 100644 index 0000000..a1b04d1 --- /dev/null +++ b/packages/json-schema/utils.ts @@ -0,0 +1,7 @@ +export { createContentTypesGenerator } from "./node_modules/astro/dist/content/index.js" +export { globalContentConfigObserver } from "./node_modules/astro/dist/content/utils.js" +export { resolveConfig } from "./node_modules/astro/dist/core/config/config.js" +export { createNodeLogger } from "./node_modules/astro/dist/core/config/logging.js" +export { createSettings } from "./node_modules/astro/dist/core/config/settings.js" +export { createVite } from "./node_modules/astro/dist/core/create-vite.js" +export type { ContentConfig } from "./node_modules/astro/dist/content/utils.js" \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 479e905..e8ee346 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -85,6 +85,18 @@ importers: specifier: '4' version: 4.0.3(@types/node@20.10.4)(typescript@5.3.3) + packages/json-schema: + dependencies: + astro: + specifier: '4' + version: 4.0.3(@types/node@20.10.4)(typescript@5.3.3) + vite: + specifier: '5' + version: 5.0.7(@types/node@20.10.4) + zod-to-json-schema: + specifier: ^3.22.3 + version: 3.22.3(zod@3.22.4) + packages/prerender-patterns: devDependencies: '@types/node': @@ -6807,6 +6819,14 @@ packages: resolution: {integrity: sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==} engines: {node: '>=12.20'} + /zod-to-json-schema@3.22.3(zod@3.22.4): + resolution: {integrity: sha512-9isG8SqRe07p+Aio2ruBZmLm2Q6Sq4EqmXOiNpDxp+7f0LV6Q/LX65fs5Nn+FV/CzfF3NLBoksXbS2jNYIfpKw==} + peerDependencies: + zod: ^3.22.4 + dependencies: + zod: 3.22.4 + dev: false + /zod@3.22.4: resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==} From 8fc7ab66899c2dd7179a600fabbe7d3e9493b35e Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:53:22 +0000 Subject: [PATCH 2/2] update lockfile --- pnpm-lock.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e8ee346..0b00a39 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -94,7 +94,7 @@ importers: specifier: '5' version: 5.0.7(@types/node@20.10.4) zod-to-json-schema: - specifier: ^3.22.3 + specifier: '3' version: 3.22.3(zod@3.22.4) packages/prerender-patterns: