Skip to content
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

docs: add new Usage document #842

Merged
merged 33 commits into from
Nov 14, 2022
Merged
Changes from 8 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
542889e
add asyncapiDocument and asyncapiString documentation
Florence-Njeri Jul 20, 2022
be026ef
copy usage from the Readme
Florence-Njeri Sep 26, 2022
b30aa8e
add ids
Florence-Njeri Sep 26, 2022
cbfc1a0
remove the spec file from this PR
Florence-Njeri Sep 26, 2022
e273f74
add this document is under construction note
Florence-Njeri Sep 26, 2022
9066968
refactor and improve the usage docs
Florence-Njeri Sep 26, 2022
7a8c3df
fix page weight and fix grammar
Florence-Njeri Sep 26, 2022
a3f86e2
refactor generator library
Florence-Njeri Sep 26, 2022
7630ecf
remove usage from readme and link the usage docs in the ReadME
Florence-Njeri Oct 2, 2022
6381ea5
Update docs/usage.md
Florence-Njeri Oct 2, 2022
6d70e3b
Update docs/usage.md
Florence-Njeri Oct 2, 2022
8699e4c
Update docs/usage.md
Florence-Njeri Oct 2, 2022
a539675
Update docs/usage.md
Florence-Njeri Oct 2, 2022
5919ced
remove ag command that's been replaced by asyncapi generate fromTemplate
Florence-Njeri Oct 2, 2022
aa3ff85
pulled from remote origin
Florence-Njeri Oct 2, 2022
b188d66
change AsyncAPI file to AsyncAPI document
Florence-Njeri Oct 4, 2022
508fceb
Update README.md
Florence-Njeri Oct 18, 2022
4dd1eac
Update README.md
Florence-Njeri Oct 18, 2022
707dd30
Update docs/usage.md
Florence-Njeri Oct 18, 2022
176643d
Update docs/usage.md
Florence-Njeri Oct 18, 2022
ec56eb2
convert all references of the generator to lowercase
Florence-Njeri Oct 19, 2022
aebd16d
remove manual addition of usage to the TOC
Florence-Njeri Oct 19, 2022
d24c1df
update internal link
Florence-Njeri Oct 19, 2022
ef448df
lowercase generator
Florence-Njeri Oct 19, 2022
c69a008
remove linking to templates.md in code section
Florence-Njeri Oct 19, 2022
2d36d24
remove usage section from readme.md
Florence-Njeri Oct 19, 2022
6110bb9
grammarly
Florence-Njeri Oct 19, 2022
1289415
update TOC
Florence-Njeri Oct 21, 2022
d378734
Merge branch 'master' into usage
derberg Nov 8, 2022
f781c1b
Merge branch 'master' into usage
Florence-Njeri Nov 10, 2022
ee92c27
Merge branch 'master' into usage
Florence-Njeri Nov 14, 2022
c962245
Merge branch 'master' into usage
derberg Nov 14, 2022
540dfbd
Merge branch 'master' into usage
derberg Nov 14, 2022
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
151 changes: 151 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
---
Copy link
Member

@quetzalliwrites quetzalliwrites Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Florence-Njeri, consistency is key 🔑 😄. If you don't normally capitalize generator so far in these docs (as I also noted in PR #818), let's keep that consistent.

📢 ACTION ITEM: Please go through this entire PR and correct all your capitalized mentions of generator to lowercase so that the Docs are consistent.

title: "Usage"
weight: 30
---

There are two ways you can use the Generator. Find your preferred method below:
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved
- [Generator CLI](#generator-in-cli)
- [Generator library](#generator-library)

## Generator CLI
```bash
Usage: ag <asyncapi> <template> [<options>]

- <asyncapi>: Local path or URL pointing to AsyncAPI file for example https://bit.ly/asyncapi
- <template>: Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template

- <options>:
-V, --version output the Generator version
-d, --disable-hook [hooks...] disable a specific hook type or hooks from a given hook type
--debug enable more specific errors in the console
-i, --install install the template and its dependencies (defaults to false)
-n, --no-overwrite <glob> glob or path of the file(s) to skip when regenerating
-o, --output <outputDir> directory to put the generated files (defaults to current directory)
-p, --param <name=value> additional parameters to pass to templates
--force-write force writing of the generated files to a given directory even if it is a Git repository with unstaged files or not empty dir (defaults to false)
--watch-template watches the template directory and the AsyncAPI document, and re-generate the files when changes occur. Ignores the output directory. This flag should be used only for template development.
--map-base-url <url:folder> maps all schema references from base URL to local folder
-h, --help display help for command
```

<details>
<summary>Click here to read more about supported values for the <code>&lt;template&gt;</code> parameter.</summary>
<br>
All [templates](template.md) are installable npm packages. Therefore, the value of <code>&lt;template&gt;</code> can be anything supported by <code>npm install</code>. Here's a summary of the possibilities:
<br><br>
<pre><code>
npm install [&lt;@scope&gt;/]&lt;name&gt;
npm install [&lt;@scope&gt;/]&lt;name&gt;@&lt;tag&gt;
npm install [&lt;@scope&gt;/]&lt;name&gt;@&lt;version&gt;
npm install [&lt;@scope&gt;/]&lt;name&gt;@&lt;version range&gt;
npm install &lt;git-host&gt;:&lt;git-user&gt;/&lt;repo-name&gt;
npm install &lt;git repo url&gt;
npm install &lt;tarball file&gt;
npm install &lt;tarball url&gt;
npm install &lt;folder&gt;</code></pre>
</details>
<br>

### Global templates installed with yarn or npm
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

You can preinstall templates globally before installing the Generator CLI. The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.

```bash
npm install -g @asyncapi/html-template@0.16.0
ag asyncapi.yaml @asyncapi/html-template
# The generator uses html-template version 0.16.0 and not the latest version.
```

### CLI usage examples

**The shortest possible syntax:**
```bash
ag asyncapi.yaml @asyncapi/html-template
```

**Generating from a URL:**
```bash
ag https://bit.ly/asyncapi @asyncapi/html-template
```

**Specify where to put the result:**
```bash
ag asyncapi.yaml @asyncapi/html-template -o ./docs
```

**Passing parameters to templates:**
```bash
ag asyncapi.yaml @asyncapi/html-template -o ./docs -p title='Hello from param'
```

In the template you can use it like this: ` {{ params.title }}`

**Disabling the hooks:**
```bash
ag asyncapi.yaml @asyncapi/html-template -o ./docs -d generate:before generate:after=foo,bar
```

The generator skips all hooks of the `generate:before` type and `foo`, `bar` hooks of the `generate:after` type.

**Installing the template from a folder:**
```bash
ag asyncapi.yaml ~/my-template
```

It creates a symbolic link to the target directory (`~/my-template` in this case).

**Installing the template from a git URL:**
```bash
ag asyncapi.yaml https://github.com/asyncapi/html-template.git
```

**Map schema references from baseUrl to local folder:**
```bash
ag test/docs/apiwithref.json @asyncapi/html-template -o ./build/ --force-write --map-base-url https://schema.example.com/crm/:./test/docs/
```

The parameter `--map-base-url` maps external schema references to local folders.

### CLI usage with Docker

We have a docker image that we regularly update and by using it you do not need to install Node.js or npm even though the generator is written with it since the Docker image has the Generator installed.
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

Install [Docker](https://docs.docker.com/get-docker/) first, then use docker to pull and run the image using the following command:

```bash
docker run --rm -it \
-v [ASYNCAPI SPEC FILE LOCATION]:/app/asyncapi.yml \
-v [GENERATED FILES LOCATION]:/app/output \
asyncapi/generator [COMMAND HERE]

# Example that you can run inside generator directory after cloning this repository. First, you specify the mount in the location of your AsyncAPI specification file and then you mount it in the directory where the generation result should be saved.
docker run --rm -it \
-v ${PWD}/test/docs/dummy.yml:/app/asyncapi.yml \
-v ${PWD}/output:/app/output \
asyncapi/generator -o /app/output /app/asyncapi.yml @asyncapi/html-template --force-write
```

### CLI usage with npx instead of npm
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

[npx](https://www.npmjs.com/package/npx) is very useful when you want to run Generator in a CI/CD environment. In such a scenario, you do not want to install the generator globally and most environments that provide Node.js and Npm, also provide npx out of the box. Use the following npx command on your terminal:
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

```bash
npx -p @asyncapi/generator ag ./asyncapi.yaml @asyncapi/html-template
```

## Generator library
Once you install the generator in your project, you can use the Generator to generate whatever you want. The following code snippet is an example of HTML generation using the official `@asyncapi/html-template` template and fetching the spec document from the server using https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml:
Florence-Njeri marked this conversation as resolved.
Show resolved Hide resolved

```js
const path = require('path');
const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));

try {
await generator.generateFromURL('https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml');
console.log('Done!');
} catch (e) {
console.error(e);
}
```

See the [API documentation](api.md) for more examples and full API reference information.