collab/mermaid/.esbuild/jsonSchemaPlugin.tsblame
View source
6dd74de1import type { JSONSchemaType } from 'ajv/dist/2019.js';
6dd74de2import type { MermaidConfig } from '../packages/mermaid/src/config.type.js';
6dd74de3import { readFile } from 'node:fs/promises';
6dd74de4import { getDefaults, getSchema, loadSchema } from '../.build/jsonSchema.js';
6dd74de5
6dd74de6/**
6dd74de7 * ESBuild plugin that handles JSON Schemas saved as a `.schema.yaml` file.
6dd74de8 *
6dd74de9 * Use `my-example.schema.yaml?only-defaults=true` to only load the default values.
6dd74de10 */
6dd74de11
6dd74de12export const jsonSchemaPlugin = {
6dd74de13 name: 'json-schema-plugin',
6dd74de14 setup(build) {
6dd74de15 let schema: JSONSchemaType<MermaidConfig> | undefined = undefined;
6dd74de16 let content = '';
6dd74de17
6dd74de18 build.onLoad({ filter: /config\.schema\.yaml$/ }, async (args) => {
6dd74de19 // Load the file from the file system
6dd74de20 const source = await readFile(args.path, 'utf8');
6dd74de21 const resolvedSchema: JSONSchemaType<MermaidConfig> =
6dd74de22 content === source && schema ? schema : loadSchema(source, args.path);
6dd74de23 if (content !== source) {
6dd74de24 content = source;
6dd74de25 schema = resolvedSchema;
6dd74de26 }
6dd74de27 const contents = args.suffix.includes('only-defaults')
6dd74de28 ? getDefaults(resolvedSchema)
6dd74de29 : getSchema(resolvedSchema);
6dd74de30 return { contents, warnings: [] };
6dd74de31 });
6dd74de32 },
6dd74de33};
6dd74de34
6dd74de35export default jsonSchemaPlugin;