mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
feat: improve monorepo dev experience
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import {createGenerator, SchemaGenerator} from 'ts-json-schema-generator';
|
||||
import {createGenerator} from 'ts-json-schema-generator';
|
||||
import {getValidatableTypes} from './get-validatable-types.js';
|
||||
import {JSONSchema7} from 'json-schema';
|
||||
|
||||
/**
|
||||
* Compile the JSON schema for a path
|
||||
*/
|
||||
export function compileSchema(path: string, tsconfig: string): ReturnType<SchemaGenerator['createSchema']> {
|
||||
export function compileSchema(path: string, tsconfig: string): [schma: JSONSchema7, type: string] {
|
||||
const generator = createGenerator({
|
||||
path,
|
||||
tsconfig,
|
||||
@@ -23,5 +24,11 @@ export function compileSchema(path: string, tsconfig: string): ReturnType<Schema
|
||||
Object.assign(fullSchema.definitions, generator.createSchema(schema).definitions);
|
||||
}
|
||||
|
||||
return fullSchema;
|
||||
const schemaTypes = `import {JSONSchema7} from 'json-schema';\n\nexport interface SchemaMap {\n${[
|
||||
...schemaNames,
|
||||
]
|
||||
.map(schemaName => ` '${schemaName}': core.${schemaName};`)
|
||||
.join('\n')}\n}\n\nconst schema: JSONSchema7;\nexport default schema;`;
|
||||
|
||||
return [fullSchema, schemaTypes];
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {ts} from 'ts-json-schema-generator';
|
||||
*/
|
||||
export function getValidatableTypes(program: ts.Program) {
|
||||
const checker = program.getTypeChecker();
|
||||
const declarationNames: string[] = [];
|
||||
const declarationNames = new Set<string>();
|
||||
|
||||
for (const sourceFile of program.getSourceFiles()) {
|
||||
const sourceFileSymbol = checker.getSymbolAtLocation(sourceFile);
|
||||
@@ -16,7 +16,7 @@ export function getValidatableTypes(program: ts.Program) {
|
||||
const name = ts.getNameOfDeclaration(declaration);
|
||||
|
||||
if (name && validatableTags.length > 0) {
|
||||
declarationNames.push(name.getText());
|
||||
declarationNames.add(name.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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