nx/scripts/documentation/schema-flattener.ts
Leosvel Pérez Espinosa 678818a58b
feat(angular): upgrade Angular to v12.0.0-rc.3 (#5630)
* feat(angular): upgrade Angular to v12.0.0-rc.3

* feat(angular): target Nx v12.4.0-beta.0 for Angular v12.0.0-rc.0 upgrade

* fix(angular): use defaultConfiguration if no other configuration is passed

* cleanup(angular): sync migration folder name to target version

* fix(repo): creating custom schema flattener for docs

* chore(repo): amend yarn.lock

* feat(angular): update angular storybook to use webpack 5

* fix(angular): add legacy peer deps for angular+jest

* fix(angular): move migrations to 12.3.0-rc.0

Co-authored-by: Zack DeRose <zack.derose@gmail.com>
Co-authored-by: Jason Jean <jasonjean1993@gmail.com>
2021-05-12 12:20:12 -04:00

41 lines
1.2 KiB
TypeScript

import { deepCopy, json, schema } from '@angular-devkit/core';
import { visitJsonSchema } from '@angular-devkit/core/src/json/schema';
import * as Ajv from 'ajv';
export interface SchemaFlattener {
flatten: (schema) => json.JsonObject;
}
export function createSchemaFlattener(
formats: schema.SchemaFormat[] = []
): SchemaFlattener {
const ajv = new Ajv();
for (const format of formats) {
ajv.addFormat(format.name, format.formatter as any);
}
return {
flatten: (schema) => {
const validate = ajv.removeSchema(schema).compile(schema);
const self = this;
function visitor(current, pointer, parentSchema, index) {
if (
current &&
parentSchema &&
index &&
json.isJsonObject(current) &&
current.hasOwnProperty('$ref') &&
typeof current['$ref'] == 'string'
) {
const resolved = self._resolver(current['$ref'], validate);
if (resolved.schema) {
parentSchema[index] = resolved.schema;
}
}
}
const schemaCopy = deepCopy(validate.schema) as json.JsonObject;
visitJsonSchema(schemaCopy, visitor);
return schemaCopy;
},
};
}