diff --git a/nx-dev/feature-package-schema-viewer/src/lib/parameter-view.tsx b/nx-dev/feature-package-schema-viewer/src/lib/parameter-view.tsx index 9827997dd2..495ab50f6a 100644 --- a/nx-dev/feature-package-schema-viewer/src/lib/parameter-view.tsx +++ b/nx-dev/feature-package-schema-viewer/src/lib/parameter-view.tsx @@ -18,7 +18,7 @@ export const ParameterView = (props: { lookup: Lookup; }) => (
-
+
{props.alias && ( @@ -46,8 +46,8 @@ export const ParameterView = (props: { )}
-
-
+
+
-
+
{ renderMarkdown(props.description, { filePath: '', @@ -84,7 +84,7 @@ export const ParameterView = (props: { function ParameterMetadata({ schema }: { schema: JsonSchema }) { const data = getParameterMetadata(schema); return !!data.length ? ( -
+
{data.map((i) => ( {i.name}: {i.value} @@ -103,7 +103,7 @@ function ParameterEnums({ }) { const potentialEnums = (getEnum(schema, lookup) as string[]) ?? []; return !!potentialEnums.length ? ( -
+
Accepted values:{' '} {potentialEnums.map((e, i) => ( diff --git a/nx-dev/feature-package-schema-viewer/src/lib/schema-viewer.tsx b/nx-dev/feature-package-schema-viewer/src/lib/schema-viewer.tsx index 380944abd0..1e551ab3bc 100644 --- a/nx-dev/feature-package-schema-viewer/src/lib/schema-viewer.tsx +++ b/nx-dev/feature-package-schema-viewer/src/lib/schema-viewer.tsx @@ -2,12 +2,103 @@ import { getDescriptionForSchema, getSchemaFromResult, Lookup, + LookupResult, } from '@nrwl/nx-dev/data-access-packages'; import { JsonSchema, JsonSchema1 } from '@nrwl/nx-dev/models-package'; import { ParameterView } from './parameter-view'; import { shouldShowInStage, Stage } from './stage'; import { Type } from './types/type'; +interface PropertySchema extends JsonSchema1 { + alias?: string; + 'x-deprecated'?: boolean; + 'x-priority'?: 'important' | 'internal'; +} + +interface PropertyModel { + alias: string; + initialSchema: PropertySchema; + isRequired: boolean; + lookupResult: LookupResult; + propertyName: string; + propertyReference: string; +} + +const isPropertyDeprecated = (schema: PropertySchema | JsonSchema): boolean => + typeof schema === 'boolean' ? false : !!schema['x-deprecated']; +const getPropertyAlias = (schema: PropertySchema | JsonSchema): string => { + if (typeof schema === 'boolean' || schema.alias === undefined) return ''; + return String(schema.alias); +}; + +function getViewModel( + schema: JsonSchema1, + lookup: Lookup, + reference: string +): PropertyModel[] { + const properties = schema.properties || {}; + return Object.keys(properties) + .sort((a, b) => a[0].localeCompare(b[0])) // Sort properties alphabetically + .map((propertyName) => { + const propertySchema = properties[propertyName] as PropertySchema; + const lookupResult = lookup.getSchema(propertySchema); + return { + propertyName, + alias: propertySchema['alias'] ?? '', + initialSchema: propertySchema, + isRequired: + typeof schema.required !== 'undefined' && + !!schema.required.find((n) => n === propertyName), + lookupResult, + propertyReference: + lookupResult?.baseReference || + `${reference}/properties/${propertyName}`, + }; + }); +} + +function extractPropertiesByImportance(properties: PropertyModel[]): { + required: PropertyModel[]; + important: PropertyModel[]; + internal: PropertyModel[]; + rest: PropertyModel[]; +} { + const result: { + required: PropertyModel[]; + important: PropertyModel[]; + internal: PropertyModel[]; + rest: PropertyModel[]; + } = { + required: [], + important: [], + internal: [], + rest: [], + }; + for (const property of properties) { + if (property.isRequired) { + result.required.push(property); + continue; + } + if ( + property.initialSchema['x-priority'] && + property.initialSchema['x-priority'] === 'important' + ) { + result.important.push(property); + continue; + } + if ( + property.initialSchema['x-priority'] && + property.initialSchema['x-priority'] === 'internal' + ) { + result.internal.push(property); + continue; + } + result.rest.push(property); + } + + return result; +} + export function SchemaViewer({ schema, reference, @@ -19,38 +110,17 @@ export function SchemaViewer({ lookup: Lookup; stage: Stage; }): JSX.Element { - const properties = schema.properties || {}; - const isDeprecated = (schema: JsonSchema): boolean => - typeof schema === 'boolean' ? false : !!schema['x-deprecated']; - const getAlias = (schema: JsonSchema): string => - typeof schema === 'boolean' ? '' : (schema['alias'] as string); + const properties = getViewModel(schema, lookup, reference).filter((p) => { + if (p.lookupResult === undefined) { + return true; + } + return shouldShowInStage(stage, p.lookupResult.schema); + }); - const renderedProps = Object.keys(properties) - .sort((a, b) => a[0].localeCompare(b[0])) // Sort properties alphabetically - .map((propertyName) => { - const propertySchema = properties[propertyName]; - const lookupResult = lookup.getSchema(propertySchema); - return { - propertyName, - alias: ((properties[propertyName] as any)['alias'] as string) ?? '', - initialSchema: propertySchema, - lookupResult, - propertyReference: - lookupResult?.baseReference || - `${reference}/properties/${propertyName}`, - }; - }) - .filter((p) => { - if (p.lookupResult === undefined) { - return true; - } - return shouldShowInStage(stage, p.lookupResult.schema); - }) - .map((p) => { - const isRequired = - typeof schema.required !== 'undefined' && - !!schema.required.find((n) => n === p.propertyName); + const categorizedProperties = extractPropertiesByImportance(properties); + function renderProps(properties: any[]): JSX.Element[] { + return properties.map((p) => { if (p.lookupResult) { return ( (); if (typeof schema.additionalProperties === 'boolean') { @@ -113,8 +191,8 @@ export function SchemaViewer({ name="Additional Properties" description={getDescriptionForSchema(additionalPropertiesResult)} required={false} - alias={getAlias(additionalPropertiesResult.schema)} - deprecated={isDeprecated(additionalPropertiesResult.schema)} + alias={getPropertyAlias(additionalPropertiesResult.schema)} + deprecated={isPropertyDeprecated(additionalPropertiesResult.schema)} schema={additionalPropertiesResult.schema} reference={resolvedReference} lookup={lookup} @@ -133,8 +211,8 @@ export function SchemaViewer({