-
-
Notifications
You must be signed in to change notification settings - Fork 155
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: improve help message for new
command
#320
Conversation
There was a problem hiding this 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.
src/commands/new.ts
Outdated
@@ -15,14 +15,22 @@ export default class New extends Command { | |||
static flags = { | |||
help: Flags.help({ char: 'h' }), | |||
'file-name': Flags.string({ char: 'n', description: 'name of the file' }), | |||
example: Flags.string({ char: 'e', description: 'name of the example to use' }), | |||
example: Flags.string({ char: 'e', description: `name of the example to use. Available examples are: \n\t- default-example.yml \n\t- somethingElseWithSpecificProtocol.json (MQTT)` }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey, sorry, I was not super clear in related github issue.
The point here is that there are multiple different examples, and user of CLI has no idea about them. In my issue I just wanted to indicate how list of examples was suppose to look like. But in fact, list is much larger and should be built in a dynamic way in the help, not hardcoded.
when you run npm run build
you will notice that in /assets/examples
directory there is much more. And there is a file called examples.json
. We already read this file -> https:/asyncapi/cli/pull/320/files#diff-bddcbb9edc64eedc14d10908f9c6a72353ea7a0cea813b34edc6d163f2a33f92R76 so we can list examples in the interactive mode of asyncapi new
. Anyway, we should definitely use it to list all the examples in the help, so folks know what they can actually use.
I hope it makes sense 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, got it, thanks. Working on it.
src/commands/new.ts
Outdated
@@ -24,6 +46,10 @@ export default class New extends Command { | |||
static args = [] | |||
|
|||
async run() { | |||
const exampleDescription = await getExamplesFlagDescription(); | |||
this.flags.example = Flags.string({ char: 'e', description: exampleDescription}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so it's been a while now that I'm stuck on this.
The problem that I'm facing is that I fetched the example.json, mapped it, and made the description out of it. But, how do I add to my static flags
?
I've tried to add it inside the run() function, before parsing the New
class, but it's not working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think flag building doesn't like async/await and promises 😄
when I quickly play with your code:
- replace any async/await with normal functions and switch to
readFileSync
- fix building description like this:
let description = 'name of the example to use. Available examples are:'; for (const element of examples) { description += (`\n\t - ${element.value}`); }
full code:
function loadExampleFile() : Example[] {
const exampleFiles = readFileSync(resolve(__dirname, '../../assets/examples/examples.json'), { encoding: 'utf8' });
//console.log(JSON.parse(exampleFiles));
return JSON.parse(exampleFiles);
}
function getExamplesFlagDescription() : string {
const examples = loadExampleFile();
let description = 'name of the example to use. Available examples are:';
for (const element of examples) {
description += (`\n\t - ${element.value}`);
}
return description;
}
example: Flags.string({ char: 'e', description: getExamplesFlagDescription()}),
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks a lot
@LakshyaSatpal looking good man! Please just add a unit test that validates if list of examples is rendered? I'm just pretty sure that in few months someone will say "why do we use sync function here, lemme refactor" 😆 in the test, please do not "hardcode" list of examples as the tests will be affected by changes in examples list. So in test also read examples dynamically |
Hi @derberg ! |
@LakshyaSatpal no worries. Happy to help you if you need.
I recommend, especially if it is your first time, first create a test, where you basically hardcode the expected message in the text, so you check if the message printed in terminal match the message that you hardcode in test (with list of examples). Once ☝🏼 works, then work on making sure that the list of examples is not hardcoded, but you read in dynamically inside the test, from examples dir |
@derberg I wrote this simple test in the
This shows me an error of |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
test/commands/new.test.ts
Outdated
|
||
test | ||
.stdout() | ||
.command(['new', '-h']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.command(['new', '-h']) | |
.command(['new', '--help']) |
try doing this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried, getting the same error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, not working only in the test environment 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
found this though oclif/oclif#142 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason is that actually new --help
in test is not successful and throws:
TypeError: cmd._help is not a function
I get the same when I try bin/run new -h
with CLI. But bin/run new --help
works fine when I try your branch 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Souvikns maybe we should just resign from tests in this PR and write an issue about what I described above and get that solved first. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we need to go forward here:
@LakshyaSatpal please look at other comments, address them, and remove the test. There is no way to make it run now. The main functionality will have to stay untested 😄
also please add "additionalHelpFlags": ["-h"],
to oclif configuration in package.json
(line 97) so we at least make -h
to work too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's not add tests in this PR, but open an issue about how we cannot test the help messages. And once oclif fixes it we can add help tests for all other commands.
@LakshyaSatpal hey, you need a hand? I was out on holidays, but now back 💪🏼 |
new
command
src/commands/new.ts
Outdated
studio: Flags.boolean({ char: 's', description: 'open in Studio' }), | ||
port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }), | ||
'no-tty': Flags.boolean({ description: 'do not use an interactive terminal' }), | ||
}; | ||
|
||
static args = []; | ||
|
||
static examples = [ | ||
'asyncapi new\t - start creation of a file in interactive mode', | ||
'asyncapi new --file-name=my-asyncapi.yml --example=default-example.yml --no-tty\t - create new file with specific name, using one of examples and without interactive emode' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'asyncapi new --file-name=my-asyncapi.yml --example=default-example.yml --no-tty\t - create new file with specific name, using one of examples and without interactive emode' | |
'asyncapi new --file-name=my-asyncapi.yml --example=default-example.yml --no-tty\t - create a new file with a specific name, using one of the examples and without interactive mode' |
test/commands/new.test.ts
Outdated
|
||
test | ||
.stdout() | ||
.command(['new', '-h']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we need to go forward here:
@LakshyaSatpal please look at other comments, address them, and remove the test. There is no way to make it run now. The main functionality will have to stay untested 😄
also please add "additionalHelpFlags": ["-h"],
to oclif configuration in package.json
(line 97) so we at least make -h
to work too.
@LakshyaSatpal I hope you are still interested with this one. It helps with DX and as you see it is ok that we drop testing part |
…-API-cli into improve-new-help-msg
new
commandnew
command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great from my point of view. Tried locally and works like a charm
(improve-new-help-msg)$ bin/run new -h
Creates a new asyncapi file
USAGE
$ asyncapi new [-h] [-n <value>] [-e <value>] [-s] [-p <value>] [--no-tty]
FLAGS
-e, --example=<value>
name of the example to use. Available examples are:
- simple.yml
- anyof.yml
- application-headers.yml
- correlation-id.yml
- websocket-gemini.yml
- gitter-streaming.yml
- mercure.yml
- not.yml
- operation-security.yml
- oneof.yml
- rpc-client.yml
- rpc-server.yml
- slack-rtm.yml
- tutorial.yml
- streetlights-kafka.yml
- streetlights-operation-security.yml
- streetlights-mqtt.yml
-h, --help
Show CLI help.
-n, --file-name=<value>
name of the file
-p, --port=<value>
port in which to start Studio
-s, --studio
open in Studio
--no-tty
do not use an interactive terminal
DESCRIPTION
Creates a new asyncapi file
EXAMPLES
$ asyncapi new - start creation of a file in interactive mode
$ asyncapi new --file-name=my-asyncapi.yml --example=default-example.yml --no-tty - create a new file with a specific name, using one of the examples and without interactive mode
@Souvikns wanna have a look?
@Souvikns ping 😉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! just need to fix lint
@LakshyaSatpal hay can you fix linter issues please |
…-API-cli into improve-new-help-msg
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
I have no idea why tests are failing 😭 @LakshyaSatpal maybe you could switch to |
@LakshyaSatpal not sure why but you are having a lot of linter issues. Do you want to continue with this PR? |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
@LakshyaSatpal can you specify if you plan to continue with this one, or should that be released to other contributors? |
no reaction so closing, hope someone will pick it up from #162 |
done with #758 |
Description
Related issue(s)
Fixes #162