mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 17:12:43 +00:00
feat: improve monorepo dev experience
This commit is contained in:
37
packages/json-schema-generator/src/index.ts
Normal file
37
packages/json-schema-generator/src/index.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {compileSchema} from './generator/compile-schema.js';
|
||||
import {generateFiles, Plugin, PluginContext} from '@openstapps/tsup-plugin';
|
||||
|
||||
export type SchemaConsumer = (
|
||||
this: PluginContext,
|
||||
schema: ReturnType<typeof compileSchema>,
|
||||
) => Record<string, string | Buffer>;
|
||||
|
||||
/**
|
||||
* TSUp plugin for generating JSONSchema files
|
||||
* @param schemaName the name of the generated schema
|
||||
* @param schemaConsumers any consumers that can directly use the schema
|
||||
*/
|
||||
export function jsonSchemaPlugin(
|
||||
schemaName: string,
|
||||
...schemaConsumers: Array<[string, SchemaConsumer]>
|
||||
): Plugin {
|
||||
return {
|
||||
name: 'json-schema-generator',
|
||||
async buildEnd() {
|
||||
let schema: ReturnType<typeof compileSchema>;
|
||||
await generateFiles('JSON-Schema', async function () {
|
||||
schema = compileSchema((this.options.entry as string[])[0], this.options.tsconfig!);
|
||||
|
||||
return {
|
||||
[schemaName]: JSON.stringify(schema),
|
||||
};
|
||||
}).call(this);
|
||||
|
||||
for (const [name, consumer] of schemaConsumers) {
|
||||
await generateFiles(name, async function () {
|
||||
return consumer.call(this, schema);
|
||||
}).call(this);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user