docs(nxdev): support x-priority schema options (#14405)

This commit is contained in:
Benjamin Cabanes 2023-01-17 12:54:48 -05:00 committed by GitHub
parent da8b855c6c
commit 07f07374fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 46 deletions

View File

@ -18,7 +18,7 @@ export const ParameterView = (props: {
lookup: Lookup; lookup: Lookup;
}) => ( }) => (
<div key={'property-' + props.name} className="mb-8"> <div key={'property-' + props.name} className="mb-8">
<div className="mb-2 flex items-center"> <div className="mb-1 flex items-center">
<Heading3 title={props.name} /> <Heading3 title={props.name} />
<div className="ml-4 flex-grow space-x-2"> <div className="ml-4 flex-grow space-x-2">
{props.alias && ( {props.alias && (
@ -46,8 +46,8 @@ export const ParameterView = (props: {
)} )}
</div> </div>
</div> </div>
<div className="mb-2 text-sm"> <div className="mb-1 text-sm">
<div className="mb-1 text-green-600"> <div className="mb-0.5 text-green-600">
<Type <Type
s={props.schema} s={props.schema}
reference={props.reference} reference={props.reference}
@ -60,7 +60,7 @@ export const ParameterView = (props: {
)} )}
</div> </div>
<div className="prose prose-slate dark:prose-invert max-w-none"> <div className="prose prose-slate dark:prose-invert -mt-4 max-w-none">
{ {
renderMarkdown(props.description, { renderMarkdown(props.description, {
filePath: '', filePath: '',
@ -84,7 +84,7 @@ export const ParameterView = (props: {
function ParameterMetadata({ schema }: { schema: JsonSchema }) { function ParameterMetadata({ schema }: { schema: JsonSchema }) {
const data = getParameterMetadata(schema); const data = getParameterMetadata(schema);
return !!data.length ? ( return !!data.length ? (
<div className="mb-1"> <div className="mb-0.5">
{data.map((i) => ( {data.map((i) => (
<span key={i.key}> <span key={i.key}>
{i.name}: <code>{i.value}</code> {i.name}: <code>{i.value}</code>
@ -103,7 +103,7 @@ function ParameterEnums({
}) { }) {
const potentialEnums = (getEnum(schema, lookup) as string[]) ?? []; const potentialEnums = (getEnum(schema, lookup) as string[]) ?? [];
return !!potentialEnums.length ? ( return !!potentialEnums.length ? (
<div className="mb-1"> <div className="mb-0.5">
Accepted values:{' '} Accepted values:{' '}
{potentialEnums.map((e, i) => ( {potentialEnums.map((e, i) => (
<span key={'enums-' + e}> <span key={'enums-' + e}>

View File

@ -2,12 +2,103 @@ import {
getDescriptionForSchema, getDescriptionForSchema,
getSchemaFromResult, getSchemaFromResult,
Lookup, Lookup,
LookupResult,
} from '@nrwl/nx-dev/data-access-packages'; } from '@nrwl/nx-dev/data-access-packages';
import { JsonSchema, JsonSchema1 } from '@nrwl/nx-dev/models-package'; import { JsonSchema, JsonSchema1 } from '@nrwl/nx-dev/models-package';
import { ParameterView } from './parameter-view'; import { ParameterView } from './parameter-view';
import { shouldShowInStage, Stage } from './stage'; import { shouldShowInStage, Stage } from './stage';
import { Type } from './types/type'; 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({ export function SchemaViewer({
schema, schema,
reference, reference,
@ -19,38 +110,17 @@ export function SchemaViewer({
lookup: Lookup; lookup: Lookup;
stage: Stage; stage: Stage;
}): JSX.Element { }): JSX.Element {
const properties = schema.properties || {}; const properties = getViewModel(schema, lookup, reference).filter((p) => {
const isDeprecated = (schema: JsonSchema): boolean => if (p.lookupResult === undefined) {
typeof schema === 'boolean' ? false : !!schema['x-deprecated']; return true;
const getAlias = (schema: JsonSchema): string => }
typeof schema === 'boolean' ? '' : (schema['alias'] as string); return shouldShowInStage(stage, p.lookupResult.schema);
});
const renderedProps = Object.keys(properties) const categorizedProperties = extractPropertiesByImportance(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);
function renderProps(properties: any[]): JSX.Element[] {
return properties.map((p) => {
if (p.lookupResult) { if (p.lookupResult) {
return ( return (
<ParameterView <ParameterView
@ -58,8 +128,8 @@ export function SchemaViewer({
alias={p.alias} alias={p.alias}
name={p.propertyName} name={p.propertyName}
description={getDescriptionForSchema(p.lookupResult.schema)} description={getDescriptionForSchema(p.lookupResult.schema)}
required={isRequired} required={p.isRequired}
deprecated={isDeprecated(p.lookupResult.schema)} deprecated={isPropertyDeprecated(p.lookupResult.schema)}
schema={p.lookupResult.schema} schema={p.lookupResult.schema}
reference={p.propertyReference} reference={p.propertyReference}
lookup={lookup} lookup={lookup}
@ -70,10 +140,10 @@ export function SchemaViewer({
<ParameterView <ParameterView
key={p.propertyName} key={p.propertyName}
alias={p.alias} alias={p.alias}
deprecated={isDeprecated(p.initialSchema)} deprecated={isPropertyDeprecated(p.initialSchema)}
name={p.propertyName} name={p.propertyName}
description={getDescriptionForSchema(p.initialSchema)} description={getDescriptionForSchema(p.initialSchema)}
required={isRequired} required={p.isRequired}
schema={p.initialSchema} schema={p.initialSchema}
reference={p.propertyReference} reference={p.propertyReference}
lookup={lookup} lookup={lookup}
@ -81,6 +151,14 @@ export function SchemaViewer({
); );
} }
}); });
}
const renderedProps = renderProps([
...categorizedProperties.required,
...categorizedProperties.important,
...categorizedProperties.rest,
...categorizedProperties.internal,
]);
const additionalProperties = new Array<JSX.Element>(); const additionalProperties = new Array<JSX.Element>();
if (typeof schema.additionalProperties === 'boolean') { if (typeof schema.additionalProperties === 'boolean') {
@ -113,8 +191,8 @@ export function SchemaViewer({
name="Additional Properties" name="Additional Properties"
description={getDescriptionForSchema(additionalPropertiesResult)} description={getDescriptionForSchema(additionalPropertiesResult)}
required={false} required={false}
alias={getAlias(additionalPropertiesResult.schema)} alias={getPropertyAlias(additionalPropertiesResult.schema)}
deprecated={isDeprecated(additionalPropertiesResult.schema)} deprecated={isPropertyDeprecated(additionalPropertiesResult.schema)}
schema={additionalPropertiesResult.schema} schema={additionalPropertiesResult.schema}
reference={resolvedReference} reference={resolvedReference}
lookup={lookup} lookup={lookup}
@ -133,8 +211,8 @@ export function SchemaViewer({
<ParameterView <ParameterView
key={`pattern-properties-${i}`} key={`pattern-properties-${i}`}
name={`/${pattern}/ (keys of pattern)`} name={`/${pattern}/ (keys of pattern)`}
alias={getAlias(currentSchema)} alias={getPropertyAlias(currentSchema)}
deprecated={isDeprecated(currentSchema)} deprecated={isPropertyDeprecated(currentSchema)}
description={getDescriptionForSchema(schema)} description={getDescriptionForSchema(schema)}
required={false} required={false}
schema={currentSchema} schema={currentSchema}

View File

@ -79,9 +79,7 @@ export interface SchemaMetadata {
} }
export interface NxSchema extends JsonSchema1 { export interface NxSchema extends JsonSchema1 {
title: string;
description: string; description: string;
presets: { name: string; keys: string[] }[];
examplesFile: string; examplesFile: string;
hidden: boolean; hidden: boolean;
} }