-
-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathGeneratorInstallation.tsx
98 lines (86 loc) · 3.28 KB
/
GeneratorInstallation.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
import React, { useState } from 'react';
import { ParagraphTypeStyle } from '@/types/typography/Paragraph';
import generatorflagList from '../config/generator-flags.json';
import generatorTemplates from '../config/generator-templates.json';
import CodeBlock from './editor/CodeBlock';
import Select from './form/Select';
import Paragraph from './typography/Paragraph';
interface GeneratorFlagData {
flag: string;
specPath: string;
}
interface GeneratorFlags {
[key: string]: GeneratorFlagData;
}
/**
* @description This component displays generator installation options.
*/
export default function GeneratorInstallation() {
const [template, setTemplate] = useState<string>('@asyncapi/[email protected]');
// By default we will have output folder flag so its set here.
const [params, setParams] = useState<string>('-o example --use-new-generator');
const [specPath, setSpecPath] = useState<string>('https://bit.ly/asyncapi');
const generatorflags = generatorflagList as GeneratorFlags;
/**
* @description Handles the change event when selecting a generator template.
* @param {string} templateName - The name of the selected template.
*/
function onChangeTemplate(templateName: string) {
setTemplate(templateName);
if (templateName && generatorflags[templateName]) {
const templateBasedJSON = generatorflags[templateName];
// options are generated from generator-templates.json
// and flags are fetched from generator-flags.json,
// so it is mandatory to have check in case if any misses the option in future
if (templateBasedJSON) {
setParams(templateBasedJSON.flag);
setSpecPath(templateBasedJSON.specPath);
}
}
}
/**
* @description Generates the npm install command.
* @returns {string} The npm install command.
*/
function getNpmCode(): string {
return `npm install -g @asyncapi/cli
asyncapi generate fromTemplate ${specPath} ${template} ${params}`;
}
/**
* Generates the Docker command.
* @returns {string} The Docker command.
*/
function getDockerCode(): string {
return `/*\nThe '--network host' flag is used here to allow access to external URLs for the AsyncAPI document.\nThis is required only when the AsyncAPI document is specified as a URL.\nIf you are pointing to a local resource instead, the '--network host' flag is not needed.\n*/\n\ndocker run --rm -it --network host --user=root -v \${PWD}/example:/app/example -v \${PWD}/output:/app/output asyncapi/cli generate fromTemplate ${specPath} ${template} ${params}`;
}
return (
<div className='relative mx-auto mt-8 max-w-full'>
<div className='mb-4'>
<Paragraph typeStyle={ParagraphTypeStyle.md} className='mr-4 inline'>
Select a Generator template:
</Paragraph>
<Select
options={generatorTemplates}
selected={template}
onChange={onChangeTemplate}
className='shadow-outline-blue'
/>
</div>
<CodeBlock
language='generator-cli'
textSizeClassName='text-sm'
className='shadow-lg'
codeBlocks={[
{
language: 'npm',
code: getNpmCode()
},
{
language: 'Docker',
code: getDockerCode()
}
]}
/>
</div>
);
}