cli icon indicating copy to clipboard operation
cli copied to clipboard

feat: implement generate command

Open Souvikns opened this issue 3 years ago • 43 comments

Description This PR aims to implement generate command

Related issue(s)

Resolves #9

Souvikns avatar Feb 09 '22 06:02 Souvikns

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

Souvikns avatar Feb 09 '22 14:02 Souvikns

@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 https://github.com/asyncapi/generator/issues/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.

magicmatatjahu avatar Feb 17 '22 12:02 magicmatatjahu

@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.

Souvikns avatar Feb 22 '22 05:02 Souvikns

@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.

magicmatatjahu avatar Feb 28 '22 13:02 magicmatatjahu

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 https://github.com/TypeStrong/ts-node/issues/883 is this relevant in our case.

Souvikns avatar Mar 02 '22 02:03 Souvikns

@Souvikns For transpiling TS on the fly.

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

magicmatatjahu avatar Mar 02 '22 09:03 magicmatatjahu

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

Souvikns avatar Mar 08 '22 10:03 Souvikns

The tests are running in my local setup 😞 image

Souvikns avatar Mar 30 '22 06:03 Souvikns

@magicmatatjahu, @derberg generator command is ready for review. I have added a spinner as per https://github.com/asyncapi/cli/issues/9#issuecomment-1081950527, I want some opinion on that.

Souvikns avatar Apr 25 '22 04:04 Souvikns

I'm not gonna do code review as I see @magicmatatjahu already did it. Just let me know when is the right time to do user testing on local. Happy to help and check DX

derberg avatar May 10 '22 13:05 derberg

I am getting this message when running asyncapi --help

=============================================
simple-git has supported promises / async await since version 2.6.0.
 Importing from 'simple-git/promise' has been deprecated and will be
 removed by July 2022.

To upgrade, change all 'simple-git/promise' imports to just 'simple-git'
=============================================

CLI is not using simple-git, but maybe a tool we depend on is using it.

Souvikns avatar May 12 '22 10:05 Souvikns

@Souvikns generator has simple-git as dependency

derberg avatar May 17 '22 14:05 derberg

@derberg We know, in the PR we used old version of generator, but after bumping the issue should be resolved :)

magicmatatjahu avatar May 17 '22 15:05 magicmatatjahu

Yeah, I have bumped the generator version, pr is ready for review now.

Souvikns avatar May 18 '22 04:05 Souvikns

I definitely want to do DX manual testing, gimme few days 🙏🏼

derberg avatar May 19 '22 07:05 derberg

@derberg No problem, however remember that now we don't have implemented --watch-template functionality in this PR.

@Souvikns If you want, I can help you with it :)

magicmatatjahu avatar May 19 '22 07:05 magicmatatjahu

Oh sorry, I was kinda confused about how the CLI's watcher is working https://github.com/asyncapi/cli/blob/b0cd740a6b405c04c092345c1db02097ee53c47c/src/globals.ts#L28, I will give another try, give me some time on this.

@Souvikns If you want, I can help you with it :)

Thanks @magicmatatjahu I will ask you if get stuck.

Souvikns avatar May 19 '22 07:05 Souvikns

@Souvikns before I do manual tests, can you solve merge conflicts and code smells? please 🙏🏼

derberg avatar May 24 '22 08:05 derberg

The watcher is not working correctly.

Getting this error

➜ ./bin/run generate https://bit.ly/asyncapi @asyncapi/html-template --output ./docs -w           
Error: Cannot find module '/Users/postman/Documents/GitHub/cli/@asyncapi/html-template/package.json'
Require stack:
- /Users/postman/Documents/GitHub/cli/lib/commands/generate.js
- /Users/postman/Documents/GitHub/cli/node_modules/@oclif/core/lib/module-loader.js
- /Users/postman/Documents/GitHub/cli/node_modules/@oclif/core/lib/config/plugin.js
- /Users/postman/Documents/GitHub/cli/node_modules/@oclif/core/lib/config/config.js
- /Users/postman/Documents/GitHub/cli/node_modules/@oclif/core/lib/config/index.js
- /Users/postman/Documents/GitHub/cli/node_modules/@oclif/core/lib/command.js
- /Users/postman/Documents/GitHub/cli/node_modules/@oclif/core/lib/index.js
- /Users/postman/Documents/GitHub/cli/bin/run

@magicmatatjahu need your help here.

Souvikns avatar May 24 '22 08:05 Souvikns

Thanks, to @magicmatatjahu implementation, is ready, @derberg I think you can do manual tests now.

Souvikns avatar May 31 '22 07:05 Souvikns

@derberg

I think you folks saw https://github.com/asyncapi/cli/pull/173 and are aware of the direction that we were going there with generate command. With generate types. Shouldn't we merge the other PR first and add this PR with generate template ?

We can even add generate template in this PR and then handle mentioned PR. What do you think?

in code I see custom handling of file vs url, and in help message no mention of context which makes me feel we are not reusing global handlers for handling file/url/context, which is not good

Where do you see that? We use load function to load file/url/context.

magicmatatjahu avatar Jun 07 '22 08:06 magicmatatjahu

I didn't use the load function because the generator supports both file system path and URL. Even if we use the load function we sill need to pass the input string to the generator function and the watcher functions.

We can even add generate a template in this PR and then handle mentioned PR. What do you think?

Yeah, I think we can move this command to generate template, but are we generating a template or using the template to generate something, should we have generate template command?

Souvikns avatar Jun 07 '22 10:06 Souvikns

😆 sorry, we are using the load function here. Then @derberg shall we just move the command under generate template?

Souvikns avatar Jun 09 '22 04:06 Souvikns

We can even add generate template in this PR and then handle mentioned PR. What do you think? Then @derberg shall we just move the command under generate template?

to be fair with contributor, we should first merge modelina, and then this 😉 unless he is ok with your suggestion.

sorry, we are using the load function here

oh sorry, I noticed https://github.com/asyncapi/cli/pull/221/files#diff-8a64c17c2ac6b0f75d2c565e975631f3c831a8eb5b0aa22eaa4cfda960b08920R8-R10 but did not check if you are using it 😞 sorry, please remove this helper

also please update help to make it clear that asyncapi argument can also be context

derberg avatar Jun 09 '22 11:06 derberg

to be fair with contributor, we should first merge modelina, and then this 😉 unless he is ok with your suggestion.

Sure @derberg I have no problem waiting, let's merge this after we have merged modelina.

Souvikns avatar Jun 15 '22 13:06 Souvikns

ok then, let us push modelina as most important PR to unblock generate template and generate relations PRs

derberg avatar Jun 21 '22 13:06 derberg

can you please fix some DX with your PR in the new asyncapi generate command?

$ asyncapi generate
 ›   Warning: help is not a asyncapi command.
Did you mean new? [y/n]: n

☝🏼 should actually work the way that I out of the box get a display of help message with list of commands available in generate topic

$ asyncapi generate
Generates typed models

USAGE
  $ asyncapi generate COMMAND

COMMANDS
  generate models  Generates typed models

derberg avatar Jun 30 '22 09:06 derberg

Yeah, it does not support out of the box for topic, I forgot to add the workaround for the generate command.

cli on  generator-command is 📦 v0.21.0 via ⬢ v16.14.0 
➜ ./bin/run generate 
Generate typed models or templates

USAGE
  $ asyncapi generate

DESCRIPTION
  Generate typed models or templates

COMMANDS
  generate models    Generates typed models
  generate template  generate whatever you want using templates compatible with AsyncAPI Generator


cli on  generator-command is 📦 v0.21.0 via ⬢ v16.14.0 took 4s 
➜ 

Souvikns avatar Jun 30 '22 09:06 Souvikns

@Souvikns you have some conflicts with package-lock.json. Lemme know when we can do the review

derberg avatar Jul 05 '22 08:07 derberg

@derberg it is ready for review 👍🏼

Souvikns avatar Jul 05 '22 08:07 Souvikns