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

Refactor index.js #1496

Closed
Niryo opened this issue Apr 5, 2021 · 7 comments
Closed

Refactor index.js #1496

Niryo opened this issue Apr 5, 2021 · 7 comments
Assignees
Milestone

Comments

@Niryo
Copy link
Contributor

Niryo commented Apr 5, 2021

Are we intentionally keeping this god index.js file as is, or are there plans for breaking it down to more manageable pieces?
I am interested in contributing more stuff to this lib, and I thought that breaking up this huge file could be a good start if there are no objections...

@shadowspawn
Copy link
Collaborator

My main concern with splitting up the monolithic index.js is if there are any common client use cases that would be broken by changing to multiple files. (No?)

A minor downside is refactoring the file makes it a harder to track back through the history to find why certain lines of code and logic were introduced. But the benefits to future work outweigh this.

For interest, the file was split up in #661 in a branch to modernise the codebase for v3.

@Niryo
Copy link
Contributor Author

Niryo commented Apr 6, 2021

About the first concern- I am not sure I can't think of a use case where client could be broken because of such an internal change, as long as the api stays the same. The api is the contract, everything else can be changed. If someone is doing some custom scripting based on anything else other than api, it's not commander.js responsibility if it's broken.

About the downside- yep, this is an annoying limitation of git, but as you said, the benefits to future work outweigh the this.
And if you really need to track back through the history, you can still do it, nothing is lost, it just became less convenient.

For me it was super annoying to orient myself inside this huge index.js file, and a split is a no-brainer decision.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Apr 14, 2021

I am having a play around with refactoring. Naively splitting Help and Command introduced a circular dependency, but @cravler supplied a work-around with subclass to break the loop. One class per file.

https://github.com/tj/commander.js/tree/feature/refactor-experiment

As of 17 Apr the extracted command.js is 1655 lines, compared with the single file index.js of 2325 lines.

@shadowspawn
Copy link
Collaborator

shadowspawn commented May 8, 2021

Non-binding feedback, should Commander split up index.js like in refactor-experiment branch? 👍 👎

@abetomo @cravler @Niryo , and any other readers welcome to add feedback too!

@shadowspawn
Copy link
Collaborator

I'll have another go at this when Pull Requests are quiet.

@shadowspawn shadowspawn self-assigned this May 12, 2021
@Niryo
Copy link
Contributor Author

Niryo commented May 12, 2021

few notes:

  1. I am not sure what are the implications of cyclic dependencies here, maybe when can live with it if we can't find any workarounds. But my hunch is that we can probably find a way to structure things in such a way that remove the cyclic dependency.
  2. Even if keep those classes in the same file, we will still benefit from extracting small classes, even minor ones.
  3. We can also extract pure functions. currently we have many big methods that sits inside the classes themselves, and some of them can be transformed to pure functions sittings outside the classes (where it makes sense of course, not all methods should be extracted this way).

@shadowspawn shadowspawn added this to the v8.0.0 milestone May 12, 2021
@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label May 25, 2021
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Jun 25, 2021
@shadowspawn
Copy link
Collaborator

Released in Commander v8.

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

No branches or pull requests

2 participants