Skip to content

Commit cf60cc2

Browse files
committed
update: add generateSchemaMixin + node22 build fix
1 parent 27e0b50 commit cf60cc2

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

dist/js/generateSchemaMixin.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
#!/usr/bin/env node
2+
import type { JSONSchema7 } from "json-schema";
23
/**
34
* Generates mixins for multiple schemas
5+
* @param schemas - Array of JSON schemas to use for generation
46
* @param outputPaths - Object mapping schema IDs to output file paths
57
* @param skipFields - Array of field names to skip during generation
68
* @returns - Object with success and error counts
79
*/
8-
declare function generateShemaMixin(outputPaths: Record<string, string>, skipFields?: string[]): {
10+
declare function generateShemaMixin(schemas: JSONSchema7[], outputPaths: Record<string, string>, skipFields?: string[]): {
911
successCount: number;
1012
errorCount: number;
1113
};
1214
/**
1315
* @example
1416
* ```ts
1517
* import generateShemaMixin from "@mat3ra/code/dist/js/generateSchemaMixin";
18+
* import allSchemas from "@mat3ra/esse/dist/js/schemas.json";
1619
*
17-
* const result = generateShemaMixin(OUTPUT_PATHS, SKIP_FIELDS);
20+
* const result = generateShemaMixin(allSchemas, OUTPUT_PATHS, SKIP_FIELDS);
1821
*
1922
* if (result.errorCount > 0) {
2023
* process.exit(1);

dist/js/generateSchemaMixin.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
1515
* node scripts/generate-mixin-properties.js
1616
*/
1717
const JSONSchemasInterface_1 = __importDefault(require("@mat3ra/esse/dist/js/esse/JSONSchemasInterface"));
18-
const schemas_json_1 = __importDefault(require("@mat3ra/esse/dist/js/schemas.json"));
1918
const child_process_1 = require("child_process");
2019
const fs_1 = __importDefault(require("fs"));
2120
/**
@@ -172,13 +171,14 @@ function runESLintAutofix(filePaths) {
172171
}
173172
/**
174173
* Generates mixins for multiple schemas
174+
* @param schemas - Array of JSON schemas to use for generation
175175
* @param outputPaths - Object mapping schema IDs to output file paths
176176
* @param skipFields - Array of field names to skip during generation
177177
* @returns - Object with success and error counts
178178
*/
179-
function generateShemaMixin(outputPaths, skipFields = []) {
179+
function generateShemaMixin(schemas, outputPaths, skipFields = []) {
180180
// Setup schemas
181-
JSONSchemasInterface_1.default.setSchemas(schemas_json_1.default);
181+
JSONSchemasInterface_1.default.setSchemas(schemas);
182182
console.log("Generating mixin properties for all schemas...");
183183
const schemaIds = Object.keys(outputPaths);
184184
let successCount = 0;
@@ -225,8 +225,9 @@ function generateShemaMixin(outputPaths, skipFields = []) {
225225
* @example
226226
* ```ts
227227
* import generateShemaMixin from "@mat3ra/code/dist/js/generateSchemaMixin";
228+
* import allSchemas from "@mat3ra/esse/dist/js/schemas.json";
228229
*
229-
* const result = generateShemaMixin(OUTPUT_PATHS, SKIP_FIELDS);
230+
* const result = generateShemaMixin(allSchemas, OUTPUT_PATHS, SKIP_FIELDS);
230231
*
231232
* if (result.errorCount > 0) {
232233
* process.exit(1);

src/js/generateSchemaMixin.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313

1414
import JSONSchemasInterface from "@mat3ra/esse/dist/js/esse/JSONSchemasInterface";
15-
import allSchemas from "@mat3ra/esse/dist/js/schemas.json";
1615
import { execSync } from "child_process";
1716
import fs from "fs";
1817
import type { JSONSchema7 } from "json-schema";
@@ -211,13 +210,18 @@ function runESLintAutofix(filePaths: string[]): void {
211210

212211
/**
213212
* Generates mixins for multiple schemas
213+
* @param schemas - Array of JSON schemas to use for generation
214214
* @param outputPaths - Object mapping schema IDs to output file paths
215215
* @param skipFields - Array of field names to skip during generation
216216
* @returns - Object with success and error counts
217217
*/
218-
function generateShemaMixin(outputPaths: Record<string, string>, skipFields: string[] = []) {
218+
function generateShemaMixin(
219+
schemas: JSONSchema7[],
220+
outputPaths: Record<string, string>,
221+
skipFields: string[] = [],
222+
) {
219223
// Setup schemas
220-
JSONSchemasInterface.setSchemas(allSchemas as JSONSchema7[]);
224+
JSONSchemasInterface.setSchemas(schemas);
221225

222226
console.log("Generating mixin properties for all schemas...");
223227

@@ -277,8 +281,9 @@ function generateShemaMixin(outputPaths: Record<string, string>, skipFields: str
277281
* @example
278282
* ```ts
279283
* import generateShemaMixin from "@mat3ra/code/dist/js/generateSchemaMixin";
284+
* import allSchemas from "@mat3ra/esse/dist/js/schemas.json";
280285
*
281-
* const result = generateShemaMixin(OUTPUT_PATHS, SKIP_FIELDS);
286+
* const result = generateShemaMixin(allSchemas, OUTPUT_PATHS, SKIP_FIELDS);
282287
*
283288
* if (result.errorCount > 0) {
284289
* process.exit(1);

tests/js/generateSchemaMixin.tests.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
/* eslint-disable no-unused-expressions */
22
import { expect } from "chai";
33
import fs from "fs";
4+
import type { JSONSchema7 } from "json-schema";
45
import path from "path";
56

67
import generateShemaMixin from "../../src/js/generateSchemaMixin";
78

89
describe("generateSchemaMixin Tests", () => {
910
const tempDir = path.join(__dirname, "temp_test_output");
1011

11-
beforeEach(() => {
12+
// Mock schemas for testing
13+
const mockSchemas: JSONSchema7[] = [
14+
{
15+
$id: "property/holder",
16+
title: "Property Holder",
17+
type: "object",
18+
properties: {
19+
metadata: { type: "object" },
20+
name: { type: "string" },
21+
description: { type: "string" },
22+
},
23+
required: ["name"],
24+
},
25+
];
26+
27+
beforeEach(function setupTests() {
28+
// Increase timeout to 10 seconds because generateShemaMixin runs ESLint autofix
29+
// which can take several seconds to complete, especially on first run
30+
this.timeout(10000);
1231
// Create a temporary directory for test files
1332
if (!fs.existsSync(tempDir)) {
1433
fs.mkdirSync(tempDir, { recursive: true });
@@ -41,7 +60,7 @@ describe("generateSchemaMixin Tests", () => {
4160
it("should handle empty output paths object", () => {
4261
const outputPaths = {};
4362

44-
const result = generateShemaMixin(outputPaths);
63+
const result = generateShemaMixin(mockSchemas, outputPaths);
4564

4665
expect(result.successCount).to.equal(0);
4766
expect(result.errorCount).to.equal(0);
@@ -53,7 +72,7 @@ describe("generateSchemaMixin Tests", () => {
5372
"property/holder": path.join(nestedDir, "PropertyHolderSchemaMixin.ts"),
5473
};
5574

56-
generateShemaMixin(outputPaths);
75+
generateShemaMixin(mockSchemas, outputPaths);
5776

5877
// The function should attempt to create the directory
5978
expect(fs.existsSync(nestedDir)).to.be.true;
@@ -67,7 +86,7 @@ describe("generateSchemaMixin Tests", () => {
6786

6887
// This should not throw an error even if the schema doesn't exist
6988
expect(() => {
70-
generateShemaMixin(outputPaths, skipFields);
89+
generateShemaMixin(mockSchemas, outputPaths, skipFields);
7190
}).to.not.throw();
7291
});
7392
});
@@ -78,7 +97,7 @@ describe("generateSchemaMixin Tests", () => {
7897
"non/existent": path.join(tempDir, "NonExistentSchemaMixin.ts"),
7998
};
8099

81-
const result = generateShemaMixin(outputPaths);
100+
const result = generateShemaMixin(mockSchemas, outputPaths);
82101

83102
expect(result.successCount).to.equal(0);
84103
expect(result.errorCount).to.equal(1);
@@ -89,7 +108,7 @@ describe("generateSchemaMixin Tests", () => {
89108
"property/holder": undefined as unknown as string,
90109
};
91110

92-
const result = generateShemaMixin(outputPaths);
111+
const result = generateShemaMixin(mockSchemas, outputPaths);
93112

94113
expect(result.successCount).to.equal(0);
95114
expect(result.errorCount).to.equal(1);
@@ -100,7 +119,7 @@ describe("generateSchemaMixin Tests", () => {
100119
"property/holder": "",
101120
};
102121

103-
const result = generateShemaMixin(outputPaths);
122+
const result = generateShemaMixin(mockSchemas, outputPaths);
104123

105124
expect(result.successCount).to.equal(0);
106125
expect(result.errorCount).to.equal(1);
@@ -111,7 +130,7 @@ describe("generateSchemaMixin Tests", () => {
111130
it("should return an object with successCount and errorCount", () => {
112131
const outputPaths = {};
113132

114-
const result = generateShemaMixin(outputPaths);
133+
const result = generateShemaMixin(mockSchemas, outputPaths);
115134

116135
expect(result).to.have.property("successCount");
117136
expect(result).to.have.property("errorCount");
@@ -125,36 +144,44 @@ describe("generateSchemaMixin Tests", () => {
125144
"another/non/existent": path.join(tempDir, "AnotherNonExistentSchemaMixin.ts"),
126145
};
127146

128-
const result = generateShemaMixin(outputPaths);
147+
const result = generateShemaMixin(mockSchemas, outputPaths);
129148

130149
expect(result.successCount).to.equal(0);
131150
expect(result.errorCount).to.equal(2);
132151
});
133152
});
134153

135154
describe("generateShemaMixin - Function Interface", () => {
136-
it("should accept outputPaths as first parameter", () => {
155+
it("should accept schemas as first parameter", () => {
137156
const outputPaths = {};
138157

139158
expect(() => {
140-
generateShemaMixin(outputPaths);
159+
generateShemaMixin(mockSchemas, outputPaths);
141160
}).to.not.throw();
142161
});
143162

144-
it("should accept skipFields as optional second parameter", () => {
163+
it("should accept outputPaths as second parameter", () => {
164+
const outputPaths = {};
165+
166+
expect(() => {
167+
generateShemaMixin(mockSchemas, outputPaths);
168+
}).to.not.throw();
169+
});
170+
171+
it("should accept skipFields as optional third parameter", () => {
145172
const outputPaths = {};
146173
const skipFields = ["field1", "field2"];
147174

148175
expect(() => {
149-
generateShemaMixin(outputPaths, skipFields);
176+
generateShemaMixin(mockSchemas, outputPaths, skipFields);
150177
}).to.not.throw();
151178
});
152179

153180
it("should work without skipFields parameter", () => {
154181
const outputPaths = {};
155182

156183
expect(() => {
157-
generateShemaMixin(outputPaths);
184+
generateShemaMixin(mockSchemas, outputPaths);
158185
}).to.not.throw();
159186
});
160187
});

0 commit comments

Comments
 (0)