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

feat(core,common): add help messages to REPL built-in functions #9695

Merged
merged 19 commits into from
Jun 1, 2022

Conversation

micalevisk
Copy link
Member

@micalevisk micalevisk commented May 31, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

same as PR #9692 but with other interface regarding REPL built-in functions

demo.mp4

What is the differences between them?

  1. ReplContext is much cleaner. It doesn't have to deal with those built-in functions
  2. no decorators (ie., no reflection)
  3. Every built-in function will live in their own class and aren't aware (directly) of the others functions
  4. To define our own 'built-in functions' we just need to implement classes that extends ReplFunction, and then we could pass a list of those class references to the main repl function (if we implement such feature in the feature). As of now, they will live along the native ones (help, debug, etc)

for instance, the get native function looks like this:

class GetReplFn extends ReplFunction {
  // metadata that describes the function itself
  public fnDefinition: ReplFnDefinition = {
    name: 'get',
    signature: '(token: InjectionToken) => any',
    description: 'Retrieves an instance of either injectable or controller, otherwise, throws exception.',
    // Optional list of aliases
    aliases: ['$'],
  };

  // The function to execute under the name `get`
  action(token: string | symbol | Function | Type<any>): any {
    this.ctx.app.get(token);
    // ^^^^^ ReplContext
  }
}

pros

  • ReplContext got easier to maintain
  • from nestjs user POV, I feel this API is much easier to follow and extend as there's no much magic happing. Users could write functions factories to build their classes easily

cons

  • we could have naming collision (that I didn't handle in this PR)
  • as the name is just an string, users could write invalid function names

Does this PR introduce a breaking change?

  • Yes
  • No

@micalevisk
Copy link
Member Author

I didn't change the test suite because I don't know if this proposal we be taken 😃

@kamilmysliwiec
Copy link
Member

Great work @micalevisk! Feel free to update tests, I'd love to merge this in :)

@micalevisk micalevisk marked this pull request as draft May 31, 2022 10:45
@micalevisk micalevisk marked this pull request as ready for review May 31, 2022 12:42
@micalevisk
Copy link
Member Author

micalevisk commented May 31, 2022

idk how to fix those failing assertions

@kamilmysliwiec
Copy link
Member

As for the unit tests that are failing, I suppose there's a tabs/spaces/new lines mismatch between expected vs actual.

@micalevisk
Copy link
Member Author

yeah, I didn't notice those trailing spaces. Also, I've moved the nested function under printCollection to a private method instead so it won't recreate a new one every time we use the debug function

@coveralls
Copy link

coveralls commented May 31, 2022

Pull Request Test Coverage Report for Build c33f096f-7875-4e22-ba67-59ad23501b92

  • 87 of 92 (94.57%) changed or added relevant lines in 10 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 94.122%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/utils/cli-colors.util.ts 0 1 0.0%
packages/core/repl/native-functions/debug-repl-fn.ts 22 23 95.65%
packages/core/repl/repl-function.ts 4 7 57.14%
Totals Coverage Status
Change from base Build a9d09da6-b7e1-45b9-8303-f0266c9e006a: 0.03%
Covered Lines: 5893
Relevant Lines: 6261

💛 - Coveralls

@micalevisk
Copy link
Member Author

micalevisk commented May 31, 2022

all done. I tried my best to write this in a way that it won't affect the bootstrapping time (without complicating the overall logic)

image

@micalevisk micalevisk force-pushed the feat/repl-improvements-v2 branch 3 times, most recently from 5b56a27 to fdefb1d Compare June 1, 2022 01:33
@micalevisk micalevisk changed the title feat(core,common): add helpers messages to REPL built-in functions feat(core,common): add help messages to REPL built-in functions Jun 1, 2022
@micalevisk micalevisk force-pushed the feat/repl-improvements-v2 branch 4 times, most recently from 727cec7 to 0ceda2f Compare June 1, 2022 02:37
@kamilmysliwiec
Copy link
Member

Fantastic contribution @micalevisk! 🙌

@kamilmysliwiec kamilmysliwiec merged commit 9fd45a9 into nestjs:feat/repl Jun 1, 2022
@micalevisk micalevisk deleted the feat/repl-improvements-v2 branch June 1, 2022 10:45
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.

None yet

4 participants