Skip to content

Commit f19b253

Browse files
authored
Merge pull request #129 from Exabyte-io/chore/SOF-7733
SOF-7733: improve generateShemaMixin
2 parents 57287c5 + 6610c87 commit f19b253

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

dist/js/generateSchemaMixin.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import type { JSONSchema7 } from "json-schema";
55
* @param schemas - Array of JSON schemas to use for generation
66
* @param outputPaths - Object mapping schema IDs to output file paths
77
* @param skipFields - Array of field names to skip during generation
8+
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
89
* @returns - Object with success and error counts
910
*/
10-
declare function generateShemaMixin(schemas: JSONSchema7[], outputPaths: Record<string, string>, skipFields?: string[]): {
11+
declare function generateShemaMixin(schemas: JSONSchema7[], outputPaths: Record<string, string>, skipFields?: string[], from?: string): {
1112
successCount: number;
1213
errorCount: number;
1314
};

dist/js/generateSchemaMixin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ function extractSchemaProperties(schema) {
6767
* @param mixinTypeName - Name of the mixin type
6868
* @param entityTypeName - Name of the entity type
6969
* @param skipFields - Array of field names to skip
70+
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
7071
* @returns - Generated TypeScript code
7172
*/
72-
function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields = []) {
73+
function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields = [], from = "@mat3ra/esse/dist/js/types") {
7374
// Convert mixin type name to camelCase for function name
7475
const functionName = mixinTypeName.charAt(0).toLowerCase() + mixinTypeName.slice(1);
7576
// Extract properties, handling allOf if present
@@ -80,7 +81,7 @@ function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName
8081
// Filter out skip fields
8182
const propertyEntries = Object.entries(properties).filter(([propertyName]) => !skipFields.includes(propertyName));
8283
let code = `import type { InMemoryEntity } from "@mat3ra/code/dist/js/entity";\n`;
83-
code += `import type { ${schemaName} } from "@mat3ra/esse/dist/js/types";\n\n`;
84+
code += `import type { ${schemaName} } from "${from}";\n\n`;
8485
// Generate the mixin type using Omit utility
8586
const skipFieldNames = skipFields.map((field) => `"${field}"`).join(" | ");
8687
code += `export type ${mixinTypeName} = Omit<${schemaName}, ${skipFieldNames}>;\n\n`;
@@ -115,9 +116,10 @@ function generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName
115116
* @param schemaId - The schema ID (e.g., "property/holder")
116117
* @param outputPath - The output file path
117118
* @param skipFields - Array of field names to skip
119+
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
118120
* @returns - Generated TypeScript code
119121
*/
120-
function generateMixinFromSchemaId(schemaId, outputPath, skipFields = []) {
122+
function generateMixinFromSchemaId(schemaId, outputPath, skipFields = [], from = "@mat3ra/esse/dist/js/types") {
121123
var _a, _b;
122124
// Get the resolved schema by ID
123125
const schema = JSONSchemasInterface_1.default.getSchemaById(schemaId);
@@ -149,7 +151,7 @@ function generateMixinFromSchemaId(schemaId, outputPath, skipFields = []) {
149151
const mixinTypeName = fileName;
150152
const entityTypeName = fileName.replace("SchemaMixin", "InMemoryEntity");
151153
// Generate the complete mixin function
152-
return generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields);
154+
return generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields, from);
153155
}
154156
/**
155157
* Runs ESLint autofix on generated files
@@ -174,9 +176,10 @@ function runESLintAutofix(filePaths) {
174176
* @param schemas - Array of JSON schemas to use for generation
175177
* @param outputPaths - Object mapping schema IDs to output file paths
176178
* @param skipFields - Array of field names to skip during generation
179+
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
177180
* @returns - Object with success and error counts
178181
*/
179-
function generateShemaMixin(schemas, outputPaths, skipFields = []) {
182+
function generateShemaMixin(schemas, outputPaths, skipFields = [], from = "@mat3ra/esse/dist/js/types") {
180183
// Setup schemas
181184
JSONSchemasInterface_1.default.setSchemas(schemas);
182185
console.log("Generating mixin properties for all schemas...");
@@ -191,7 +194,7 @@ function generateShemaMixin(schemas, outputPaths, skipFields = []) {
191194
if (!outputPath) {
192195
throw new Error(`No output path defined for schema: ${schemaId}`);
193196
}
194-
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields);
197+
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields, from);
195198
// Ensure the directory exists
196199
const dir = outputPath.substring(0, outputPath.lastIndexOf("/"));
197200
if (!fs_1.default.existsSync(dir)) {

src/js/generateSchemaMixin.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function extractSchemaProperties(schema: JSONSchema7): {
7575
* @param mixinTypeName - Name of the mixin type
7676
* @param entityTypeName - Name of the entity type
7777
* @param skipFields - Array of field names to skip
78+
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
7879
* @returns - Generated TypeScript code
7980
*/
8081
function generateMixinFunction(
@@ -83,6 +84,7 @@ function generateMixinFunction(
8384
mixinTypeName: string,
8485
entityTypeName: string,
8586
skipFields: string[] = [],
87+
from = "@mat3ra/esse/dist/js/types",
8688
): string {
8789
// Convert mixin type name to camelCase for function name
8890
const functionName = mixinTypeName.charAt(0).toLowerCase() + mixinTypeName.slice(1);
@@ -100,7 +102,7 @@ function generateMixinFunction(
100102
);
101103

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

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

186190
// Generate the complete mixin function
187-
return generateMixinFunction(schema, schemaName, mixinTypeName, entityTypeName, skipFields);
191+
return generateMixinFunction(
192+
schema,
193+
schemaName,
194+
mixinTypeName,
195+
entityTypeName,
196+
skipFields,
197+
from,
198+
);
188199
}
189200

190201
/**
@@ -213,12 +224,14 @@ function runESLintAutofix(filePaths: string[]): void {
213224
* @param schemas - Array of JSON schemas to use for generation
214225
* @param outputPaths - Object mapping schema IDs to output file paths
215226
* @param skipFields - Array of field names to skip during generation
227+
* @param from - Import path for the schema type (default: "@mat3ra/esse/dist/js/types")
216228
* @returns - Object with success and error counts
217229
*/
218230
function generateShemaMixin(
219231
schemas: JSONSchema7[],
220232
outputPaths: Record<string, string>,
221233
skipFields: string[] = [],
234+
from = "@mat3ra/esse/dist/js/types",
222235
) {
223236
// Setup schemas
224237
JSONSchemasInterface.setSchemas(schemas);
@@ -239,7 +252,7 @@ function generateShemaMixin(
239252
throw new Error(`No output path defined for schema: ${schemaId}`);
240253
}
241254

242-
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields);
255+
const generatedCode = generateMixinFromSchemaId(schemaId, outputPath, skipFields, from);
243256

244257
// Ensure the directory exists
245258
const dir = outputPath.substring(0, outputPath.lastIndexOf("/"));

0 commit comments

Comments
 (0)