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): enhance repl '.help' command with nestjs functions #10608

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/repl/native-functions/debug-repl-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class DebugReplFn extends ReplFunction {
public fnDefinition: ReplFnDefinition = {
name: 'debug',
description:
'Print all registered modules as a list together with their controllers and providers. If the argument is passed in, for example, "debug(MyModule)" then it will only print components of this specific module.',
'Print all registered modules as a list together with their controllers and providers.\nIf the argument is passed in, for example, "debug(MyModule)" then it will only print components of this specific module.',
signature: '(moduleCls?: ClassRef | string) => void',
};

Expand Down
50 changes: 50 additions & 0 deletions packages/core/repl/repl-native-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { REPLServer } from 'repl';

/**
* Displays a list of available commands in the REPL alongside with their
* descriptions.
* (c) This code was inspired by the 'help' command from Node.js core:
* {@link https://github.com/nodejs/node/blob/58b60c1393dd65cd228a8b0084a19acd2c1d16aa/lib/repl.js#L1741-L1759}
*/
function listAllCommands(replServer: REPLServer) {
Object.keys(replServer.commands)
.sort()
.forEach(name => {
const cmd = replServer.commands[name];
if (cmd) {
replServer.output.write(`${name}\t${cmd.help || ''}\n`);
}
});
}

export function defineDefaultCommandsOnRepl(replServer: REPLServer): void {
replServer.defineCommand('help', {
help: 'Show REPL options',
action(name?: string) {
this.clearBufferedCommand();

if (name) {
// Considering native commands before native nestjs injected functions.
const nativeCommandOrFunction =
this.commands[name] || this.context[name];
// NOTE: If the command was retrieve from the context, it will have a `help`
// getter property that outputs the helper message and returns undefined.
// But if the command was retrieve from the `commands` object, it will
// have a `help` property that returns the helper message.
const helpMessage = nativeCommandOrFunction?.help;
if (helpMessage) {
this.output.write(`${helpMessage}\n`);
}
} else {
listAllCommands(this);
this.output.write('\n\n');
this.context.help();
this.output.write(
'\nPress Ctrl+C to abort current expression, Ctrl+D to exit the REPL\n',
);
}

this.displayPrompt();
},
});
}
3 changes: 3 additions & 0 deletions packages/core/repl/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assignToObject } from './assign-to-object.util';
import { REPL_INITIALIZED_MESSAGE } from './constants';
import { ReplContext } from './repl-context';
import { ReplLogger } from './repl-logger';
import { defineDefaultCommandsOnRepl } from './repl-native-commands';

export async function repl(module: Type | DynamicModule) {
const app = await NestFactory.createApplicationContext(module, {
Expand All @@ -23,5 +24,7 @@ export async function repl(module: Type | DynamicModule) {
});
assignToObject(replServer.context, replContext.globalScope);

defineDefaultCommandsOnRepl(replServer);

return replServer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('HelpReplFn', () => {
.equal(`You can call .help on any function listed below (e.g.: help.help):

$ - Retrieves an instance of either injectable or controller, otherwise, throws exception.
debug - Print all registered modules as a list together with their controllers and providers. If the argument is passed in, for example, "debug(MyModule)" then it will only print components of this specific module.
debug - Print all registered modules as a list together with their controllers and providers.
If the argument is passed in, for example, "debug(MyModule)" then it will only print components of this specific module.
get - Retrieves an instance of either injectable or controller, otherwise, throws exception.
help - Display all available REPL native functions.
methods - Display all public methods available on a given provider or controller.
Expand Down