Skip to content

chore: refactors readme.md file of js websocket client to reduce cognitive complexity #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ee71017
refactors readme.md file of js websocket client to reduce cognitive c…
KunalNasa Apr 7, 2025
d1bb181
Merge branch 'master' into ws-js-readme-refactor
asyncapi-bot Apr 7, 2025
34226b6
pull out a separate helper function for fetching example payload from…
KunalNasa Apr 8, 2025
5d57b42
test: add testing for hooks function (#1466)
Adi-204 Apr 8, 2025
60de527
test: add tests to registered hooks (#1433)
ssala034 Apr 9, 2025
a4ca5f7
test: improve unit test coverage for `parse.js` (#1439)
ssala034 Apr 9, 2025
ee759e4
chore: add acceptance testing with microcks mocks (#1467)
derberg Apr 14, 2025
b60a056
ci: ensure PR testing runs correctly based on conditions (#1486)
ssala034 Apr 14, 2025
cb43573
docs: add AdityaP700 as a contributor for infra (#1345)
allcontributors[bot] Apr 14, 2025
5f2a4ea
chore: fix contributors link in `README.md` (#1491)
Adi-204 Apr 14, 2025
f7cee77
docs: correct template generation process steps (#1489)
KunalNasa Apr 14, 2025
7575b39
chore: update generated docs (#1495)
asyncapi-bot Apr 14, 2025
cc644b7
chore: revert `ci: ensure PR testing runs correctly based on conditio…
derberg Apr 14, 2025
5f22b28
refactor: move `example.py` out of `test` folder and update README in…
AdeebaNizam404 Apr 15, 2025
6083c47
refactor: move fixtures to test folder (#1499)
thulieblack Apr 16, 2025
dcc496b
chore: setup Dockerfile & Docker Compose for project-wide testing (#1…
nightknighto Apr 16, 2025
2719d1b
chore: remove broken lint:tpl:validator script and its references (#1…
Aditya08Vashisht Apr 16, 2025
e6608ba
chore: update Postman client example in python folder. (#1498)
Light13008 Apr 17, 2025
e3b024a
fixed helpers to use parser api efficiently and componentize operati…
KunalNasa Apr 20, 2025
0aae29c
fixed helpers to use parser api efficiently and componentize operati…
KunalNasa Apr 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -224,6 +224,15 @@
"review",
"test"
]
},
{
"login": "AdityaP700",
"name": "AdityaPat_",
"avatar_url": "https://avatars.githubusercontent.com/u/126982848?v=4",
"profile": "https://linktr.ee/aditya_pat",
"contributions": [
"infra"
]
}
],
"contributorsPerLine": 3,
37 changes: 37 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Node modules
**/node_modules
npm-debug.log
yarn-debug.log
yarn-error.log

# Environment variables
.env
.env.*

# Build files
dist
build
*.tgz

# Git
.git
.gitignore

# IDE files
.idea
.vscode
*.sublime-project
*.sublime-workspace

# Logs
logs
*.log

# OS specific
.DS_Store
Thumbs.db

# Testing
**/coverage
.nyc_output
test-results
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apps/generator/output/*
**/node_modules/
.github/templates-list-validator/dist/*
**/temp/*
**/__transpiled/*
packages/components/lib/*
packages/components/lib/*
6 changes: 2 additions & 4 deletions .github/workflows/pr-testing-with-test-project.yml
Original file line number Diff line number Diff line change
@@ -8,8 +8,6 @@ on:
jobs:
changes:
runs-on: ubuntu-latest
outputs:
should_test: ${{ steps.filter.outputs.modified_files !='[]' }}
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3 https://github.com/actions/checkout/releases/tag/v3
- uses: rohansingh/paths-filter@084ca29929a4e7e708a4aa8b790347facfd830c9 # fork with predicate-quantifier fix
@@ -29,7 +27,7 @@ jobs:

test:
needs: changes
if: ${{ needs.changes.outputs.should_test == 'true' && github.event.pull_request.draft == false }}
if: ${{ github.event.pull_request.draft == false }}
name: Test generator as dependency with Node ${{ matrix.node }}
runs-on: ubuntu-latest
strategy:
@@ -41,7 +39,7 @@ jobs:
- name: Determine if tests should run
id: should_run
if: >
!github.event.pull_request.draft && !(
!(
(github.actor == 'asyncapi-bot' && (
startsWith(github.event.pull_request.title, 'ci: update of files from global .github repo') ||
startsWith(github.event.pull_request.title, 'chore(release):')
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -16,4 +16,6 @@ coverage

/.idea
temp
__pycache__
__pycache__

microcks-data
19 changes: 6 additions & 13 deletions Development.md
Original file line number Diff line number Diff line change
@@ -47,21 +47,14 @@ To run tests in an isolated Docker environment:
2. Run the following command from the project root:

```bash
docker run --rm -v ${PWD}:/app -w /app node:18 sh -c "
cp -r /app /tmp/app &&
cd /tmp/app &&
npm install &&
npm test
"
docker compose up
```

This command above does the following:
- Mounts the current directory to `/app` in the container
- Copies the project to a temporary directory
- Installs dependencies
- Runs all tests
You can also opt in to run the lint checks after the tests, by setting an environment variable `LINT` with any value before the command:
- Windows: `set LINT=true && docker compose up`
- Linux/macOS: `LINT=true docker compose up`

Note: This approach ensures a clean environment for each test run by removing any existing `node_modules`.
> This approach ensures a clean environment for each test run by cleanly installing dependencies and running tests in a Docker container.

### Manually testing with test templates

@@ -188,4 +181,4 @@ If you encounter any issues during development or testing, please check the foll
2. Clear the `node_modules` directory and reinstall dependencies if you encounter unexpected behavior.
3. For Docker-related issues, make sure Docker is running and you have sufficient permissions.

If problems persist, please open an issue on the GitHub repository.
If problems persist, please open an issue on the GitHub repository.
44 changes: 44 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM node:18-alpine AS base

WORKDIR /app

# ----------------------------------------
# Stage 1: Prepare package.json files
FROM base AS installer

# COPY the whole project to the container
# The following line raises a security hotspot in SonarQube, but it is necessary to copy the whole project to the container
# We can ignore this and deem it as false positive, because this is mainly for local development and testing
# We have manually marked this as safe in SonarQube UI
COPY . .

# Run turbo prune to prune the project down to just package.json files of the project
# This creates a new directory called /out with the following structure:
# /out
# ├── json -> package.json files of the project
# ├── full -> full source code of the project
# └── package-lock.json -> package-lock.json of the project
# We have to specify the package names. Some packages are included as dependencies in others, they will be automatically included
RUN npx turbo@1.13.3 prune @asyncapi/generator @asyncapi/template-js-websocket-client @asyncapi/generator-components --docker --out-dir /out

# ----------------------------------------
# Stage 2: Install dependencies
FROM base AS final

# Copy package.json files extracted by turbo prune
COPY --from=installer /out/json/ .
COPY --from=installer /out/package-lock.json ./package-lock.json

# Install dependencies only with package.json files to make use of cache
RUN npm ci --ignore-scripts

# Copy the rest of the source code
COPY --from=installer /out/full/ .

# Change ownership of the /app directory to the node user
RUN chown -R node:node /app

# Run the application as a non-root user
USER node

CMD ["npm", "test"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -119,6 +119,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="33.33%"><a href="https://dhaiyra-majmudar.netlify.app/"><img src="https://avatars.githubusercontent.com/u/124715224?v=4?s=100" width="100px;" alt="Dhairya Majmudar"/><br /><sub><b>Dhairya Majmudar</b></sub></a><br /><a href="https://github.com/asyncapi/generator/issues?q=author%3ADhairyaMajmudar" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="33.33%"><a href="https://github.com/Gmin2"><img src="https://avatars.githubusercontent.com/u/127925465?v=4?s=100" width="100px;" alt="Mintu Gogoi"/><br /><sub><b>Mintu Gogoi</b></sub></a><br /><a href="https://github.com/asyncapi/generator/issues?q=author%3AGmin2" title="Bug reports">🐛</a> <a href="https://github.com/asyncapi/generator/commits?author=Gmin2" title="Code">💻</a> <a href="#ideas-Gmin2" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/asyncapi/generator/commits?author=Gmin2" title="Documentation">📖</a> <a href="https://github.com/asyncapi/generator/pulls?q=is%3Apr+reviewed-by%3AGmin2" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/asyncapi/generator/commits?author=Gmin2" title="Tests">⚠️</a></td>
</tr>
<tr>
<td align="center" valign="top" width="33.33%"><a href="https://linktr.ee/aditya_pat"><img src="https://avatars.githubusercontent.com/u/126982848?v=4?s=100" width="100px;" alt="AdityaPat_"/><br /><sub><b>AdityaPat_</b></sub></a><br /><a href="#infra-AdityaP700" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
</tr>
</tbody>
</table>

4 changes: 2 additions & 2 deletions apps/generator/docs/template.md
Original file line number Diff line number Diff line change
@@ -23,9 +23,9 @@ You can store template projects on a local drive or as a `git` repository during
## Template generation process

1. Template is provided as input to the **Generator**.
2. **asyncapi** is the original AsyncAPI document injected into your template file by default.
2. **asyncapi** (which is an instance of AsyncAPIDocument) is injected into your template file by default.
3. **params** are the parameters you pass to the AsyncAPI CLI. Later, you can also pass these **params** further to other components.
4. The generator passes both the original **asyncapi**, the original AsyncAPI document, and the **params** to the **Template Context**.
4. The generator passes both the **asyncapi**, the **originalAsyncAPI**, and the **params** to the **Template Context**.
5. Concurrently, the generator passes **Template files** to the **Render engine** as well. AsyncAPI uses two render engines — _react_ and _nunjucks_.
6. Once the Render Engine receives both the Template Files and the Template Context, it injects all the dynamic values into your react or nunjucks engine, based on the Template Files using the Template Context.
7. The render engine generates whatever output you may have specified in your template. (i.e. code, documentation, diagrams, pdfs, applications, etc.)
9 changes: 8 additions & 1 deletion apps/generator/lib/hooksRegistry.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ const { exists, registerTypeScript } = require('./utils');
* @param {String} templateDir Directory where template is located.
* @param {String} hooksDir Directory where local hooks are located.
*/
module.exports.registerHooks = async (hooks, templateConfig, templateDir, hooksDir) => {
async function registerHooks (hooks, templateConfig, templateDir, hooksDir) {
await registerLocalHooks(hooks, templateDir, hooksDir);

if (templateConfig && Object.keys(templateConfig).length > 0) await registerConfigHooks(hooks, templateDir, templateConfig);
@@ -122,3 +122,10 @@ function addHook(hooks, mod, config) {
});
return hooks;
}

module.exports = {
registerHooks,
registerLocalHooks,
registerConfigHooks,
addHook
};
10 changes: 10 additions & 0 deletions apps/generator/lib/parser.js
Original file line number Diff line number Diff line change
@@ -50,6 +50,14 @@ parser.getProperApiDocument = (asyncapiDocument, templateConfig = {}) => {
return ConvertDocumentParserAPIVersion(asyncapiDocument, apiVersion);
};

/**
* Converts old parser options to the new format.
*
* @private - This function should not be used outside this module.
* @param {object} oldOptions - The old options to convert.
* @param {object} generator - The generator instance.
* @returns {object} The converted options.
*/
// The new options for the v2 Parser are different from those for the v1 version, but in order not to release Generator v2, we are converting the old options of Parser to the new ones.
function convertOldOptionsToNew(oldOptions, generator) {
if (!oldOptions) return;
@@ -143,3 +151,5 @@ function canReadFn(uri, canRead) {
}
return false;
}

module.exports.convertOldOptionsToNew = convertOldOptionsToNew;
3 changes: 1 addition & 2 deletions apps/generator/package.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@
"docs": "jsdoc2md --partial docs/jsdoc2md-handlebars/custom-sig-name.hbs docs/jsdoc2md-handlebars/main.hbs docs/jsdoc2md-handlebars/docs.hbs docs/jsdoc2md-handlebars/header.hbs docs/jsdoc2md-handlebars/defaultvalue.hbs docs/jsdoc2md-handlebars/link.hbs docs/jsdoc2md-handlebars/params-table.hbs --files lib/generator.js > docs/api.md",
"docker:build": "docker build -t asyncapi/generator:latest .",
"lint": "eslint --max-warnings 0 --config ../../.eslintrc --ignore-path ../../.eslintignore .",
"lint:tpl:validator": "eslint --fix --config ../../.eslintrc ../../.github/templates-list-validator",
"generate:readme:toc": "markdown-toc -i README.md",
"generate:assets": "npm run docs && npm run generate:readme:toc",
"bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION"
@@ -48,10 +47,10 @@
"license": "Apache-2.0",
"homepage": "https://github.com/asyncapi/generator",
"dependencies": {
"@asyncapi/generator-hooks": "*",
"@asyncapi/generator-react-sdk": "^1.1.2",
"@asyncapi/multi-parser": "^2.1.1",
"@asyncapi/nunjucks-filters": "*",
"@asyncapi/generator-hooks": "*",
"@asyncapi/parser": "^3.0.14",
"@npmcli/arborist": "5.6.3",
"@npmcli/config": "^8.0.2",
Loading