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

feat: automatically generate docs #318

Merged
merged 39 commits into from
Jan 12, 2023

Conversation

AceTheCreator
Copy link
Member

Description
This PR provides a way to automatically generate docs

It generates docs in a folder inside the project. Fully configurable from the glee.config.js file, like the path to the folder and the Generator template to use. We can default to docs and the https://github.com/asyncapi/markdown-template/.

Related issue(s)
Resolves #261

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@coveralls
Copy link

coveralls commented Jun 6, 2022

Pull Request Test Coverage Report for Build 3893317675

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 3 of 11 (27.27%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-17.7%) to 68.861%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/lib/docs.ts 3 11 27.27%
Totals Coverage Status
Change from base Build 3885930737: -17.7%
Covered Lines: 304
Relevant Lines: 395

💛 - Coveralls

Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

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

Looks good to me, we are only missing a test to ensure it works 🙂 There is a bunch of middleware test you can look for guidance, otherwise let me know if you get stuck or have any questions 🙂

@AceTheCreator
Copy link
Member Author

Looks good to me, we are only missing a test to ensure it works slightly_smiling_face There is a bunch of middleware test you can look for guidance, otherwise let me know if you get stuck or have any questions slightly_smiling_face

@jonaslagoni please kindly review this PR :)

}

expect(err).toBeUndefined();
fs.emptyDirSync(testDir);
Copy link
Member

Choose a reason for hiding this comment

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

Please at the end remove the testDir dir, not only remove content from it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@magicmatatjahu what do you mean by "not only remove content from it"?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ok.. my fault. You shouldn't. Don't focus on my previous comments.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay 😂. Is the PR okay?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if PR is okay, but that test is.

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

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

Left a couple of comments. I think this PR could be simpler. If I'm understanding correctly, you don't need the readGleeConfig util function. We already have the initializeConfig function that does the same and it's actually doing it with the config only.

src/index.ts Outdated
@@ -41,7 +44,7 @@ export default async function GleeAppInitializer () {
showAppDir: GLEE_PROJECT_DIR !== process.cwd(),
showFunctionsDir: GLEE_FUNCTIONS_DIR !== resolve(GLEE_DIR, 'functions'),
})

const gleeDocs = await readGleeConfig(GLEE_CONFIG_FILE_PATH)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need the readGleeConfig function. You already have the config loaded a few lines before.

)
return 'done'
} catch (error) {
logLineWithIcon(
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@fmvilas I just updated the PR. Kindly review it :)

src/index.ts Outdated
@@ -120,6 +121,7 @@ export default async function GleeAppInitializer () {
})

app.on('adapter:server:ready', (e: EnrichedEvent) => {
generateDocs(parsedAsyncAPI, config, null)
Copy link
Member

Choose a reason for hiding this comment

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

Why here? If we generate docs on "adapter:server:ready", it will only work for WebSocket and HTTP servers but not for clients (MQTT, Kafka, etc.). I think it should be generated as soon as possible and not necessarily when an event happens.

await generator.generateFromString(JSON.stringify(resolvedData))
logLineWithIcon(
':zap:',
'Successfully generated docs for your specification'
Copy link
Member

Choose a reason for hiding this comment

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

"specification" = https://github.com/asyncapi/spec/blob/master/spec/asyncapi.md
"document" = https://github.com/asyncapi/spec/blob/master/examples/simple.yml

People don't have specs but AsyncAPI documents. I'd keep it simpler and rephrase it as:

Suggested change
'Successfully generated docs for your specification'
'Successfully generated docs'

Comment on lines 26 to 28
logErrorLine(
`Failed to generate docs for your specs due to the following reason: ${error}`,
)
Copy link
Member

Choose a reason for hiding this comment

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

If you're going to log the reason, i.e., the full error, I recommend you use logError. Have a look at the logger.ts file and get familiar with the stuff there 😄

Suggested change
logErrorLine(
`Failed to generate docs for your specs due to the following reason: ${error}`,
)
logErrorLine('Failed to generate docs...')
logError(error)

@@ -49,4 +51,27 @@ describe('util', () => {
expect(functionEvent.glee).toBe(glee)
})
})
describe('readGleeConfig', () => {
Copy link
Member

Choose a reason for hiding this comment

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

🚜 ...
😄

Copy link
Member Author

Choose a reason for hiding this comment

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

@fmvilas I also didn't get the message you're trying to pass here 😂

Copy link
Member

Choose a reason for hiding this comment

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

hahaha I knew this can happen but I like to comment like this when something needs to be removed
😄 We're not using this function anymore, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you're right :)

it('should generate documentation', async () => {
const testDir = tmpdir() + `/${CONFIG_TEST_DATA.generator.folder}`;
fs.emptyDirSync(testDir);
let err: Error | undefined = undefined;
Copy link
Member

Choose a reason for hiding this comment

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

I think you can do something like this:

Suggested change
let err: Error | undefined = undefined;
let err?: Error

or like this, I can't remember:

Suggested change
let err: Error | undefined = undefined;
let err: Error?

Copy link
Member Author

Choose a reason for hiding this comment

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

@fmvilas I don't think I understand what you're trying to do here but it seems your code snippet is wrong :)

Mind explaining what you're trying to achieve with that snippet?

Copy link
Member

Choose a reason for hiding this comment

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

I was trying to do something like this: https://www.typescriptlang.org/docs/handbook/advanced-types.html#optional-parameters-and-properties. But it looks like it only works on parameters and object properties 🤦

Copy link
Member

Choose a reason for hiding this comment

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

In any case, you can simplify it like this:

Suggested change
let err: Error | undefined = undefined;
let err: Error | undefined

Souvikns
Souvikns previously approved these changes Dec 16, 2022
src/lib/docs.ts Outdated

export default async (spec, config, resDir) => {
const configData = config.docs
if (configData?.enabled) {
Copy link
Member

Choose a reason for hiding this comment

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

With this check, docs will only be generated if we add this configuration to the config file. It would be awesome if, by default, it generated docs and then people can disable it if they want to. So maybe it should be configData?.disabled instead? Folder can default to docs and template to @asyncapi/markdown-template.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why should it generate if it says configData?.disabled ? isn't that saying generate if disabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

shouldn't i be doing !configData || configData?.enabled?

src/lib/docs.ts Outdated

export default async (spec, config, resDir) => {
const configData = config.docs
if (!configData || configData?.enabled) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Something like this @fmvilas

Copy link
Member

Choose a reason for hiding this comment

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

What about the case when configData exists but enabled is not defined? It should still work but with this check it won't. The reason I said to use disabled instead is that you can easily check for it like this:

  if (configData?.disabled) return

This way, you early return only when you find that disabled is true.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, you can check for this:

  if (configData?.enabled === false) return

That should work as well, I think.

Copy link
Member

@fmvilas fmvilas Dec 23, 2022

Choose a reason for hiding this comment

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

But seems more logical to me that, since the default is that it generates docs, you only go there when you want to disable it or when you want to change the template/folder.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! Now I understand your point @fmvilas

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the PR @fmvilas. wyt?

Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

LGTM! left some suggestions.

Comment on lines 34 to 37
enabled: true // Enable/Disable documentation generation
folder: 'docs' // Folder where you want the output of your docs to reside.
template: '@asyncapi/markdown-template' // Type of template you want to use.
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
enabled: true // Enable/Disable documentation generation
folder: 'docs' // Folder where you want the output of your docs to reside.
template: '@asyncapi/markdown-template' // Type of template you want to use.
}
enabled: true, // Enable/Disable documentation generation
folder: 'docs', // Folder where you want the output of your docs to reside.
template: '@asyncapi/markdown-template' // Type of template you want to use.
}

package.json Outdated
@@ -36,6 +36,8 @@
"license": "Apache-2.0",
"dependencies": {
"@asyncapi/generator": "^1.9.3",
Copy link
Member

Choose a reason for hiding this comment

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

can we safely bump up the generator version here? 🤔 getting some incompatibility errors with this version.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review @KhudaDad414

KhudaDad414
KhudaDad414 previously approved these changes Dec 19, 2022
Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

🚢 🙇

@AceTheCreator
Copy link
Member Author

@fmvilas @Souvikns any other suggestions?

@AceTheCreator
Copy link
Member Author

Pinging you all for visibility @fmvilas @KhudaDad414 @Souvikns

Souvikns
Souvikns previously approved these changes Jan 10, 2023
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@AceTheCreator
Copy link
Member Author

Updated the ReadME @fmvilas. wyt?

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@fmvilas
Copy link
Member

fmvilas commented Jan 11, 2023

@jonaslagoni will have to approve too.

@AceTheCreator
Copy link
Member Author

/rtm

@asyncapi-bot asyncapi-bot merged commit e7ada8b into asyncapi:master Jan 12, 2023
@AceTheCreator AceTheCreator deleted the docs_generator branch January 12, 2023 09:40
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically generate docs
8 participants