mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
feat: improve monorepo dev experience
This commit is contained in:
@@ -1,10 +1,44 @@
|
||||
import {compileSchema} from './generator/compile-schema.js';
|
||||
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';
|
||||
|
||||
export type SchemaConsumer = (
|
||||
this: PluginContext,
|
||||
schema: ReturnType<typeof compileSchema>,
|
||||
) => Record<string, string | Buffer>;
|
||||
export type SchemaConsumer = (this: PluginContext, schema: JSONSchema7) => Record<string, string | Buffer>;
|
||||
|
||||
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}, ({path, importer}) => {
|
||||
const [from, name] = path.replace(fileRegex, '').split('#', 1);
|
||||
|
||||
return {
|
||||
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',
|
||||
};
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* TSUp plugin for generating JSONSchema files
|
||||
@@ -18,12 +52,17 @@ export function jsonSchemaPlugin(
|
||||
return {
|
||||
name: 'json-schema-generator',
|
||||
async buildEnd() {
|
||||
let schema: ReturnType<typeof compileSchema>;
|
||||
let schema: JSONSchema7;
|
||||
await generateFiles('JSON-Schema', async function () {
|
||||
schema = compileSchema((this.options.entry as string[])[0], this.options.tsconfig!);
|
||||
const [jsonSchema, types] = compileSchema(
|
||||
(this.options.entry as string[])[0],
|
||||
this.options.tsconfig!,
|
||||
);
|
||||
schema = jsonSchema;
|
||||
|
||||
return {
|
||||
[schemaName]: JSON.stringify(schema),
|
||||
[schemaName]: JSON.stringify(jsonSchema),
|
||||
[`${schemaName.replace(/\.json$/, '')}.d.ts`]: types,
|
||||
};
|
||||
}).call(this);
|
||||
|
||||
@@ -35,3 +74,5 @@ export function jsonSchemaPlugin(
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export {compileSchema} from './generator/compile-schema.js';
|
||||
|
||||
Reference in New Issue
Block a user