import { XCircleIcon } from '@heroicons/react/solid'; import { getSchemaFromReference } from '@nrwl/nx-dev/data-access-packages'; import { JsonSchema1, NxSchema } from '@nrwl/nx-dev/models-package'; import { renderMarkdown } from '@nrwl/nx-dev/ui-markdoc'; import Link from 'next/link'; import { useRouter } from 'next/router'; import React, { ReactNode, useState } from 'react'; import { generateJsonExampleFor, isErrors } from './examples'; import { SchemaViewModel } from './get-schema-view-model'; import { SchemaEditor } from './schema-editor'; import { SchemaViewer } from './schema-viewer'; import { Heading2, Heading3 } from './ui/headings'; function pathCleaner(path: string): string { return path.split('?')[0]; } export function Content({ schemaViewModel, }: { schemaViewModel: SchemaViewModel; }) { if (!schemaViewModel.currentSchema) throw new Error( 'A valid schema has to be defined for the "currentSchema" property' ); const router = useRouter(); const [presets, setPresets] = useState([]); const filterWithPresets = ( data: Record, wantedProperties: string[] ): Record => { const result: Record = {}; if (!wantedProperties.length) return data; for (const p in data) { if (wantedProperties.includes(p)) { result[p] = data[p]; } } return result; }; const vm = { get fullExample(): Record { const examples = generateJsonExampleFor( schemaViewModel.currentSchema as NxSchema, schemaViewModel.lookup, 'both' ); return isErrors(examples) ? {} : examples.value; }, get pages(): { name: string; href: string; current: boolean }[] { return [ { name: schemaViewModel.packageName, href: schemaViewModel.packageUrl, current: false, }, { name: schemaViewModel.schemaMetadata.name, href: pathCleaner(router.asPath), current: !schemaViewModel.subReference, }, !!schemaViewModel.subReference ? { name: schemaViewModel.subReference.split('/')[2], href: pathCleaner(router.asPath), current: true, } : void 0, ].filter( (x): x is { name: string; href: string; current: boolean } => !!x ); }, get markdown(): ReactNode { return renderMarkdown({ content: getMarkdown({ type: schemaViewModel.type, packageName: schemaViewModel.packageName, schemaName: schemaViewModel.schemaMetadata.name, schemaAlias: schemaViewModel.schemaMetadata.aliases[0] ?? '', schema: schemaViewModel.currentSchema as NxSchema, }), filePath: '', data: {}, }); }, }; return ( <>
{/* We remove the top description on sub property lookup */} {!schemaViewModel.subReference && ( <>
{vm.markdown}
{/* SPACER */}
)} {/*TODO@ben: create new component*/} {schemaViewModel.type === 'executors' && !schemaViewModel.subReference && (

Try out this interactive editor of the configuration object. Values are validated as you type and hovering over labels will give you more information.

{!!schemaViewModel.currentSchema.presets.length && ( <>

These buttons show the config object for specific common tasks.

{schemaViewModel.currentSchema.presets.map((p) => ( ))} {!!presets.length && ( )}
)}
)}
{!schemaViewModel.subReference && ( )} {schemaViewModel.subReference && ( )}
); } const getMarkdown = (data: { packageName: string; schemaAlias: string; schemaName: string; schema: NxSchema; type: 'executors' | 'generators'; }): string => { const hasExamplesFile = !!data.schema['examplesFile']; const executorNotice: string = `Options can be configured in \`project.json\` when defining the executor, or when invoking it. Read more about how to configure targets and executors here: [https://nx.dev/configuration/projectjson#targets](https://nx.dev/configuration/projectjson#targets).`; return [ `# ${data.packageName}:${data.schemaName}`, `\n\n`, data.schema.description, '\n\n', data.type === 'executors' ? executorNotice : '', `\n\n`, hasExamplesFile ? data.schema['examplesFile'] : '', data.type === 'generators' ? hasExamplesFile ? data.schema['examplesFile'] : getUsage(data.packageName, data.schemaName, data.schemaAlias) : '', !!data.schema['examples'] ? `### Examples \n ${data.schema['examples'] .map( (e: any) => `${e.description}: \n \`\`\`bash\n${e.command}\n\`\`\`` ) .join('\n')}` : '', `\n\n`, ].join(''); }; const getUsage = ( packageName: string, schemaName: string, schemaAlias: string ): string => ` ## Usage \`\`\`bash nx generate ${schemaName} ... \`\`\` ${!!schemaAlias ? `\`\`\`bash\nnx g ${schemaAlias} ... #same\n\`\`\`\n` : ''} By default, Nx will search for \`${schemaName}\` in the default collection provisioned in workspace.json. You can specify the collection explicitly as follows: \`\`\`bash nx g ${packageName}:${schemaName} ... \`\`\` Show what will be generated without writing to disk: \`\`\`bash nx g ${schemaName} ... --dry-run \`\`\` `; export default Content;