/
content.tsx
317 lines (296 loc) · 11.8 KB
/
content.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { HandRaisedIcon } from '@heroicons/react/24/outline';
import { XCircleIcon } from '@heroicons/react/24/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<string[]>([]);
const filterWithPresets = (
data: Record<string, any>,
wantedProperties: string[]
): Record<string, any> => {
const result: Record<string, any> = {};
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<string, any> {
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 (
<div className="min-w-0 flex-auto pt-8 pb-24 lg:pb-16">
<div className="mb-8 flex w-full items-center space-x-2">
<div className="w-full flex-grow space-x-4">
<div
aria-hidden="true"
data-tooltip="Schema type"
className="relative inline-flex rounded-md border border-slate-200 bg-slate-50 px-4 py-2 text-xs font-medium uppercase dark:border-slate-700 dark:bg-slate-800/60"
>
{schemaViewModel.type}
</div>
{schemaViewModel.hidden && (
<div
aria-hidden="true"
data-tooltip="Nx internal use only"
className="relative inline-flex rounded-md border border-red-100 bg-red-50 px-4 py-2 text-xs font-medium uppercase text-red-600 dark:border-red-900 dark:bg-red-900/30 dark:text-red-400"
>
Internal
</div>
)}
</div>
<div className="relative z-0 inline-flex flex-shrink-0 rounded-md shadow-sm">
<Link href={schemaViewModel.packageUrl}>
<a
title="See package information"
className="focus:ring-blue-nx-base focus:border-blue-nx-base relative inline-flex items-center rounded-l-md border border-slate-200 bg-white px-4 py-2 text-xs font-medium text-slate-600 hover:bg-slate-50 focus:z-10 focus:outline-none focus:ring-1 dark:border-slate-700 dark:bg-slate-800/60 dark:text-slate-400 dark:hover:bg-slate-800"
>
{schemaViewModel.packageName}
</a>
</Link>
<a
href={schemaViewModel.schemaGithubUrl}
target="_blank"
rel="noreferrer"
title="See this schema on Github"
className="focus:ring-blue-nx-base focus:border-blue-nx-base relative -ml-px inline-flex items-center rounded-r-md border border-slate-200 bg-white px-4 py-2 text-xs font-medium text-slate-600 hover:bg-slate-50 focus:z-10 focus:outline-none focus:ring-1 dark:border-slate-700 dark:bg-slate-800/60 dark:text-slate-400 dark:hover:bg-slate-800"
>
<svg
className="mr-2 h-4 w-4"
viewBox="0 0 16 16"
fill="currentColor"
>
<path
fillRule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"
></path>
</svg>
See schema
</a>
</div>
</div>
{!schemaViewModel.subReference && schemaViewModel.hidden && (
<div className="my-6 block rounded-md bg-red-50 p-4 ring-1 ring-red-100 dark:bg-red-900/30 dark:ring-red-900">
<div className="flex">
<div className="flex-shrink-0">
<HandRaisedIcon
className="h-5 w-5 text-red-500"
aria-hidden="true"
/>
</div>
<div className="ml-3">
<div className="mt-0 block text-sm font-medium text-red-600 dark:text-red-400">
Schema for internal use only
</div>
<p className="prose-sm mt-2 block text-red-700 dark:text-red-600">
Please do not extend this schema as it is part of Nx internal
usage.
</p>
</div>
</div>
</div>
)}
{/* We remove the top description on sub property lookup */}
{!schemaViewModel.subReference && (
<>
<div className="prose prose-slate dark:prose-invert max-w-none">
{vm.markdown}
</div>
<div className="h-12">{/* SPACER */}</div>
</>
)}
{/*TODO@ben: create new component*/}
{schemaViewModel.type === 'executors' && !schemaViewModel.subReference && (
<div className="mt-8 hidden md:block">
<Heading2 title="Options playground" />
<p className="my-6">
Try out this interactive editor of the configuration object. Values
are validated as you type and hovering over labels will give you
more information.
</p>
{!!schemaViewModel.currentSchema.presets.length && (
<>
<Heading3 title="Examples" />
<p className="my-4">
These buttons show the config object for specific common tasks.
</p>
<div className="mb-4 flex flex-wrap gap-4">
{schemaViewModel.currentSchema.presets.map((p) => (
<button
key={'preset-' + p.name.toLowerCase()}
onClick={() => {
setPresets(p.keys);
}}
type="button"
className="relative inline-flex items-center rounded-md border border-slate-200 bg-white px-4 py-2 px-4 py-2 text-xs font-medium text-slate-600 hover:bg-slate-50 dark:border-slate-700 dark:bg-slate-800/60 dark:text-slate-400 dark:hover:bg-slate-800"
>
{p.name}
</button>
))}
{!!presets.length && (
<button
onClick={() => setPresets([])}
type="button"
className="relative inline-flex items-center rounded-md border border-slate-200 bg-white px-4 py-2 px-4 py-2 text-xs font-medium text-slate-600 hover:bg-slate-50 dark:border-slate-700 dark:bg-slate-800/60 dark:text-slate-400 dark:hover:bg-slate-800"
>
Reset <XCircleIcon className="ml-1.5 h-4 w-4" />
</button>
)}
</div>
</>
)}
<div className="rounded-md border border-slate-200 p-0.5 dark:border-slate-700">
<SchemaEditor
packageName={schemaViewModel.packageName}
schemaName={schemaViewModel.schemaMetadata.name}
type={schemaViewModel.type.replace('s', '') as any}
content={filterWithPresets(vm.fullExample, presets)}
schema={schemaViewModel.currentSchema}
/>
</div>
</div>
)}
<div className="mt-8">
<Heading2 title="Options" />
{!schemaViewModel.subReference && (
<SchemaViewer
schema={schemaViewModel.currentSchema}
lookup={schemaViewModel.lookup}
reference="#"
stage={'both'}
/>
)}
{schemaViewModel.subReference && (
<SchemaViewer
schema={
getSchemaFromReference(
schemaViewModel.subReference,
schemaViewModel.lookup
) as JsonSchema1
}
lookup={schemaViewModel.lookup}
reference={schemaViewModel.subReference}
stage={'both'}
/>
)}
</div>
</div>
);
}
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/reference/project-configuration#targets](https://nx.dev/reference/project-configuration#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'
? 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;