Skip to content

fix: options for init command have required arguments - #1324

Closed
jamesgeorge007 wants to merge 2 commits into
react-native-community:masterfrom
jamesgeorge007:hotfix/required-args
Closed

jamesgeorge007 wants to merge 2 commits into
react-native-community:masterfrom
jamesgeorge007:hotfix/required-args

Conversation

@jamesgeorge007

Copy link
Copy Markdown
Contributor

Summary:

closes #1323

Test Plan:

@jamesgeorge007 jamesgeorge007 changed the title fix: options for init command has required arguments fix: options for init command have required arguments Nov 23, 2020
@thymikee

Copy link
Copy Markdown
Member

e2e tests are failing

@jamesgeorge007

Copy link
Copy Markdown
Contributor Author

e2e tests are failing

Fixed.

@jamesgeorge007
jamesgeorge007 force-pushed the hotfix/required-args branch 2 times, most recently from 9aa9861 to bbfe60a Compare November 23, 2020 11:11
@grabbou

grabbou commented Nov 23, 2020

Copy link
Copy Markdown
Member

BTW, we could've just set require: true to make it work in the current form w/o updating commander. I am a bit nervous about doing that kind of an update.

Do we have a list of all breaking changes between 2.x and 4.x?

@grabbou

grabbou commented Nov 23, 2020

Copy link
Copy Markdown
Member

Also, keep in mind that removing assertRequiredOptions is going to be a breaking change for all the users that currently rely on that functionality.

That said, I would accept a quick fix that just adds required: true to necessary options and a follow-up that bumps commander and removes that one.

@thymikee

Copy link
Copy Markdown
Member

I'm also hesitant on merging this at the moment. @grabbou suggestion is a good way to unblock ourselves for now, and once we hit a stable release we can bump commander after testing it thoroughly

@jamesgeorge007
jamesgeorge007 force-pushed the hotfix/required-args branch 2 times, most recently from 2b937db to 997ab33 Compare November 23, 2020 16:35
@jamesgeorge007

Copy link
Copy Markdown
Contributor Author

assertRequiredOptions() throws for not supplying a particular option, which doesn't satisfy the use case.

@jamesgeorge007
jamesgeorge007 force-pushed the hotfix/required-args branch 3 times, most recently from 367f3e5 to 8d996ea Compare November 23, 2020 17:33
@grabbou

grabbou commented Nov 23, 2020

Copy link
Copy Markdown
Member

Ah, of course - arguments are different than options! Anyway, I got a bit confused here - please help me out by answering my comments and I hope we can get through this!


if (!option.required) {
// Early return if the option is not required
if (!opt.required) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, the thing is... const option = new Option(opt.name); creates an Option object from the name, e.g. --version <string> or --version [string].

Depending on <> or [] used, the option.required will be true or false.

We shouldn't be adding required to CommandOption interface and doing an early return here - that's my bad, I got you confused in my other comment.

The way it works is that commander creates an Option object with required based on the name, but it doesn't do the actual validation and early returns in case the value is missing. That's what this code handles here.

In other words, we the code was fine as is. If we're handling arguments, we should be looking into a different place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The current implementation is misleading; having a required key would be better IMO. The intended way would be to use requiredOption() once we bump commander.js.

@grabbou

grabbou commented Nov 23, 2020

Copy link
Copy Markdown
Member

Technically, when we set --template <name>, we make it ALWAYS required. In other words, react-native init app will be invalid invocation.

Another technical detail, --template is as valid as --template name and that's just how Commander works. In that case, its value will be true, because it's treated as a boolean option.

The quickest way to do it would be to go here

const templateName = options.template || `react-native@${version}`;
and change this line to something that checks if options.template is String. If it's string, it's at least 1 character long.

@jamesgeorge007
jamesgeorge007 force-pushed the hotfix/required-args branch 2 times, most recently from 6b73b21 to ce46d40 Compare November 25, 2020 17:51
@grabbou

grabbou commented Nov 26, 2020

Copy link
Copy Markdown
Member

Looks good. I'll apply a tiny naming tweak and ship it. Thanks!

@grabbou

grabbou commented Nov 26, 2020

Copy link
Copy Markdown
Member

Unfortunately, while testing your PR, I have found out that we do support this feature out of the box without any additional work. All we have to do is to correct [string] with <string> when is required.

I have sent a follow-up PR to do it: #1328

I am really sorry that we have found this out THAT LATE in the process. I really appreciate your time and contributions and while this PR didn't get merged, we have brainstormed some interesting ideas together and have found out a better way to address the underlying problem!

#1328

@grabbou grabbou closed this Nov 26, 2020
@jamesgeorge007
jamesgeorge007 deleted the hotfix/required-args branch November 26, 2020 12:29
@jamesgeorge007

jamesgeorge007 commented Nov 26, 2020 •

Copy link
Copy Markdown
Contributor Author

Unfortunately, while testing your PR, I have found out that we do support this feature out of the box without any additional work. All we have to do is to correct [string] with <string> when is required.

In fact, this was the initial approach #1324 (comment)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

options for init command should have required arguments.

3 participants