Skip to content

Commit

Permalink
feat(app): allow passing command function without name
Browse files Browse the repository at this point in the history
```
function helloWorld(name: string) {
}

const app = new App();
app.command(helloWorld);
app.run();
```

registers `hello-world [name]` as CLI command.
  • Loading branch information
marcj committed Jan 23, 2024
1 parent 488247a commit 6796414
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
8 changes: 5 additions & 3 deletions packages/app/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export class App<T extends RootModuleDefinition> {
return this;
}

command(name: string, callback: (...args: any[]) => any): this {
command(name: string | ((...args: any[]) => any), callback?: (...args: any[]) => any): this {
callback = isFunction(name) ? name : callback!;
name = isFunction(name) ? '' : name;
this.appModule.addCommand(name, callback);
return this;
}
Expand Down Expand Up @@ -348,7 +350,6 @@ export class App<T extends RootModuleDefinition> {
}

const scopedInjectorContext = this.getInjectorContext().createChildScope('cli');
const commands = [...this.serviceContainer.cliControllerRegistry.controllers.entries()];

if ('string' !== typeof bin) {
bin = bin || getBinFromEnvironment();
Expand All @@ -360,7 +361,8 @@ export class App<T extends RootModuleDefinition> {
return await executeCommand(
bin, argv || getArgsFromEnvironment(),
eventDispatcher, logger,
scopedInjectorContext, commands.map(c => c[1])
scopedInjectorContext,
this.serviceContainer.cliControllerRegistry.controllers
);
}
}
6 changes: 3 additions & 3 deletions packages/app/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class AppModule<T extends RootModuleDefinition = {}, C extends ExtractCla

public imports: AppModule<any>[] = [];
public controllers: ClassType[] = [];
public commands: { name: string, callback: Function }[] = [];
public commands: { name?: string, callback: Function }[] = [];
public workflows: WorkflowDefinition<any>[] = [];
public listeners: ListenerType[] = [];
public middlewares: MiddlewareFactory[] = [];
Expand Down Expand Up @@ -369,11 +369,11 @@ export class AppModule<T extends RootModuleDefinition = {}, C extends ExtractCla
return this.controllers;
}

getCommands(): { name: string, callback: Function }[] {
getCommands(): { name?: string, callback: Function }[] {
return this.commands;
}

addCommand(name: string, callback: (...args: []) => any): this {
addCommand(name: string | undefined, callback: (...args: []) => any): this {
this.assertInjectorNotBuilt();
this.commands.push({ name, callback });
return this;
Expand Down
11 changes: 7 additions & 4 deletions packages/app/src/service-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ControllerConfig {
}

export class CliControllerRegistry {
public readonly controllers = new Map<string, ControllerConfig>();
public readonly controllers: ControllerConfig[] = [];
}

export type MiddlewareRegistryEntry = { config: MiddlewareConfig, module: AppModule<any> };
Expand Down Expand Up @@ -303,19 +303,22 @@ export class ServiceContainer {
protected processController(module: AppModule<any>, controller: ControllerConfig) {
let name = controller.name || '';
if (controller.controller) {

if (!name) {
const cliConfig = cli._fetch(controller.controller);
if (cliConfig) {
name = cliConfig.name || '';
controller.name = name || cliConfig.name || '';

//make sure CLI controllers are provided in cli scope
if (!module.isProvided(controller.controller)) {
module.addProvider({ provide: controller.controller, scope: 'cli' });
}
this.cliControllerRegistry.controllers.set(name, controller);
this.cliControllerRegistry.controllers.push(controller);
}
}

} else if (controller.for === 'cli') {
this.cliControllerRegistry.controllers.set(name, controller);
this.cliControllerRegistry.controllers.push(controller);
}

for (const m of this.modules) {
Expand Down
23 changes: 0 additions & 23 deletions packages/app/tests/command.spec.ts

This file was deleted.

0 comments on commit 6796414

Please sign in to comment.