Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dist/js/generateSchemaMixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import type { JSONSchema7 } from "json-schema";
* @param schemas - Array of JSON schemas to use for generation
* @param outputPaths - Object mapping schema IDs to output file paths
* @param skipFields - Array of field names to skip during generation
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Object with success and error counts
*/
declare function generateShemaMixin(schemas: JSONSchema7[], outputPaths: Record<string, string>, skipFields?: string[]): {
declare function generateShemaMixin(schemas: JSONSchema7[], outputPaths: Record<string, string>, skipFields?: string[], from?: string): {
successCount: number;
errorCount: number;
};
Expand Down
15 changes: 9 additions & 6 deletions dist/js/generateSchemaMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ function extractSchemaProperties(schema) {
* @param mixinTypeName - Name of the mixin type
* @param entityTypeName - Name of the entity type
* @param skipFields - Array of field names to skip
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Generated TypeScript code
*/
function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields = []) {
function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields = [], from = "@mat3ra/esse/dist/js/types") {
// Convert mixin type name to camelCase for function name
const functionName = mixinTypeName.charAt(0).toLowerCase() + mixinTypeName.slice(1);
// Extract properties, handling allOf if present
Expand All @@ -80,7 +81,7 @@ function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName
// Filter out skip fields
const propertyEntries = Object.entries(properties).filter(([propertyName]) => !skipFields.includes(propertyName));
let code = `import type { InMemoryEntity } from "@mat3ra/code/dist/js/entity";\n`;
code += `import type { ${schemaName} } from "@mat3ra/esse/dist/js/types";\n\n`;
code += `import type { ${schemaName} } from "${from}";\n\n`;
// Generate the mixin type using Omit utility
const skipFieldNames = skipFields.map((field) => `"${field}"`).join(" | ");
code += `export type ${mixinTypeName} = Omit<${schemaName}, ${skipFieldNames}>;\n\n`;
Expand Down Expand Up @@ -115,9 +116,10 @@ function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName
* @param schemaId - The schema ID (e.g., "property/holder")
* @param outputPath - The output file path
* @param skipFields - Array of field names to skip
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Generated TypeScript code
*/
function generateMixinFromSchemaId(schemaId, outputPath, skipFields = []) {
function generateMixinFromSchemaId(schemaId, outputPath, skipFields = [], from = "@mat3ra/esse/dist/js/types") {
var _a, _b;
// Get the resolved schema by ID
const schema = JSONSchemasInterface_1.default.getSchemaById(schemaId);
Expand Down Expand Up @@ -149,7 +151,7 @@ function generateMixinFromSchemaId(schemaId, outputPath, skipFields = []) {
const mixinTypeName = fileName;
const entityTypeName = fileName.replace("SchemaMixin", "InMemoryEntity");
// Generate the complete mixin function
return generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields);
return generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields, from);
}
/**
* Runs ESLint autofix on generated files
Expand All @@ -174,9 +176,10 @@ function runESLintAutofix(filePaths) {
* @param schemas - Array of JSON schemas to use for generation
* @param outputPaths - Object mapping schema IDs to output file paths
* @param skipFields - Array of field names to skip during generation
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Object with success and error counts
*/
function generateShemaMixin(schemas, outputPaths, skipFields = []) {
function generateShemaMixin(schemas, outputPaths, skipFields = [], from = "@mat3ra/esse/dist/js/types") {
// Setup schemas
JSONSchemasInterface_1.default.setSchemas(schemas);
console.log("Generating mixin properties for all schemas...");
Expand All @@ -191,7 +194,7 @@ function generateShemaMixin(schemas, outputPaths, skipFields = []) {
if (!outputPath) {
throw new Error(`No output path defined for schema: ${schemaId}`);
}
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields);
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields, from);
// Ensure the directory exists
const dir = outputPath.substring(0, outputPath.lastIndexOf("/"));
if (!fs_1.default.existsSync(dir)) {
Expand Down
19 changes: 16 additions & 3 deletions src/js/generateSchemaMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function extractSchemaProperties(schema: JSONSchema7): {
* @param mixinTypeName - Name of the mixin type
* @param entityTypeName - Name of the entity type
* @param skipFields - Array of field names to skip
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Generated TypeScript code
*/
function generateMixinFunction(
Expand All @@ -83,6 +84,7 @@ function generateMixinFunction(
mixinTypeName: string,
entityTypeName: string,
skipFields: string[] = [],
from = "@mat3ra/esse/dist/js/types",
): string {
// Convert mixin type name to camelCase for function name
const functionName = mixinTypeName.charAt(0).toLowerCase() + mixinTypeName.slice(1);
Expand All @@ -100,7 +102,7 @@ function generateMixinFunction(
);

let code = `import type { InMemoryEntity } from "@mat3ra/code/dist/js/entity";\n`;
code += `import type { ${schemaName} } from "@mat3ra/esse/dist/js/types";\n\n`;
code += `import type { ${schemaName} } from "${from}";\n\n`;

// Generate the mixin type using Omit utility
const skipFieldNames = skipFields.map((field) => `"${field}"`).join(" | ");
Expand Down Expand Up @@ -143,12 +145,14 @@ function generateMixinFunction(
* @param schemaId - The schema ID (e.g., "property/holder")
* @param outputPath - The output file path
* @param skipFields - Array of field names to skip
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Generated TypeScript code
*/
function generateMixinFromSchemaId(
schemaId: string,
outputPath: string,
skipFields: string[] = [],
from = "@mat3ra/esse/dist/js/types",
): string {
// Get the resolved schema by ID
const schema = JSONSchemasInterface.getSchemaById(schemaId);
Expand Down Expand Up @@ -184,7 +188,14 @@ function generateMixinFromSchemaId(
const entityTypeName = fileName.replace("SchemaMixin", "InMemoryEntity");

// Generate the complete mixin function
return generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields);
return generateMixinFunction(
schema,
schemaName,
mixinTypeName,
entityTypeName,
skipFields,
from,
);
}

/**
Expand Down Expand Up @@ -213,12 +224,14 @@ function runESLintAutofix(filePaths: string[]): void {
* @param schemas - Array of JSON schemas to use for generation
* @param outputPaths - Object mapping schema IDs to output file paths
* @param skipFields - Array of field names to skip during generation
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
* @returns - Object with success and error counts
*/
function generateShemaMixin(
schemas: JSONSchema7[],
outputPaths: Record<string, string>,
skipFields: string[] = [],
from = "@mat3ra/esse/dist/js/types",
) {
// Setup schemas
JSONSchemasInterface.setSchemas(schemas);
Expand All @@ -239,7 +252,7 @@ function generateShemaMixin(
throw new Error(`No output path defined for schema: ${schemaId}`);
}

const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields);
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields, from);

// Ensure the directory exists
const dir = outputPath.substring(0, outputPath.lastIndexOf("/"));
Expand Down
Loading