Skip to content

Commit

Permalink
Merge branch 'master' into cli.js-deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
Florence-Njeri authored Oct 13, 2024
2 parents 6ab60db + 6f60d50 commit 9e7f010
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 153 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@
"test",
"doc"
]
},
{
"login": "DhairyaMajmudar",
"name": "Dhairya Majmudar",
"avatar_url": "https://avatars.githubusercontent.com/u/124715224?v=4",
"profile": "https://dhaiyra-majmudar.netlify.app/",
"contributions": [
"bug"
]
}
],
"contributorsPerLine": 3,
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extends:
- plugin:sonarjs/recommended

parserOptions:
ecmaVersion: 2018
ecmaVersion: 2020
sourceType: module
ecmaFeatures:
jsx: true
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/update-docs-in-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- 'master'
paths:
- 'apps/generator/docs/*.md'
- '.github/workflows/update-docs-in-website.yml'

jobs:
Make-PR:
Expand Down Expand Up @@ -39,7 +40,7 @@ jobs:
mkdir -p ./markdown/docs/tools/generator
rm ../generator/apps/generator/docs/README.md
rm -r ../generator/apps/generator/docs/jsdoc2md-handlebars
printf "%s\ntitle: Generator\nweight: 3\n%s" "---" "---"> ../generator/docs/_section.md
printf "%s\ntitle: Generator\nweight: 3\n%s" "---" "---"> ../generator/apps/generator/docs/_section.md
mv ../generator/apps/generator/docs/*.md ./markdown/docs/tools/generator
- name: Commit and push
working-directory: ./website
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**
187 changes: 187 additions & 0 deletions Development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Development guide

This guide will help you set up the `generator` locally, run tests, and use Docker for isolated testing.

## Getting started

1. Fork & Clone the repository:

First fork the repository from github and then clone it,

```bash
git clone https://github.com/{your_username}/generator.git
cd generator
```

After cloning the repository, you should setup the fork properly and configure the `remote` repository as described [here](https://github.com/asyncapi/community/blob/master/git-workflow.md)

2. Install dependencies:

```bash
npm install
```

## Running tests

### Local testing

To run all tests locally:

- Unit tests: `npm run generator:test:unit`
- Integration tests: `npm run generator:test:integration`
- CLI tests: `npm run generator:test:cli`

### Adding tests

1. Create new test files in the appropriate directory under `apps/generator/test/`:

2. Follow the existing test patterns.

3. Run your new tests using the commands mentioned above.

## Docker isolated testing

To run tests in an isolated Docker environment:

1. Ensure Docker is installed and running on your machine.

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
"
```

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

Note: This approach ensures a clean environment for each test run by removing any existing `node_modules`.

### Manually testing with test templates

To test template features manually we have `react-template` and `nunjucks-template` in `apps/generator/test/templates`, you can use this templates to manually test your changes like this:

1. Navigate to the generator directory:

```bash
cd apps/generator
```
2. Modify the react-template in `./test/test-templates/react-template` to test different features.

3. Run the generator with the react-template:

```bash
asyncapi generate fromTemplate ./test/docs/dummy.yml ./test/test-templates/react-template -o ./test/output --force-write
```

4. Check the output in the `./test/output` directory to verify the output that you desired.

## Release process

To release a major/minor/patch:

### Conventional Commits:

To maintain a clear git history of commits and easily identify what each commit changed and whether it triggered a release, we use conventional commits. The feat and fix prefixes are particularly important as they are needed to trigger changesets. Using these prefixes ensures that the changes are correctly categorized and the versioning system functions as expected.

For Example:
```
feat: add new feature
```

#### Manual

1. Create a new release markdown file in the `.changeset` directory. The filename should indicate what the change is about.

2. Add the following content to the file in this particular format:

```markdown
---
"@package-name-1": [type] (major/minor/patch)
"@package-name-2": [type]
---

[Provide a brief description of the changes. For example: Added a new Release GitHub Flow to the Turborepo. No new features or bugfixes were introduced.]
```

For Example:

```markdown
---
"@asyncapi/generator": minor
---

Adding new Release Github Flow to the Turborepo. No new features or bugfixes were introduced.

```

3. Include the file in your pull request.

#### Using CLI

1. Create a new release markdown file using changeset CLI. Below command will trigger an interactive prompt that you can use to specify release type and affected packages.
```cli
npx -p @changesets/[email protected] changeset
```

2. Include the file in your pull request.

> [!TIP]
> For more detailed instructions, you can refer to the official documentation for creating a changeset:
[Adding a changeset](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md)

### Release Flow:

1. **Add a Changeset**:
- When you make changes that need to be released, create a markdown file in the `.changeset` directory stating the package name and level of change (major/minor/patch).

2. **Open a Pull Request**:
- Push your changes and open a Pull Request (PR). After the PR is merged the changeset file helps communicate the type of changes (major, minor, patch).

3. **CI Processes Changeset**:
- After PR is merged, a dedicated GitHub Actions release workflow runs using changeset action,

- This action reads the markdown files in the `.changeset` folder and creates a PR with the updated version of the package and removes the markdown file. For example:

Before:
```json
"name": "@asyncapi/generator",
"version": "2.0.1",
```

After:
```json
"name": "@asyncapi/generator",
"version": "3.0.1",
```

- The new PR will also contain the description from the markdown files,

- AsyncAPI bot automatically merge such release PR.

4. **Release the Package**:

- After the PR is merged, the CI/CD pipeline triggers again. The `changesets/action` step identifies that the PR was created by itself. It then verifies if the current version of the package is greater than the previously released version. If a difference is detected, it executes the publish command to release the updated package.

## Additional commands

- Lint the code: `npm run lint`
- Generate documentation: `npm run docs`
- Build Docker image: `npm run docker:build`

## Troubleshooting

If you encounter any issues during development or testing, please check the following:

1. Ensure you're using the correct Node.js version (18.12.0 or higher) and npm version (8.19.0 or higher).
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.
92 changes: 3 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,97 +63,10 @@ This library consists of:
- Custom filters. Check out [API docs](apps/nunjucks-filters/docs/api.md) for complete list
- Lodash-powered filters. For the list of all available filters check [official docs](https://lodash.com/docs/)

## Release Process

To release a major/minor/patch:

### Conventional Commits:

To maintain a clear git history of commits and easily identify what each commit changed and whether it triggered a release, we use conventional commits. The feat and fix prefixes are particularly important as they are needed to trigger changesets. Using these prefixes ensures that the changes are correctly categorized and the versioning system functions as expected.

For Example:
```
feat: add new feature
```

#### Manual

1. Create a new release markdown file in the `.changeset` directory. The filename should indicate what the change is about.

2. Add the following content to the file in this particular format:

```markdown
---
"@package-name-1": [type] (major/minor/patch)
"@package-name-2": [type]
---

[Provide a brief description of the changes. For example: Added a new Release GitHub Flow to the Turborepo. No new features or bugfixes were introduced.]
```

For Example:

```markdown
---
"@asyncapi/generator": minor
---

Adding new Release Github Flow to the Turborepo. No new features or bugfixes were introduced.

```

3. Include the file in your pull request.

#### Using CLI

1. Create a new release markdown file using changeset CLI. Below command will trigger an interactive prompt that you can use to specify release type and affected packages.
```cli
npx -p @changesets/[email protected] changeset
```

2. Include the file in your pull request.

> [!TIP]
> For more detailed instructions, you can refer to the official documentation for creating a changeset:
[Adding a changeset](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md)



### Release Flow:

1. **Add a Changeset**:
- When you make changes that need to be released, create a markdown file in the `.changeset` directory stating the package name and level of change (major/minor/patch).

2. **Open a Pull Request**:
- Push your changes and open a Pull Request (PR). After the PR is merged the changeset file helps communicate the type of changes (major, minor, patch).

3. **CI Processes Changeset**:
- After PR is merged, a dedicated GitHub Actions release workflow runs using changeset action,

- This action reads the markdown files in the `.changeset` folder and creates a PR with the updated version of the package and removes the markdown file. For example:

Before:
```json
"name": "@asyncapi/generator",
"version": "2.0.1",
```

After:
```json
"name": "@asyncapi/generator",
"version": "3.0.1",
```

- The new PR will also contain the description from the markdown files,

- AsyncAPI bot automatically merge such release PR.

4. **Release the Package**:

- After the PR is merged, the CI/CD pipeline triggers again. The `changesets/action` step identifies that the PR was created by itself. It then verifies if the current version of the package is greater than the previously released version. If a difference is detected, it executes the publish command to release the updated package.

## Contributing

For developement setup you can follow the detailed guide in [Developement guide](Development.md)

Read [CONTRIBUTING](CONTRIBUTING.md) guide.

## Contributors ✨
Expand Down Expand Up @@ -192,6 +105,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tr>
<tr>
<td align="center" valign="top" width="33.33%"><a href="https://github.com/pierrick-boule"><img src="https://avatars.githubusercontent.com/u/3237116?v=4?s=100" width="100px;" alt="pierrick-boule"/><br /><sub><b>pierrick-boule</b></sub></a><br /><a href="https://github.com/asyncapi/generator/commits?author=pierrick-boule" title="Code">💻</a> <a href="https://github.com/asyncapi/generator/commits?author=pierrick-boule" title="Tests">⚠️</a> <a href="https://github.com/asyncapi/generator/commits?author=pierrick-boule" title="Documentation">📖</a></td>
<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>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion apps/generator/docs/generator-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ To see this in action, navigate to the **python-mqtt-client-template** directory

``` cmd
Generation in progress. Keep calm and wait a bit... done
Check out your shiny new generated files at output.
Check out your shiny new generated files at test/project.
```

Navigating to the **test/project** directory. You should see a **client.py** file; the only content is `Temperature Service`.
Expand Down
6 changes: 3 additions & 3 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ class Generator {

try {
installedPkg = getTemplateDetails(this.templateName, PACKAGE_JSON_FILENAME);
pkgPath = installedPkg && installedPkg.pkgPath;
packageVersion = installedPkg && installedPkg.version;
pkgPath = installedPkg?.pkgPath;
packageVersion = installedPkg?.version;
log.debug(logMessage.templateSource(pkgPath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));

Expand Down Expand Up @@ -759,7 +759,7 @@ class Generator {
// Check if the filename dictates it should be separated
let wasSeparated = false;
for (const prop in fileNamesForSeparation) {
if (Object.prototype.hasOwnProperty.call(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) {
if (Object.hasOwn(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) {
await this.generateSeparateFiles(asyncapiDocument, fileNamesForSeparation[prop], prop, stats.name, root);
const templateFilePath = path.relative(this.templateContentDir, path.resolve(root, stats.name));
fs.unlink(path.resolve(this.targetDir, templateFilePath), next);
Expand Down
4 changes: 2 additions & 2 deletions apps/generator/lib/logMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const TEMPLATE_INSTALL_FLAG_MSG = 'because you passed --install flag';

const TEMPLATE_INSTALL_DISK_MSG = 'because the template cannot be found on disk';

const NODE_MODULES_INSTALL ='Remember that your local template must have its own node_modules installed first, \"npm install\" is not triggered by the generator.';
const NODE_MODULES_INSTALL = 'Remember that your local template must have its own node_modules installed first, "npm install" is not triggered by the generator.';

const NPM_INSTALL_TRIGGER = 'Installation of template located on disk technically means symlink creation betweed node_modules of the generator and template sources. Your local template must have its own node_modules, "npm install" is not triggered.';

Expand All @@ -19,7 +19,7 @@ function templateNotFound(templateName) {
}

function packageNotAvailable(packageDetails) {
if (packageDetails && packageDetails.pkgPath) {
if (packageDetails?.pkgPath) {
return `Unable to resolve template location at ${packageDetails.pkgPath}. Package is not available locally.`;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/generator/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function convertOldOptionsToNew(oldOptions, generator) {
}

const resolvers = [];
if (generator && generator.mapBaseUrlToFolder && generator.mapBaseUrlToFolder.url) {
if (generator?.mapBaseUrlToFolder?.url) {
resolvers.push(...getMapBaseUrlToFolderResolvers(generator.mapBaseUrlToFolder));
}
if (oldOptions.resolve) {
Expand Down
Loading

0 comments on commit 9e7f010

Please sign in to comment.