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: add generate fromTemplate command #221

Merged
merged 96 commits into from
Sep 27, 2022

Conversation

Souvikns
Copy link
Member

@Souvikns Souvikns commented Feb 9, 2022

Description
This PR aims to implement generate command that will replace Generator CLI

Related issue(s)

Resolves #9

@Souvikns
Copy link
Member Author

Souvikns commented Feb 9, 2022

@magicmatatjahu should I create local types for https://github.com/asyncapi/generator or is there a plan for adding typescript support?

@Souvikns Souvikns marked this pull request as ready for review February 17, 2022 12:45
@magicmatatjahu
Copy link
Member

magicmatatjahu commented Feb 17, 2022

@Souvikns

should I create local types for https://github.com/asyncapi/generator or is there a plan for adding typescript support?

We have plan to add types asyncapi/generator#468 but as I see you added types in this project. For me it's not a problem, we can always remove them and switch to the official typing.

default: false,
description: 'force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)'
}),
'watch-tempalte': flags.boolean({
Copy link
Member

Choose a reason for hiding this comment

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

@Souvikns
Copy link
Member Author

@magicmatatjahu I need your help with this

For some reason, due to generate command the CLI is throwing ts error all over the project.

Currently, I am getting these warnings when I run CLI.

Error Logs
PS D:\Programs\AsyncAPI\cli> ./bin/run 
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/config/context.ts:2:5 - error TS7022: '__importDefault' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
      ~~~~~~~~~~~~~~~
src/commands/config/context.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/start/studio.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~
src/commands/start/studio.ts:6:7 - error TS2451: Cannot redeclare block-scoped variable 'command_1'.

6 const command_1 = require("@oclif/command");
        ~~~~~~~~~

  src/commands/config/context.ts:6:7
    6 const command_1 = require("@oclif/command");
            ~~~~~~~~~
    'command_1' was also declared here.
src/commands/start/studio.ts:7:7 - error TS2451: Cannot redeclare block-scoped variable 'base_1'.

7 const base_1 = __importDefault(require("../../base"));
        ~~~~~~

  src/commands/config/context.ts:7:7
    7 const base_1 = __importDefault(require("../../base"));
            ~~~~~~
    'base_1' was also declared here.

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/config/context/add.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~
src/commands/config/context/add.ts:6:7 - error TS2451: Cannot redeclare block-scoped variable 'command_1'.

6 const command_1 = require("@oclif/command");
        ~~~~~~~~~

  src/commands/config/context.ts:6:7
    6 const command_1 = require("@oclif/command");
            ~~~~~~~~~
    'command_1' was also declared here.
src/commands/config/context/add.ts:7:7 - error TS2451: Cannot redeclare block-scoped variable 'base_1'.

7 const base_1 = __importDefault(require("../../../base"));
        ~~~~~~

  src/commands/config/context.ts:7:7
    7 const base_1 = __importDefault(require("../../base"));
            ~~~~~~
    'base_1' was also declared here.

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/config/context/current.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~
src/commands/config/context/current.ts:6:7 - error TS2451: Cannot redeclare block-scoped variable 'command_1'.

6 const command_1 = require("@oclif/command");
        ~~~~~~~~~

  src/commands/config/context.ts:6:7
    6 const command_1 = require("@oclif/command");
            ~~~~~~~~~
    'command_1' was also declared here.
src/commands/config/context/current.ts:7:7 - error TS2451: Cannot redeclare block-scoped variable 'base_1'.

7 const base_1 = __importDefault(require("../../../base"));
        ~~~~~~

  src/commands/config/context.ts:7:7
    7 const base_1 = __importDefault(require("../../base"));
            ~~~~~~
    'base_1' was also declared here.
src/commands/config/context/current.ts:8:7 - error TS2451: Cannot redeclare block-scoped variable 'Context_1'.

8 const Context_1 = require("../../../models/Context");
        ~~~~~~~~~

  src/commands/config/context/add.ts:8:7
    8 const Context_1 = require("../../../models/Context");
            ~~~~~~~~~
    'Context_1' was also declared here.

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/config/context/list.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~
src/commands/config/context/list.ts:6:7 - error TS2451: Cannot redeclare block-scoped variable 'command_1'.

6 const command_1 = require("@oclif/command");
        ~~~~~~~~~

  src/commands/config/context.ts:6:7
    6 const command_1 = require("@oclif/command");
            ~~~~~~~~~
    'command_1' was also declared here.
src/commands/config/context/list.ts:7:7 - error TS2451: Cannot redeclare block-scoped variable 'base_1'.

7 const base_1 = __importDefault(require("../../../base"));
        ~~~~~~

  src/commands/config/context.ts:7:7
    7 const base_1 = __importDefault(require("../../base"));
            ~~~~~~
    'base_1' was also declared here.
src/commands/config/context/list.ts:8:7 - error TS2451: Cannot redeclare block-scoped variable 'Context_1'.

8 const Context_1 = require("../../../models/Context");
        ~~~~~~~~~

  src/commands/config/context/add.ts:8:7
    8 const Context_1 = require("../../../models/Context");
            ~~~~~~~~~
    'Context_1' was also declared here.

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/config/context/remove.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~
src/commands/config/context/remove.ts:6:7 - error TS2451: Cannot redeclare block-scoped variable 'command_1'.

6 const command_1 = require("@oclif/command");
        ~~~~~~~~~

  src/commands/config/context.ts:6:7
    6 const command_1 = require("@oclif/command");
            ~~~~~~~~~
    'command_1' was also declared here.
src/commands/config/context/remove.ts:7:7 - error TS2451: Cannot redeclare block-scoped variable 'base_1'.

7 const base_1 = __importDefault(require("../../../base"));
        ~~~~~~

  src/commands/config/context.ts:7:7
    7 const base_1 = __importDefault(require("../../base"));
            ~~~~~~
    'base_1' was also declared here.
src/commands/config/context/remove.ts:8:7 - error TS2451: Cannot redeclare block-scoped variable 'Context_1'.

8 const Context_1 = require("../../../models/Context");
        ~~~~~~~~~

  src/commands/config/context/add.ts:8:7
    8 const Context_1 = require("../../../models/Context");
            ~~~~~~~~~
    'Context_1' was also declared here.

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
(node:6664) TSError Plugin: @asyncapi/cli: ⨯ Unable to compile TypeScript:
src/commands/config/context/use.ts:2:67 - error TS7006: Parameter 'mod' implicitly has an 'any' type.

2 var __importDefault = (this && this.__importDefault) || function (mod) {
                                                                    ~~~
src/commands/config/context/use.ts:6:7 - error TS2451: Cannot redeclare block-scoped variable 'command_1'.

6 const command_1 = require("@oclif/command");
        ~~~~~~~~~

  src/commands/config/context.ts:6:7
    6 const command_1 = require("@oclif/command");
            ~~~~~~~~~
    'command_1' was also declared here.
src/commands/config/context/use.ts:7:7 - error TS2451: Cannot redeclare block-scoped variable 'base_1'.

7 const base_1 = __importDefault(require("../../../base"));
        ~~~~~~

  src/commands/config/context.ts:7:7
    7 const base_1 = __importDefault(require("../../base"));
            ~~~~~~
    'base_1' was also declared here.
src/commands/config/context/use.ts:8:7 - error TS2451: Cannot redeclare block-scoped variable 'Context_1'.

8 const Context_1 = require("../../../models/Context");
        ~~~~~~~~~

  src/commands/config/context/add.ts:8:7
    8 const Context_1 = require("../../../models/Context");
            ~~~~~~~~~
    'Context_1' was also declared here.

module: @oclif/[email protected]
task: toCached
plugin: @asyncapi/cli
root: D:\Programs\AsyncAPI\cli
See more details with DEBUG=*
All in one CLI for all AsyncAPI tools

VERSION
  @asyncapi/cli/0.14.1 win32-x64 node-v16.13.0

USAGE
  $ asyncapi [COMMAND]

COMMANDS
  config    access configs
  diff      find diff between two asyncapi files
  generate  Generator is a tool that you can use to generate whatever you want basing on the AsyncAPI specification file as an input.
  new       creates a new asyncapi file
  start     starts a new local instance of Studio
  validate  validate asyncapi file

PS D:\Programs\AsyncAPI\cli> 

These warnings are coming from here

const generator = new Generator(template, flags.output || path.resolve(os.tmpdir(), 'asyncapi-generator'), {
  forceWrite: flags['force-write'],
  install: flags.install,
  debug: flags.debug,
  templateParams: params,
  noOverwriteGlobs: flags['no-overwrite'],
  mapBaseUrlToFolder: flags['map-base-url'],
  disableHooks,
  mapBaseURLToFolder
});
await generator.generateFomString(asyncapi.text());

To be precise it breaks in when I create a new Generator object. I am trying to follow this, but maybe I am missing something.

@magicmatatjahu
Copy link
Member

@Souvikns I will check that but as I remember we had similar issue in the server-api project and I changed the imports to fix them. It's a problem with TS/TS-node, not with generator package.

@Souvikns
Copy link
Member Author

Souvikns commented Mar 2, 2022

https://github.com/asyncapi/generator/blob/1359f15a99a72f58380249b489bded4c29d0e978/package.json#L76-L77

@magicmatatjahu why does generator has ts-node and typescript as dependencies also why does it need typescript when it is a js project?

Found TypeStrong/ts-node#883 is this relevant in our case.

@magicmatatjahu
Copy link
Member

@Souvikns For transpiling TS on the fly.

https://github.com/asyncapi/generator/blob/master/docs/authoring.md#typescript-support

@Souvikns Souvikns mentioned this pull request Mar 3, 2022
16 tasks
@Souvikns
Copy link
Member Author

Souvikns commented Mar 8, 2022

@magicmatatjahu I tried ignoring the import but I am getting the same errors.

@derberg
Copy link
Member

derberg commented Sep 1, 2022

I found something about map-base-url flag, for some reason generateFromString command does not support map-base-url and which is something we are using because we are using CLI's load function for loading the spec file, so currently map-base-url flag does not work at all. What should we do, not use our CLI's load function?

Ah yes, cause we are not using generateFromURL as there is no point in doing it as we have our own load.
I think we should be consistent and use our own load function.

There are 2 options:

  • you just take https://github.com/asyncapi/generator/blob/master/lib/generator.js#L326-L328 and move it to generateFromString. I don't see a problem with it. In the end, somebody consciously passes mapBaseUrlToFolder to the constructor, so they know what they're doing 🤷🏼 and now the current solution limits/forces them to use generateFromURL 🤔
  • you can just make CLI responsible for getMapBaseUrlToFolderResolver, not rely on generator 🤔

I am struggling to solve this, generator does not have any errors that I can extend, one thing we can do is match output string and create our own errors and use them, or should we wait for asyncapi/community#266
@derberg what do your think?

yeah it will take some time until it is actually used in the generator. one thing we can do is match output string and create our own errors and use them is a good option, we just need to have some meaningful fallback, in case error strings change 😄

@magicmatatjahu @boyney123 any thoughts on above challenges?

package.json Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@Souvikns
Copy link
Member Author

Souvikns commented Sep 9, 2022

you can just make CLI responsible for getMapBaseUrlToFolderResolver, not rely on generator 🤔

@derberg I went with this and also removed the stack from the output so now the error messages look something like this

Screenshot 2022-09-09 at 2 15 12 PM

The messages coming from the generator commands are self explanatory so I didn't changed anything, can you review and let me know if any thing needs to be changed.

Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

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

@Souvikns 3 small suggestions,

in the meantime I'll do some manual testing

package.json Show resolved Hide resolved
test/commands/generate/fromTemplate.test.ts Outdated Show resolved Hide resolved
test/commands/generate/fromTemplate.test.ts Outdated Show resolved Hide resolved
@derberg derberg changed the title feat: implement generate command feat: add generate fromTemplate command Sep 26, 2022
@sonarcloud
Copy link

sonarcloud bot commented Sep 26, 2022

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 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

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

@Souvikns amazing work done here 👏🏼 well well done 💪🏼

@magicmatatjahu as you were involved in this PR as well in regards to watcher, let us wait for your approval before merge too

Copy link
Member

@magicmatatjahu magicmatatjahu left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

@Souvikns
Copy link
Member Author

/rtm

@asyncapi-bot asyncapi-bot merged commit 3bafd43 into asyncapi:master Sep 27, 2022
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.24.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.

generator features
5 participants