| 6dd74de | | | 1 | import type { JSONSchemaType } from 'ajv/dist/2019.js'; |
| 6dd74de | | | 2 | import type { MermaidConfig } from '../packages/mermaid/src/config.type.js'; |
| 6dd74de | | | 3 | import { readFile } from 'node:fs/promises'; |
| 6dd74de | | | 4 | import { getDefaults, getSchema, loadSchema } from '../.build/jsonSchema.js'; |
| 6dd74de | | | 5 | |
| 6dd74de | | | 6 | /** |
| 6dd74de | | | 7 | * ESBuild plugin that handles JSON Schemas saved as a `.schema.yaml` file. |
| 6dd74de | | | 8 | * |
| 6dd74de | | | 9 | * Use `my-example.schema.yaml?only-defaults=true` to only load the default values. |
| 6dd74de | | | 10 | */ |
| 6dd74de | | | 11 | |
| 6dd74de | | | 12 | export const jsonSchemaPlugin = { |
| 6dd74de | | | 13 | name: 'json-schema-plugin', |
| 6dd74de | | | 14 | setup(build) { |
| 6dd74de | | | 15 | let schema: JSONSchemaType<MermaidConfig> | undefined = undefined; |
| 6dd74de | | | 16 | let content = ''; |
| 6dd74de | | | 17 | |
| 6dd74de | | | 18 | build.onLoad({ filter: /config\.schema\.yaml$/ }, async (args) => { |
| 6dd74de | | | 19 | // Load the file from the file system |
| 6dd74de | | | 20 | const source = await readFile(args.path, 'utf8'); |
| 6dd74de | | | 21 | const resolvedSchema: JSONSchemaType<MermaidConfig> = |
| 6dd74de | | | 22 | content === source && schema ? schema : loadSchema(source, args.path); |
| 6dd74de | | | 23 | if (content !== source) { |
| 6dd74de | | | 24 | content = source; |
| 6dd74de | | | 25 | schema = resolvedSchema; |
| 6dd74de | | | 26 | } |
| 6dd74de | | | 27 | const contents = args.suffix.includes('only-defaults') |
| 6dd74de | | | 28 | ? getDefaults(resolvedSchema) |
| 6dd74de | | | 29 | : getSchema(resolvedSchema); |
| 6dd74de | | | 30 | return { contents, warnings: [] }; |
| 6dd74de | | | 31 | }); |
| 6dd74de | | | 32 | }, |
| 6dd74de | | | 33 | }; |
| 6dd74de | | | 34 | |
| 6dd74de | | | 35 | export default jsonSchemaPlugin; |