cli
cli copied to clipboard
feat: implement generate command
Description This PR aims to implement generate command
Related issue(s)
Resolves #9
@magicmatatjahu should I create local types for https://github.com/asyncapi/generator or is there a plan for adding typescript support?
@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 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 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.
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 For transpiling TS on the fly.
https://github.com/asyncapi/generator/blob/master/docs/authoring.md#typescript-support
@magicmatatjahu I tried ignoring the import but I am getting the same errors.
The tests are running in my local setup 😞

@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.
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
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 generator has simple-git as dependency
@derberg We know, in the PR we used old version of generator, but after bumping the issue should be resolved :)
Yeah, I have bumped the generator version, pr is ready for review now.
I definitely want to do DX manual testing, gimme few days 🙏🏼
@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 :)
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 before I do manual tests, can you solve merge conflicts and code smells? please 🙏🏼
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.
Thanks, to @magicmatatjahu implementation, is ready, @derberg I think you can do manual tests now.
@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.
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?
😆 sorry, we are using the load function here. Then @derberg shall we just move the command under generate template?
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
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.
ok then, let us push modelina as most important PR to unblock generate template and generate relations PRs
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
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 you have some conflicts with package-lock.json. Lemme know when we can do the review
@derberg it is ready for review 👍🏼