-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10608 from micalevisk/feat/enhance-native-help-co…
…mmand-repl feat(core): enhance repl '.help' command with nestjs functions
- Loading branch information
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters