mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
refactor: revert json schema changes
This commit is contained in:
@@ -3,41 +3,35 @@ import {generateFiles, Plugin, PluginContext} from '@openstapps/tsup-plugin';
|
||||
import {JSONSchema7} from 'json-schema';
|
||||
import {Plugin as EsbuildPlugin} from 'esbuild';
|
||||
import {createGenerator} from 'ts-json-schema-generator';
|
||||
import {dirname} from 'path';
|
||||
|
||||
export type SchemaConsumer = (this: PluginContext, schema: JSONSchema7) => Record<string, string | Buffer>;
|
||||
|
||||
export const esbuildJsonSchemaPlugin: EsbuildPlugin = {
|
||||
export const jsonSchema: EsbuildPlugin = {
|
||||
name: 'json-schema',
|
||||
setup(build) {
|
||||
const fileRegex = /^schema:/;
|
||||
const namespace = 'json-schema-ns';
|
||||
const schemas = new Map<string, string>();
|
||||
|
||||
build.onResolve({filter: fileRegex}, async ({path, importer}) => {
|
||||
const [from, name] = path.replace(fileRegex, '').split('#', 2);
|
||||
const outputName = `${name}.schema.json`;
|
||||
if (!schemas.has(outputName)) {
|
||||
const generator = createGenerator({
|
||||
path: from
|
||||
? await build
|
||||
.resolve(from, {resolveDir: dirname(importer), kind: 'import-statement'})
|
||||
.then(it => it.path)
|
||||
: importer,
|
||||
extraTags: ['elasticsearch'],
|
||||
skipTypeCheck: true,
|
||||
});
|
||||
schemas.set(outputName, JSON.stringify(generator.createSchema(name)));
|
||||
}
|
||||
build.onResolve({filter: fileRegex}, ({path, importer}) => {
|
||||
const [from, name] = path.replace(fileRegex, '').split('#', 1);
|
||||
|
||||
return {
|
||||
path: outputName,
|
||||
pluginData: {schema: JSON.parse(schemas.get(outputName)!)},
|
||||
path: `${from === 'file' ? importer : from}#${name}`,
|
||||
namespace,
|
||||
};
|
||||
});
|
||||
|
||||
build.onLoad({filter: /.*/, namespace}, ({path}) => {
|
||||
if (!schemas.has(path)) {
|
||||
const [sourcePath, schemaName] = path.split('#', 1);
|
||||
const generator = createGenerator({
|
||||
path: sourcePath,
|
||||
extraTags: ['elasticsearch'],
|
||||
skipTypeCheck: true,
|
||||
});
|
||||
schemas.set(path, JSON.stringify(generator.createSchema(schemaName)));
|
||||
}
|
||||
return {
|
||||
contents: schemas.get(path),
|
||||
loader: 'json',
|
||||
|
||||
Reference in New Issue
Block a user