diff --git a/dist/js/generateSchemaMixin.d.ts b/dist/js/generateSchemaMixin.d.ts index 93c6452..ddb4f4f 100644 --- a/dist/js/generateSchemaMixin.d.ts +++ b/dist/js/generateSchemaMixin.d.ts @@ -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, skipFields?: string[]): { +declare function generateShemaMixin(schemas: JSONSchema7[], outputPaths: Record, skipFields?: string[], from?: string): { successCount: number; errorCount: number; }; diff --git a/dist/js/generateSchemaMixin.js b/dist/js/generateSchemaMixin.js index 1781ecd..d65c061 100644 --- a/dist/js/generateSchemaMixin.js +++ b/dist/js/generateSchemaMixin.js @@ -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 @@ -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`; @@ -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); @@ -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 @@ -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..."); @@ -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)) { diff --git a/src/js/generateSchemaMixin.ts b/src/js/generateSchemaMixin.ts index 0ef4726..704de98 100644 --- a/src/js/generateSchemaMixin.ts +++ b/src/js/generateSchemaMixin.ts @@ -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( @@ -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); @@ -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(" | "); @@ -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); @@ -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, + ); } /** @@ -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, skipFields: string[] = [], + from = "@mat3ra/esse/dist/js/types", ) { // Setup schemas JSONSchemasInterface.setSchemas(schemas); @@ -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("/"));