-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): enhance repl 'help' command with nestjs functions
- Loading branch information
1 parent
220b098
commit d8ece48
Showing
2 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { REPLServer } from 'repl'; | ||
|
||
/** | ||
* Displays the list of available commands in the REPL alonside 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} | ||
*/ | ||
/* istanbul ignore next */ | ||
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