Skip to content

Commit 7807817

Browse files
committed
finish migrating to react renderer
1 parent 7d696f6 commit 7807817

19 files changed

+301
-3502
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
output
44
test/temp
5+
__transpiled

helper/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const _ = require('lodash');
2+
const sanitizeFilename = require('sanitize-filename');
3+
4+
function camelCase(string) {
5+
return _.camelCase(string);
6+
}
7+
8+
function pascalCase(string) {
9+
string = _.camelCase(string);
10+
return string.charAt(0).toUpperCase() + string.slice(1);
11+
}
12+
13+
function kebabCase(string) {
14+
return _.kebabCase(string);
15+
}
16+
17+
function capitalize(string) {
18+
return _.capitalize(string);
19+
}
20+
21+
function oneLine(string) {
22+
return string.replace(/\n/g, ' ').trim();
23+
}
24+
25+
function convertToFilename(string, options = { replacement: '-', maxLength: 255 }) {
26+
let sanitizedString = sanitizeFilename(string);
27+
28+
sanitizedString = sanitizedString.replace(/[\s]+/g, options.replacement);
29+
30+
if (sanitizedString.length > options.maxLength) {
31+
sanitizedString = sanitizedString.slice(0, options.maxLength);
32+
}
33+
34+
return sanitizedString;
35+
}
36+
37+
module.exports = {
38+
camelCase,
39+
pascalCase,
40+
kebabCase,
41+
capitalize,
42+
oneLine,
43+
convertToFilename
44+
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"dependencies": {
3636
"@asyncapi/generator-filters": "^2.1.0",
37-
"@asyncapi/generator-hooks": "^0.1.0"
37+
"@asyncapi/generator-hooks": "^0.1.0",
38+
"@asyncapi/generator-react-sdk": "^1.1.1"
3839
},
3940
"devDependencies": {
4041
"@asyncapi/generator": "^1.9.17",
@@ -47,9 +48,11 @@
4748
"eslint-plugin-jest": "^25.3.4",
4849
"eslint-plugin-sonarjs": "^0.11.0",
4950
"jest": "^27.4.5",
51+
"lodash": "^4.17.21",
5052
"markdown-toc": "^1.2.0",
5153
"node-fetch": "^2.6.1",
5254
"rimraf": "^3.0.2",
55+
"sanitize-filename": "^1.6.3",
5356
"semantic-release": "^21.0.1"
5457
},
5558
"release": {
@@ -77,6 +80,7 @@
7780
"supportedProtocols": [
7881
"ws"
7982
],
83+
"renderer": "react",
8084
"parameters": {
8185
"server": {
8286
"description": "The server you want to use in the code.",

template/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

template/README.md.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { File } = require('@asyncapi/generator-react-sdk');
2+
3+
function ReadmeFile({ asyncapi }) {
4+
console.log("Hello", asyncapi.info().description());
5+
return (
6+
<File name={'README.md'}>
7+
{`# ${asyncapi.info().title()}
8+
9+
${asyncapi.info().description() || 'Safe'}
10+
`}
11+
</File>
12+
);
13+
}
14+
15+
module.exports = ReadmeFile;

template/config/common.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

template/config/config.yml.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { File } = require('@asyncapi/generator-react-sdk');
2+
const { port } = require('../../filters/all.js');
3+
4+
function commonConfig({ asyncapi, params }) {
5+
const serverUrl = asyncapi.server(params.server).url();
6+
7+
return (
8+
<File name={'common.yml'}>
9+
{`default:
10+
port: ${port(serverUrl)}
11+
development:
12+
test:
13+
staging:
14+
production:
15+
`}
16+
</File>
17+
);
18+
}
19+
20+
module.exports = commonConfig;

template/index.html renamed to template/index.html.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
<!DOCTYPE html>
1+
const { File } = require('@asyncapi/generator-react-sdk');
2+
3+
function indexHtmlRender({ asyncapi }) {
4+
const channels = asyncapi.channels();
5+
const channelNames = Object.keys(channels);
6+
7+
const subscribeChannels = channelNames.filter(channelName => {
8+
const channel = channels[channelName];
9+
return channel.subscribe && typeof channel.subscribe === 'function';
10+
});
11+
12+
return (
13+
<File name="index.html">
14+
{`<!DOCTYPE html>
215
<html lang="en">
316
<head>
4-
<title>{{ asyncapi.info().title() }}</title>
17+
<title>${asyncapi.info().title()}</title>
518
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
619
<meta content="utf-8" http-equiv="encoding">
720
</head>
@@ -13,26 +26,22 @@ <h1>Open your browser console to see the logs and details of API you can use to
1326
1427
function displayHelp() {
1528
console.log('Available channels:')
16-
{%- for channelName, channel in asyncapi.channels() %}
17-
{%- if channel.hasSubscribe() %}
18-
console.log('* {{ channelName }}');
19-
{%- endif -%}
20-
{% endfor %}
29+
${subscribeChannels.map(channelName => `console.log('* ${channelName}');`).join('\n')}
2130
console.log('Available commands:')
2231
console.log('- listen: Establish a connection with the server at a given path.')
2332
console.log(' Usage: listen(channelName)');
24-
console.log(` Example: listen('{{ asyncapi.channelNames()[0] }}')`);
33+
console.log(\` Example: listen('${channelNames[0]}')\`);
2534
console.log('- send: Send a message to the server at a currently connected path.')
2635
console.log(' Usage: send(message)');
27-
console.log(` Example: send({ greet: 'Hello from client' })`);
36+
console.log(\` Example: send({ greet: 'Hello from client' })\`);
2837
}
2938
3039
function listen(path) {
31-
const url = new URL(path, 'ws://{{ asyncapi.server(params.server).url() }}').toString()
40+
const url = new URL(path, 'ws://${asyncapi.server('localhost').url()}').toString()
3241
connection = new WebSocket(url)
3342
3443
connection.onerror = error => {
35-
console.log(`WebSocket error: ${error}`)
44+
console.log(\`WebSocket error: \${error}\`)
3645
}
3746
3847
connection.onopen = () => {
@@ -59,4 +68,9 @@ <h1>Open your browser console to see the logs and details of API you can use to
5968
displayHelp();
6069
</script>
6170
</body>
62-
</html>
71+
</html>`}
72+
</File>
73+
);
74+
}
75+
76+
module.exports = indexHtmlRender;

0 commit comments

Comments
 (0)