Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new integration: json schema for data collections #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions packages/json-schema/integration.ts
Original file line number Diff line number Diff line change
@@ -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<Options>): 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<ContentConfig> {
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()
}
}
12 changes: 12 additions & 0 deletions packages/json-schema/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions packages/json-schema/utils.ts
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading