docusaurus-plugin-generate-schema-docs 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -6
- package/__tests__/ExampleDataLayer.test.js +78 -155
- package/__tests__/__fixtures__/static/schemas/add-to-cart-event.json +4 -15
- package/__tests__/__fixtures__/static/schemas/choice-event.json +72 -0
- package/__tests__/__fixtures__/static/schemas/components/dataLayer.json +52 -54
- package/__tests__/__fixtures__/static/schemas/components/product.json +124 -210
- package/__tests__/__fixtures__/static/schemas/nested/child-event.json +10 -0
- package/__tests__/__fixtures__/static/schemas/nested/grandchild-a.json +9 -0
- package/__tests__/__fixtures__/static/schemas/nested/grandchild-b.json +9 -0
- package/__tests__/__fixtures__/static/schemas/nested/parent-event.json +7 -0
- package/__tests__/__fixtures__/static/schemas/root-any-of-event.json +34 -0
- package/__tests__/__fixtures__/static/schemas/root-choice-event.json +36 -0
- package/__tests__/__fixtures__/validateSchemas/circular-schema.json +6 -6
- package/__tests__/__fixtures__/validateSchemas/components/referenced.json +9 -7
- package/__tests__/__fixtures__/validateSchemas/invalid-example-schema.json +7 -7
- package/__tests__/__fixtures__/validateSchemas/main-schema-with-missing-ref.json +7 -7
- package/__tests__/__fixtures__/validateSchemas/main-schema-with-ref.json +7 -7
- package/__tests__/__fixtures__/validateSchemas/no-example-schema.json +11 -11
- package/__tests__/__fixtures__/validateSchemas/schema-A.json +5 -5
- package/__tests__/__fixtures__/validateSchemas/schema-B.json +5 -5
- package/__tests__/__fixtures__/validateSchemas/valid-schema.json +7 -7
- package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/add-to-cart-event.json +44 -0
- package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/dataLayer.json +56 -0
- package/__tests__/__fixtures_versioned__/static/schemas/1.1.1/components/product.json +125 -0
- package/__tests__/__fixtures_versioned__/static/schemas/next/add-to-cart-event.json +44 -0
- package/__tests__/__fixtures_versioned__/static/schemas/next/components/dataLayer.json +56 -0
- package/__tests__/__fixtures_versioned__/static/schemas/next/components/product.json +125 -0
- package/__tests__/__fixtures_versioned__/versions.json +1 -0
- package/__tests__/__snapshots__/ExampleDataLayer.test.js.snap +117 -0
- package/__tests__/__snapshots__/generateEventDocs.nested.test.js.snap +92 -0
- package/__tests__/__snapshots__/generateEventDocs.test.js.snap +113 -15
- package/__tests__/__snapshots__/generateEventDocs.versioned.test.js.snap +53 -0
- package/__tests__/components/FoldableRows.test.js +330 -0
- package/__tests__/components/PropertiesTable.test.js +31 -14
- package/__tests__/components/PropertyRow.test.js +471 -51
- package/__tests__/components/SchemaJsonViewer.test.js +23 -19
- package/__tests__/components/SchemaRows.test.js +96 -66
- package/__tests__/components/SchemaViewer.test.js +34 -17
- package/__tests__/components/TableHeader.test.js +12 -12
- package/__tests__/generateEventDocs.nested.test.js +80 -0
- package/__tests__/generateEventDocs.test.js +77 -71
- package/__tests__/generateEventDocs.versioned.test.js +69 -0
- package/__tests__/helpers/buildExampleFromSchema.test.js +160 -160
- package/__tests__/helpers/file-system.test.js +44 -0
- package/__tests__/helpers/getConstraints.test.js +48 -48
- package/__tests__/helpers/loadSchema.test.js +11 -5
- package/__tests__/helpers/path-helpers.test.js +34 -0
- package/__tests__/helpers/processSchema.test.js +42 -22
- package/__tests__/helpers/schema-processing.test.js +82 -0
- package/__tests__/helpers/schemaToExamples.test.js +56 -0
- package/__tests__/helpers/schemaToTableData.filtering.test.js +65 -0
- package/__tests__/helpers/schemaToTableData.hierarchicalLines.test.js +539 -0
- package/__tests__/helpers/schemaToTableData.test.js +222 -0
- package/__tests__/helpers/update-schema-ids.test.js +107 -0
- package/__tests__/update-schema-ids.test.js +39 -0
- package/__tests__/validateSchemas.test.js +125 -88
- package/components/ExampleDataLayer.js +59 -28
- package/components/FoldableRows.js +164 -0
- package/components/PropertiesTable.js +10 -7
- package/components/PropertyRow.js +169 -60
- package/components/SchemaJsonViewer.js +6 -6
- package/components/SchemaRows.css +236 -14
- package/components/SchemaRows.js +24 -41
- package/components/SchemaViewer.js +19 -13
- package/components/TableHeader.js +12 -12
- package/generateEventDocs.js +141 -61
- package/helpers/buildExampleFromSchema.js +58 -72
- package/helpers/choice-index-template.js +22 -0
- package/helpers/file-system.js +32 -0
- package/helpers/getConstraints.js +43 -44
- package/helpers/loadSchema.js +2 -2
- package/helpers/path-helpers.js +22 -0
- package/helpers/processSchema.js +19 -19
- package/helpers/{mdx-template.js → schema-doc-template.js} +12 -12
- package/helpers/schema-processing.js +75 -0
- package/helpers/schemaToExamples.js +99 -0
- package/helpers/schemaToTableData.js +311 -0
- package/helpers/update-schema-ids.js +47 -0
- package/index.js +143 -54
- package/package.json +1 -1
- package/validateSchemas.js +54 -71
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { getConstraints } from './getConstraints';
|
|
2
|
+
|
|
3
|
+
function processOptions(
|
|
4
|
+
choices,
|
|
5
|
+
level,
|
|
6
|
+
path,
|
|
7
|
+
isNestedInProperty,
|
|
8
|
+
requiredArray = [],
|
|
9
|
+
continuingLevels = [],
|
|
10
|
+
) {
|
|
11
|
+
return choices.map((optionSchema, index) => {
|
|
12
|
+
const optionTitle = optionSchema.title || 'Option';
|
|
13
|
+
|
|
14
|
+
// Determine if this is the last option in the list.
|
|
15
|
+
// If it is NOT the last option, its children must not close the visual tree branch.
|
|
16
|
+
const isLastOption = index === choices.length - 1;
|
|
17
|
+
|
|
18
|
+
let optionRows = [];
|
|
19
|
+
|
|
20
|
+
// This is a primitive type (string, number, etc.) within a choice
|
|
21
|
+
if (optionSchema.type && !optionSchema.properties) {
|
|
22
|
+
const isRequired = requiredArray.includes(path[path.length - 1]);
|
|
23
|
+
const constraints = getConstraints(optionSchema);
|
|
24
|
+
if (isRequired) {
|
|
25
|
+
constraints.unshift('required');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
optionRows.push({
|
|
29
|
+
type: 'property',
|
|
30
|
+
// The name of the property is the name of the parent property that holds the choice
|
|
31
|
+
name: path.length > 0 ? path[path.length - 1] : optionTitle,
|
|
32
|
+
path: [...path, `(${optionTitle})`],
|
|
33
|
+
// If it's a top-level choice (like user_id), the level is the same as the choice itself.
|
|
34
|
+
// Otherwise, it's nested.
|
|
35
|
+
level: level,
|
|
36
|
+
required: isRequired,
|
|
37
|
+
propertyType: optionSchema.type,
|
|
38
|
+
description: optionSchema.description,
|
|
39
|
+
example: optionSchema.examples || optionSchema.example,
|
|
40
|
+
constraints: constraints,
|
|
41
|
+
isLastInGroup: isLastOption, // Updated: Uses the calculated flag instead of always true
|
|
42
|
+
hasChildren: false,
|
|
43
|
+
containerType: null,
|
|
44
|
+
continuingLevels: [...continuingLevels],
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
// This is a complex object within a choice
|
|
48
|
+
optionRows = schemaToTableData(
|
|
49
|
+
optionSchema,
|
|
50
|
+
// If nested in a property (like payment_method), the sub-properties start at the same level as the choice
|
|
51
|
+
// Otherwise, they are one level deeper.
|
|
52
|
+
level,
|
|
53
|
+
isNestedInProperty ? [] : path,
|
|
54
|
+
continuingLevels,
|
|
55
|
+
isLastOption,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
title: optionTitle,
|
|
61
|
+
description: optionSchema.description,
|
|
62
|
+
rows: optionRows,
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function schemaToTableData(
|
|
68
|
+
schema,
|
|
69
|
+
level = 0,
|
|
70
|
+
path = [],
|
|
71
|
+
parentContinuingLevels = [],
|
|
72
|
+
isLastOption = true,
|
|
73
|
+
) {
|
|
74
|
+
const flatRows = [];
|
|
75
|
+
|
|
76
|
+
function isEffectivelyEmpty(schemaNode) {
|
|
77
|
+
if (
|
|
78
|
+
schemaNode.type !== 'object' &&
|
|
79
|
+
typeof schemaNode.properties === 'undefined'
|
|
80
|
+
) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
if (schemaNode.oneOf || schemaNode.anyOf) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (
|
|
87
|
+
!schemaNode.properties ||
|
|
88
|
+
Object.keys(schemaNode.properties).length === 0
|
|
89
|
+
) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
return Object.values(schemaNode.properties).every(isEffectivelyEmpty);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function buildRows(
|
|
96
|
+
subSchema,
|
|
97
|
+
currentLevel,
|
|
98
|
+
currentPath,
|
|
99
|
+
requiredFromParent = [],
|
|
100
|
+
continuingLevels = [],
|
|
101
|
+
) {
|
|
102
|
+
if (!subSchema) return;
|
|
103
|
+
|
|
104
|
+
if (subSchema.properties) {
|
|
105
|
+
const propKeys = Object.keys(subSchema.properties);
|
|
106
|
+
const hasSiblingChoices = !!(subSchema.oneOf || subSchema.anyOf);
|
|
107
|
+
|
|
108
|
+
// Filter out properties that should be skipped to get accurate count
|
|
109
|
+
const visiblePropKeys = propKeys.filter((name) => {
|
|
110
|
+
const propSchema = subSchema.properties[name];
|
|
111
|
+
return !(
|
|
112
|
+
propSchema['x-gtm-clear'] === true && isEffectivelyEmpty(propSchema)
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
visiblePropKeys.forEach((name, index) => {
|
|
117
|
+
const propSchema = subSchema.properties[name];
|
|
118
|
+
const newPath = [...currentPath, name];
|
|
119
|
+
|
|
120
|
+
const isLastProp =
|
|
121
|
+
index === visiblePropKeys.length - 1 && !hasSiblingChoices;
|
|
122
|
+
|
|
123
|
+
// Updated Logic:
|
|
124
|
+
// A property is visually "last" only if it is the last property
|
|
125
|
+
// AND (it is deeper in the hierarchy OR the parent option itself is the last one).
|
|
126
|
+
const isLast = isLastProp && (currentLevel !== level || isLastOption);
|
|
127
|
+
|
|
128
|
+
const isChoiceWrapper = !!(propSchema.oneOf || propSchema.anyOf);
|
|
129
|
+
|
|
130
|
+
// Determine if this property has children and what type
|
|
131
|
+
const hasNestedProperties = !!propSchema.properties;
|
|
132
|
+
const hasArrayItems =
|
|
133
|
+
propSchema.type === 'array' && !!propSchema.items?.properties;
|
|
134
|
+
const hasNestedChoice = isChoiceWrapper;
|
|
135
|
+
const hasChildren =
|
|
136
|
+
hasNestedProperties || hasArrayItems || hasNestedChoice;
|
|
137
|
+
|
|
138
|
+
// Determine container type for the symbol
|
|
139
|
+
let containerType = null;
|
|
140
|
+
if (
|
|
141
|
+
hasNestedProperties ||
|
|
142
|
+
(isChoiceWrapper && propSchema.type === 'object')
|
|
143
|
+
) {
|
|
144
|
+
containerType = 'object';
|
|
145
|
+
} else if (hasArrayItems) {
|
|
146
|
+
containerType = 'array';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Calculate continuing levels for children
|
|
150
|
+
// If this is not the last item, add current level to continuing levels for children
|
|
151
|
+
// If this IS the last item, remove the immediate parent level (currentLevel - 1) because
|
|
152
|
+
// that line stops at this item and should not continue through its children
|
|
153
|
+
const childContinuingLevels = isLast
|
|
154
|
+
? continuingLevels.filter((lvl) => lvl !== currentLevel - 1)
|
|
155
|
+
: [...continuingLevels, currentLevel];
|
|
156
|
+
|
|
157
|
+
// This is a "simple" choice property like user_id.
|
|
158
|
+
// It gets unwrapped into a choice row directly.
|
|
159
|
+
if (
|
|
160
|
+
isChoiceWrapper &&
|
|
161
|
+
!propSchema.properties &&
|
|
162
|
+
propSchema.type !== 'object'
|
|
163
|
+
) {
|
|
164
|
+
const choiceType = propSchema.oneOf ? 'oneOf' : 'anyOf';
|
|
165
|
+
const choices = propSchema[choiceType];
|
|
166
|
+
flatRows.push({
|
|
167
|
+
type: 'choice',
|
|
168
|
+
choiceType,
|
|
169
|
+
path: newPath,
|
|
170
|
+
level: currentLevel,
|
|
171
|
+
title: propSchema.title,
|
|
172
|
+
description: propSchema.description,
|
|
173
|
+
isLastInGroup: isLast,
|
|
174
|
+
hasChildren: false,
|
|
175
|
+
containerType: null,
|
|
176
|
+
continuingLevels: [...continuingLevels],
|
|
177
|
+
options: processOptions(
|
|
178
|
+
choices,
|
|
179
|
+
currentLevel,
|
|
180
|
+
newPath,
|
|
181
|
+
false,
|
|
182
|
+
subSchema.required || requiredFromParent,
|
|
183
|
+
childContinuingLevels,
|
|
184
|
+
),
|
|
185
|
+
});
|
|
186
|
+
} else {
|
|
187
|
+
// This is a "normal" property or a complex one with a nested choice.
|
|
188
|
+
const isRequired =
|
|
189
|
+
(subSchema.required || requiredFromParent)?.includes(name) || false;
|
|
190
|
+
const constraints = getConstraints(propSchema);
|
|
191
|
+
if (isRequired) {
|
|
192
|
+
constraints.unshift('required');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
flatRows.push({
|
|
196
|
+
type: 'property',
|
|
197
|
+
name,
|
|
198
|
+
path: newPath,
|
|
199
|
+
level: currentLevel,
|
|
200
|
+
required: isRequired,
|
|
201
|
+
propertyType:
|
|
202
|
+
propSchema.type || (propSchema.enum ? 'enum' : 'object'),
|
|
203
|
+
description: propSchema.description,
|
|
204
|
+
example: propSchema.examples || propSchema.example,
|
|
205
|
+
constraints,
|
|
206
|
+
isLastInGroup: isLast,
|
|
207
|
+
hasChildren,
|
|
208
|
+
containerType,
|
|
209
|
+
continuingLevels: [...continuingLevels],
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (propSchema.properties) {
|
|
213
|
+
buildRows(
|
|
214
|
+
propSchema,
|
|
215
|
+
currentLevel + 1,
|
|
216
|
+
newPath,
|
|
217
|
+
propSchema.required,
|
|
218
|
+
childContinuingLevels,
|
|
219
|
+
);
|
|
220
|
+
} else if (
|
|
221
|
+
propSchema.type === 'array' &&
|
|
222
|
+
propSchema.items?.properties
|
|
223
|
+
) {
|
|
224
|
+
buildRows(
|
|
225
|
+
propSchema.items,
|
|
226
|
+
currentLevel + 1,
|
|
227
|
+
[...newPath, '[n]'],
|
|
228
|
+
propSchema.items.required,
|
|
229
|
+
childContinuingLevels,
|
|
230
|
+
);
|
|
231
|
+
} else if (isChoiceWrapper) {
|
|
232
|
+
// This handles the "complex" choice property like payment_method.
|
|
233
|
+
// A property row has already been created above, now we add the choice row.
|
|
234
|
+
const choiceType = propSchema.oneOf ? 'oneOf' : 'anyOf';
|
|
235
|
+
const choices = propSchema[choiceType];
|
|
236
|
+
flatRows.push({
|
|
237
|
+
type: 'choice',
|
|
238
|
+
choiceType,
|
|
239
|
+
path: [...newPath, choiceType], // Make path unique
|
|
240
|
+
level: currentLevel + 1,
|
|
241
|
+
title: propSchema.title,
|
|
242
|
+
description: null,
|
|
243
|
+
isLastInGroup: true,
|
|
244
|
+
hasChildren: false,
|
|
245
|
+
containerType: null,
|
|
246
|
+
continuingLevels: childContinuingLevels,
|
|
247
|
+
options: processOptions(
|
|
248
|
+
choices,
|
|
249
|
+
currentLevel + 1,
|
|
250
|
+
newPath,
|
|
251
|
+
true,
|
|
252
|
+
propSchema.required,
|
|
253
|
+
childContinuingLevels,
|
|
254
|
+
),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// This handles choices at the root of a schema
|
|
262
|
+
const choiceType = subSchema.oneOf
|
|
263
|
+
? 'oneOf'
|
|
264
|
+
: subSchema.anyOf
|
|
265
|
+
? 'anyOf'
|
|
266
|
+
: null;
|
|
267
|
+
if (choiceType) {
|
|
268
|
+
const choices = subSchema[choiceType];
|
|
269
|
+
flatRows.push({
|
|
270
|
+
type: 'choice',
|
|
271
|
+
choiceType,
|
|
272
|
+
path: currentPath,
|
|
273
|
+
level: currentLevel,
|
|
274
|
+
title: subSchema.title,
|
|
275
|
+
description: subSchema.description,
|
|
276
|
+
isLastInGroup: true,
|
|
277
|
+
hasChildren: false,
|
|
278
|
+
containerType: null,
|
|
279
|
+
continuingLevels: [...continuingLevels],
|
|
280
|
+
options: processOptions(
|
|
281
|
+
choices,
|
|
282
|
+
currentLevel,
|
|
283
|
+
currentPath,
|
|
284
|
+
false,
|
|
285
|
+
subSchema.required || requiredFromParent,
|
|
286
|
+
continuingLevels,
|
|
287
|
+
),
|
|
288
|
+
});
|
|
289
|
+
} else if (!subSchema.properties && subSchema.type) {
|
|
290
|
+
// This handles a schema that is just a single primitive type
|
|
291
|
+
flatRows.push({
|
|
292
|
+
type: 'property',
|
|
293
|
+
name: subSchema.title || '<value>',
|
|
294
|
+
path: currentPath,
|
|
295
|
+
level: currentLevel,
|
|
296
|
+
required: false,
|
|
297
|
+
propertyType: subSchema.type,
|
|
298
|
+
description: subSchema.description,
|
|
299
|
+
example: subSchema.examples || subSchema.example,
|
|
300
|
+
constraints: getConstraints(subSchema),
|
|
301
|
+
isLastInGroup: true,
|
|
302
|
+
hasChildren: false,
|
|
303
|
+
containerType: null,
|
|
304
|
+
continuingLevels: [...continuingLevels],
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
buildRows(schema, level, path, schema.required, parentContinuingLevels);
|
|
310
|
+
return flatRows;
|
|
311
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
export default function updateSchemaIds(siteDir, url, version = null) {
|
|
5
|
+
const versionsJsonPath = path.join(siteDir, 'versions.json');
|
|
6
|
+
if (!fs.existsSync(versionsJsonPath)) {
|
|
7
|
+
console.log('No versions.json file found, skipping schema ID update.');
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const getAllFiles = (dir, allFiles = []) => {
|
|
12
|
+
const files = fs.readdirSync(dir);
|
|
13
|
+
|
|
14
|
+
files.forEach((file) => {
|
|
15
|
+
const filePath = path.join(dir, file);
|
|
16
|
+
if (fs.statSync(filePath).isDirectory()) {
|
|
17
|
+
getAllFiles(filePath, allFiles);
|
|
18
|
+
} else {
|
|
19
|
+
if (file.endsWith('.json')) {
|
|
20
|
+
allFiles.push(filePath);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return allFiles;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const versions = version
|
|
28
|
+
? [version]
|
|
29
|
+
: JSON.parse(fs.readFileSync(versionsJsonPath, 'utf8'));
|
|
30
|
+
|
|
31
|
+
for (const version of versions) {
|
|
32
|
+
const schemaDir = path.join(siteDir, 'static/schemas', version);
|
|
33
|
+
if (!fs.existsSync(schemaDir)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const files = getAllFiles(schemaDir);
|
|
37
|
+
|
|
38
|
+
for (const file of files) {
|
|
39
|
+
const schema = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
40
|
+
const baseUrl = url.endsWith('/') ? url.slice(0, -1) : url;
|
|
41
|
+
const relativePath = path.relative(path.join(siteDir, 'static'), file);
|
|
42
|
+
schema.$id = `${baseUrl}/${relativePath.replace(/\\/g, '/')}`;
|
|
43
|
+
fs.writeFileSync(file, JSON.stringify(schema, null, 2));
|
|
44
|
+
console.log(`Updated $id for ${file} in version ${version}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
package/index.js
CHANGED
|
@@ -1,60 +1,149 @@
|
|
|
1
|
+
import { getPathsForVersion } from './helpers/path-helpers.js';
|
|
2
|
+
import updateSchemaIds from './helpers/update-schema-ids.js';
|
|
3
|
+
import fs from 'fs';
|
|
1
4
|
import validateSchemas from './validateSchemas.js';
|
|
2
5
|
import generateEventDocs from './generateEventDocs.js';
|
|
3
6
|
import path from 'path';
|
|
4
7
|
|
|
5
8
|
export default async function (context) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
9
|
+
const { siteDir } = context;
|
|
10
|
+
const { organizationName, projectName, url } = context.siteConfig;
|
|
11
|
+
const options = { organizationName, projectName, siteDir, url };
|
|
12
|
+
const versionsJsonPath = path.join(siteDir, 'versions.json');
|
|
13
|
+
const isVersioned = fs.existsSync(versionsJsonPath);
|
|
14
|
+
|
|
15
|
+
const extendCli = (cli) => {
|
|
16
|
+
cli
|
|
17
|
+
.command('validate-schemas [version]')
|
|
18
|
+
.description('Validate JSON Schemas with the examples inside the schemas')
|
|
19
|
+
.action(async (version) => {
|
|
20
|
+
console.log('Validating GTM Schemas...');
|
|
21
|
+
const schemaVersion = version || 'next';
|
|
22
|
+
const { schemaDir } = getPathsForVersion(
|
|
23
|
+
schemaVersion,
|
|
24
|
+
context.siteDir,
|
|
25
|
+
);
|
|
26
|
+
const success = await validateSchemas(schemaDir);
|
|
27
|
+
if (!success) {
|
|
28
|
+
console.error('Validation failed.');
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
console.log('✅ All schemas and examples are valid!');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
cli
|
|
35
|
+
.command('generate-schema-docs')
|
|
36
|
+
.description('Generate schema documentation from JSON schemas')
|
|
37
|
+
.action(async () => {
|
|
38
|
+
if (isVersioned) {
|
|
39
|
+
const versions = JSON.parse(
|
|
40
|
+
fs.readFileSync(versionsJsonPath, 'utf8'),
|
|
41
|
+
);
|
|
42
|
+
for (const version of versions) {
|
|
43
|
+
await generateEventDocs({ ...options, version });
|
|
44
|
+
}
|
|
45
|
+
await generateEventDocs({ ...options, version: 'current' });
|
|
46
|
+
} else {
|
|
47
|
+
await generateEventDocs(options);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
cli
|
|
52
|
+
.command('update-schema-ids [version]')
|
|
53
|
+
.description('Update the $id of the versioned schemas')
|
|
54
|
+
.action((version) => {
|
|
55
|
+
updateSchemaIds(siteDir, url, version);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
cli
|
|
59
|
+
.command('version-with-schemas <version>')
|
|
60
|
+
.description(
|
|
61
|
+
'Create a new docs version and copy schemas from next to versioned folder',
|
|
62
|
+
)
|
|
63
|
+
.action(async (version) => {
|
|
64
|
+
const { execSync } = await import('child_process');
|
|
65
|
+
|
|
66
|
+
console.log(`📦 Creating docs version ${version}...`);
|
|
67
|
+
try {
|
|
68
|
+
// Run the standard docusaurus docs:version command
|
|
69
|
+
execSync(`npx docusaurus docs:version ${version}`, {
|
|
70
|
+
cwd: siteDir,
|
|
71
|
+
stdio: 'inherit',
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error('❌ Failed to create docs version');
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
console.log(`📂 Copying schemas from next to ${version}...`);
|
|
79
|
+
const nextSchemasDir = path.join(siteDir, 'static/schemas/next');
|
|
80
|
+
const versionedSchemasDir = path.join(
|
|
81
|
+
siteDir,
|
|
82
|
+
'static/schemas',
|
|
83
|
+
version,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (!fs.existsSync(nextSchemasDir)) {
|
|
87
|
+
console.error(
|
|
88
|
+
`❌ Source schemas directory not found: ${nextSchemasDir}`,
|
|
89
|
+
);
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Copy the schemas
|
|
94
|
+
try {
|
|
95
|
+
fs.cpSync(nextSchemasDir, versionedSchemasDir, { recursive: true });
|
|
96
|
+
console.log(`✅ Schemas copied to ${versionedSchemasDir}`);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error(`❌ Failed to copy schemas: ${error.message}`);
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Update schema IDs for the new version
|
|
103
|
+
console.log(`🔄 Updating schema $ids for version ${version}...`);
|
|
104
|
+
updateSchemaIds(siteDir, url, version);
|
|
105
|
+
|
|
106
|
+
// Generate documentation for the new version
|
|
107
|
+
console.log(`📝 Generating documentation for version ${version}...`);
|
|
108
|
+
await generateEventDocs({ ...options, version });
|
|
109
|
+
|
|
110
|
+
console.log(`\n✅ Version ${version} created successfully!`);
|
|
111
|
+
console.log(`\nNext steps:`);
|
|
112
|
+
console.log(` 1. Review the changes in static/schemas/${version}/`);
|
|
113
|
+
console.log(
|
|
114
|
+
` 2. Review the changes in versioned_docs/version-${version}/`,
|
|
115
|
+
);
|
|
116
|
+
console.log(` 3. Commit the changes to version control`);
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const schemasPath = isVersioned
|
|
121
|
+
? path.join(siteDir, 'static/schemas/next')
|
|
122
|
+
: path.join(siteDir, 'static/schemas');
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
name: 'docusaurus-plugin-generate-schema-docs',
|
|
126
|
+
|
|
127
|
+
getPathsToWatch() {
|
|
128
|
+
return [schemasPath];
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
async loadContent() {
|
|
132
|
+
if (isVersioned) {
|
|
133
|
+
const versions = JSON.parse(fs.readFileSync(versionsJsonPath, 'utf8'));
|
|
134
|
+
for (const version of versions) {
|
|
135
|
+
await generateEventDocs({ ...options, version });
|
|
136
|
+
}
|
|
137
|
+
await generateEventDocs({ ...options, version: 'current' });
|
|
138
|
+
} else {
|
|
139
|
+
await generateEventDocs(options);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
getThemePath() {
|
|
144
|
+
return './components';
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
extendCli,
|
|
148
|
+
};
|
|
60
149
|
}
|