diff --git a/modules/graph-layers/package.json b/modules/graph-layers/package.json index 104d4e6e..328ab766 100644 --- a/modules/graph-layers/package.json +++ b/modules/graph-layers/package.json @@ -19,6 +19,11 @@ "types": "./dist/index.d.ts", "require": "./dist/index.cjs", "import": "./dist/index.js" + }, + "./graph-style-schema.json": "./dist/graph-style-schema.json", + "./graph-style-schema.cdn": { + "types": "./dist/graph-style-schema.cdn.d.ts", + "default": "./dist/graph-style-schema.cdn.js" } }, "files": [ @@ -26,6 +31,8 @@ "src" ], "scripts": { + "build": "tsc -b tsconfig.json && node ./scripts/generate-graphstyle-schema.mjs", + "build:schema": "node ./scripts/generate-graphstyle-schema.mjs", "test": "vitest run", "test-watch": "vitest" }, @@ -56,7 +63,8 @@ }, "devDependencies": { "ngraph.generators": "^20.1.0", - "zod": "^4.0.0" + "zod": "^4.0.0", + "zod-to-json-schema": "^3.22.5" }, "peerDependencies": { "zod": "^3.23.8 || ^4.0.0" diff --git a/modules/graph-layers/scripts/generate-graphstyle-schema.mjs b/modules/graph-layers/scripts/generate-graphstyle-schema.mjs new file mode 100644 index 00000000..2a8ab117 --- /dev/null +++ b/modules/graph-layers/scripts/generate-graphstyle-schema.mjs @@ -0,0 +1,67 @@ +// deck.gl-community +// SPDX-License-Identifier: MIT + +import {mkdir, readFile, writeFile} from 'node:fs/promises'; +import {dirname, join, relative} from 'node:path'; +import {fileURLToPath, pathToFileURL} from 'node:url'; + +import {zodToJsonSchema} from 'zod-to-json-schema'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const packageRoot = join(__dirname, '..'); +const distDir = join(packageRoot, 'dist'); + +async function loadGraphStylesheetModule() { + const schemaPath = join(distDir, 'style', 'graph-stylesheet.schema.js'); + try { + const moduleUrl = pathToFileURL(schemaPath).href; + return await import(moduleUrl); + } catch (error) { + const relativePath = relative(packageRoot, schemaPath); + throw new Error( + `Failed to import compiled schema from "${relativePath}". Ensure TypeScript build succeeds before running this script.\n${ + error instanceof Error ? error.message : error + }` + ); + } +} + +async function createSchemaArtifacts() { + const module = await loadGraphStylesheetModule(); + const {GraphStylesheetSchema} = module; + if (!GraphStylesheetSchema) { + throw new Error('GraphStylesheetSchema export not found.'); + } + + const packageJson = JSON.parse(await readFile(join(packageRoot, 'package.json'), 'utf8')); + const packageName = packageJson.name; + const packageVersion = packageJson.version; + const cdnUrl = `https://cdn.jsdelivr.net/npm/${packageName}@${packageVersion}/dist/graph-style-schema.json`; + + const schema = zodToJsonSchema(GraphStylesheetSchema, 'GraphStylesheet'); + const {definitions, ...schemaBody} = schema; + + const jsonSchema = { + $schema: 'https://json-schema.org/draft/2020-12/schema', + $id: cdnUrl, + title: 'Deck.gl Graph Stylesheet', + description: + 'JSON schema describing the declarative stylesheet format supported by @deck.gl-community/graph-layers.', + ...schemaBody, + ...(definitions ? {definitions} : {}) + }; + + await mkdir(distDir, {recursive: true}); + await writeFile(join(distDir, 'graph-style-schema.json'), JSON.stringify(jsonSchema, null, 2)); + + const cdnModule = `export const GRAPH_STYLE_SCHEMA_CDN_URL = '${cdnUrl}';\nexport default GRAPH_STYLE_SCHEMA_CDN_URL;\n`; + const cdnTypes = `export declare const GRAPH_STYLE_SCHEMA_CDN_URL: string;\nexport default GRAPH_STYLE_SCHEMA_CDN_URL;\n`; + + await writeFile(join(distDir, 'graph-style-schema.cdn.js'), cdnModule); + await writeFile(join(distDir, 'graph-style-schema.cdn.d.ts'), cdnTypes); +} + +createSchemaArtifacts().catch((error) => { + console.error(error); + process.exitCode = 1; +}); diff --git a/yarn.lock b/yarn.lock index e00fb9c3..41837e97 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1416,6 +1416,7 @@ __metadata: "@luma.gl/core": "npm:~9.2.0" "@luma.gl/engine": "npm:~9.2.0" "@luma.gl/shadertools": "npm:~9.2.0" + preact: "npm:^10.17.0" languageName: unknown linkType: soft @@ -1467,6 +1468,7 @@ __metadata: preact: "npm:^10.17.0" raf: "npm:^3.4.1" zod: "npm:^4.0.0" + zod-to-json-schema: "npm:^3.22.5" peerDependencies: zod: ^3.23.8 || ^4.0.0 languageName: unknown @@ -22268,6 +22270,15 @@ __metadata: languageName: node linkType: hard +"zod-to-json-schema@npm:^3.22.5": + version: 3.24.6 + resolution: "zod-to-json-schema@npm:3.24.6" + peerDependencies: + zod: ^3.24.1 + checksum: 10c0/b907ab6d057100bd25a37e5545bf5f0efa5902cd84d3c3ec05c2e51541431a47bd9bf1e5e151a244273409b45f5986d55b26e5d207f98abc5200702f733eb368 + languageName: node + linkType: hard + "zod@npm:^4.0.0": version: 4.1.12 resolution: "zod@npm:4.1.12"