Skip to content

Commit

Permalink
feat(core): add help native function to repl
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed May 31, 2022
1 parent 2b34854 commit 9484d3a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/core/repl/repl-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { InternalCoreModule } from '../injector/internal-core-module';
import { Module } from '../injector/module';
import { MetadataScanner } from '../metadata-scanner';
import { makeReplFnOpt, ReplFn } from './repl-fn.decorator';
import { REPL_METADATA_KEY } from './constants';
import type {
ReplMetadata,
ReplNativeFunctionMetadata,
} from './repl.interfaces';

type ModuleKey = string;
type ModuleDebugEntry = {
Expand All @@ -31,6 +36,34 @@ export class ReplContext {
this.initialize();
}

@ReplFn(
makeReplFnOpt('Display all available REPL native functions.', '() => void'),
)
help(): void {
const buildHelpMessage = ({
name,
description,
}: ReplNativeFunctionMetadata) =>
clc.cyanBright(name) +
(description ? ` ${clc.bold('-')} ${description}` : '');

const replMetadata: ReplMetadata = Reflect.getMetadata(
REPL_METADATA_KEY,
ReplContext,
);
const sortedNativeFunctions = replMetadata.nativeFunctions.sort((a, b) =>
a.name < b.name ? -1 : 1,
);
this.writeToStdout(
`You can call ${clc.bold(
'.help',
)} on any function listed below (e.g.: ${clc.bold('help.help')}):\n\n` +
sortedNativeFunctions.map(buildHelpMessage).join('\n') +
// Without the following LF the last item won't be displayed
'\n',
);
}

@ReplFn(
makeReplFnOpt(
'Retrieves an instance of either injectable or controller, otherwise, throws exception.',
Expand Down

0 comments on commit 9484d3a

Please sign in to comment.