docusaurus-plugin-generate-schema-docs 1.8.4 → 1.8.5

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.
Files changed (39) hide show
  1. package/README.md +10 -0
  2. package/__tests__/__fixtures__/validateSchemas/schema-with-not-anyof-multi.json +12 -0
  3. package/__tests__/__fixtures__/validateSchemas/schema-with-not-anyof.json +30 -0
  4. package/__tests__/__fixtures__/validateSchemas/schema-with-not-edge-cases.json +24 -0
  5. package/__tests__/__fixtures__/validateSchemas/schema-with-not-non-object.json +15 -0
  6. package/__tests__/generateEventDocs.anchor.test.js +1 -1
  7. package/__tests__/generateEventDocs.nested.test.js +1 -1
  8. package/__tests__/generateEventDocs.partials.test.js +1 -1
  9. package/__tests__/generateEventDocs.test.js +506 -1
  10. package/__tests__/generateEventDocs.versioned.test.js +1 -1
  11. package/__tests__/helpers/buildExampleFromSchema.test.js +240 -0
  12. package/__tests__/helpers/constraintSchemaPaths.test.js +208 -0
  13. package/__tests__/helpers/continuingLinesStyle.test.js +492 -0
  14. package/__tests__/helpers/exampleModel.test.js +209 -0
  15. package/__tests__/helpers/file-system.test.js +73 -1
  16. package/__tests__/helpers/getConstraints.test.js +27 -0
  17. package/__tests__/helpers/mergeSchema.test.js +94 -0
  18. package/__tests__/helpers/processSchema.test.js +291 -1
  19. package/__tests__/helpers/schema-doc-template.test.js +54 -0
  20. package/__tests__/helpers/schema-processing.test.js +122 -2
  21. package/__tests__/helpers/schemaToExamples.test.js +1007 -0
  22. package/__tests__/helpers/schemaToTableData.mutations.test.js +970 -0
  23. package/__tests__/helpers/schemaToTableData.test.js +157 -0
  24. package/__tests__/helpers/snippetTargets.test.js +432 -0
  25. package/__tests__/helpers/trackingTargets.test.js +319 -0
  26. package/__tests__/helpers/validator.test.js +385 -1
  27. package/__tests__/index.test.js +436 -0
  28. package/__tests__/syncGtm.test.js +139 -3
  29. package/__tests__/update-schema-ids.test.js +70 -1
  30. package/__tests__/validateSchemas-integration.test.js +2 -2
  31. package/__tests__/validateSchemas.test.js +142 -1
  32. package/generateEventDocs.js +21 -1
  33. package/helpers/constraintSchemaPaths.js +10 -14
  34. package/helpers/schemaToTableData.js +538 -492
  35. package/helpers/trackingTargets.js +26 -3
  36. package/helpers/validator.js +18 -4
  37. package/index.js +1 -2
  38. package/package.json +1 -1
  39. package/scripts/sync-gtm.js +25 -7
@@ -10,6 +10,28 @@ export const SUPPORTED_TRACKING_TARGETS = [
10
10
 
11
11
  const TARGET_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+){2,}$/;
12
12
 
13
+ function isReferenceAggregatorSchema(schema) {
14
+ if (!schema || typeof schema !== 'object') {
15
+ return false;
16
+ }
17
+
18
+ const hasChoiceAggregation =
19
+ Array.isArray(schema.oneOf) || Array.isArray(schema.anyOf);
20
+ const hasPropertiesKeyword = Object.hasOwn(schema, 'properties');
21
+ const hasValidPropertiesMap =
22
+ hasPropertiesKeyword &&
23
+ schema.properties &&
24
+ typeof schema.properties === 'object' &&
25
+ !Array.isArray(schema.properties);
26
+ const hasOwnProperties =
27
+ hasValidPropertiesMap && Object.keys(schema.properties).length > 0;
28
+
29
+ return (
30
+ hasChoiceAggregation &&
31
+ (!hasPropertiesKeyword || (hasValidPropertiesMap && !hasOwnProperties))
32
+ );
33
+ }
34
+
13
35
  export function resolveTrackingTargets(
14
36
  schema,
15
37
  { schemaFile = 'schema', isQuiet = false } = {},
@@ -17,9 +39,10 @@ export function resolveTrackingTargets(
17
39
  const configuredTargets = schema?.['x-tracking-targets'];
18
40
 
19
41
  if (configuredTargets == null) {
20
- const warning = isQuiet
21
- ? null
22
- : `Schema ${schemaFile} is missing x-tracking-targets. Falling back to "${DEFAULT_TRACKING_TARGET}".`;
42
+ const warning =
43
+ isQuiet || isReferenceAggregatorSchema(schema)
44
+ ? null
45
+ : `Schema ${schemaFile} is missing x-tracking-targets. Falling back to "${DEFAULT_TRACKING_TARGET}".`;
23
46
  return {
24
47
  targets: [DEFAULT_TRACKING_TARGET],
25
48
  warning,
@@ -9,14 +9,29 @@ import { promises as fs } from 'fs';
9
9
  import { URL } from 'url';
10
10
  import { resolveConstraintSchemaPath } from './constraintSchemaPaths.js';
11
11
 
12
+ const schemaFileCache = new Map();
13
+
14
+ export function clearSchemaFileCache() {
15
+ schemaFileCache.clear();
16
+ }
17
+
18
+ function readSchemaFile(filePath) {
19
+ if (!schemaFileCache.has(filePath)) {
20
+ schemaFileCache.set(
21
+ filePath,
22
+ fs.readFile(filePath, 'utf-8').then(JSON.parse),
23
+ );
24
+ }
25
+ return schemaFileCache.get(filePath);
26
+ }
27
+
12
28
  function createAjvInstance(schemas, mainSchema, schemaPath) {
13
29
  const schemaVersion = mainSchema?.$schema;
14
30
 
15
31
  const loadSchema = async (uri) => {
16
32
  const constraintPath = resolveConstraintSchemaPath(uri);
17
33
  if (constraintPath) {
18
- const schemaContent = await fs.readFile(constraintPath, 'utf-8');
19
- return JSON.parse(schemaContent);
34
+ return readSchemaFile(constraintPath);
20
35
  }
21
36
 
22
37
  let localPath;
@@ -29,8 +44,7 @@ function createAjvInstance(schemas, mainSchema, schemaPath) {
29
44
  } else {
30
45
  localPath = path.resolve(schemaPath, uri);
31
46
  }
32
- const schemaContent = await fs.readFile(localPath, 'utf-8');
33
- return JSON.parse(schemaContent);
47
+ return readSchemaFile(localPath);
34
48
  };
35
49
 
36
50
  const options = {
package/index.js CHANGED
@@ -7,8 +7,7 @@ import path from 'path';
7
7
  import { execSync } from 'child_process';
8
8
  import { fileURLToPath } from 'url';
9
9
 
10
- const __filename = fileURLToPath(import.meta.url);
11
- const __dirname = path.dirname(__filename);
10
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
12
11
 
13
12
  export default async function (context, options) {
14
13
  const { siteDir } = context;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-generate-schema-docs",
3
- "version": "1.8.4",
3
+ "version": "1.8.5",
4
4
  "description": "Docusaurus plugin to generate documentation from JSON schemas.",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
@@ -137,10 +137,10 @@ async function getVariablesFromSchemas(
137
137
  for (const file of jsonFiles) {
138
138
  try {
139
139
  let schema = await RefParser.bundle(file);
140
- schema = mergeAllOf(schema);
141
140
  if (!shouldIncludeSchemaForGtm(schema)) {
142
141
  continue;
143
142
  }
143
+ schema = mergeAllOf(schema);
144
144
  const fileVariables = parseSchema(schema, { skipArraySubProperties });
145
145
  for (const variable of fileVariables) {
146
146
  if (!allVariables.has(variable.name)) {
@@ -217,19 +217,27 @@ function createGtmVariables(variablesToCreate) {
217
217
 
218
218
  function deleteGtmVariables(variablesToDelete) {
219
219
  logger.log(`Found ${variablesToDelete.length} variables to delete.`);
220
+ const deleted = [];
221
+ const failedDeletes = [];
220
222
  for (const v of variablesToDelete) {
221
223
  const command = `gtm variables delete --variable-id ${v.variableId} --force --quiet`;
222
224
  logger.log(`Executing: ${command}`);
223
- execSync(command, { stdio: 'inherit' });
225
+ try {
226
+ execSync(command, { stdio: 'inherit' });
227
+ deleted.push(v.parameter.find((p) => p.key === 'name').value);
228
+ } catch {
229
+ failedDeletes.push({
230
+ name: v.parameter.find((p) => p.key === 'name').value,
231
+ variableId: v.variableId,
232
+ });
233
+ }
224
234
  }
225
- return variablesToDelete.map(
226
- (v) => v.parameter.find((p) => p.key === 'name').value,
227
- );
235
+ return { deleted, failedDeletes };
228
236
  }
229
237
 
230
238
  async function syncGtmVariables(
231
239
  schemaVariables,
232
- { skipArraySubProperties = false },
240
+ { skipArraySubProperties = false } = {},
233
241
  ) {
234
242
  const gtmVariables = getGtmVariables();
235
243
 
@@ -247,12 +255,13 @@ async function syncGtmVariables(
247
255
  );
248
256
 
249
257
  const created = createGtmVariables(toCreate);
250
- const deleted = deleteGtmVariables(toDelete);
258
+ const { deleted, failedDeletes } = deleteGtmVariables(toDelete);
251
259
 
252
260
  logger.log('GTM variable synchronization complete.');
253
261
  return {
254
262
  created,
255
263
  deleted,
264
+ failedDeletes,
256
265
  inSync: inSync.map((v) => v.name),
257
266
  };
258
267
  }
@@ -360,6 +369,14 @@ async function main(argv, deps) {
360
369
  ),
361
370
  );
362
371
  } else {
372
+ if (summary.failedDeletes?.length > 0) {
373
+ log.log(
374
+ `Skipped deleting ${summary.failedDeletes.length} GTM variables (GTM rejected the delete, they may still be referenced):`,
375
+ );
376
+ for (const failed of summary.failedDeletes) {
377
+ log.log(`- ${failed.name} (ID: ${failed.variableId})`);
378
+ }
379
+ }
363
380
  log.log('Synchronization successful!');
364
381
  log.log(
365
382
  `All changes applied in GTM workspace: "${workspaceName}" (ID: ${workspaceId})`,
@@ -397,6 +414,7 @@ module.exports = {
397
414
  main,
398
415
  getVariablesToCreate,
399
416
  getVariablesToDelete,
417
+ getGtmVariables,
400
418
  createGtmVariables,
401
419
  deleteGtmVariables,
402
420
  parseSchema,