Skip to content

Commit b4e156b

Browse files
committed
Migrate CLI Command
1 parent 3d7bb61 commit b4e156b

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/core/cli/command.discovery.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
import {
2-
type DiscoveredModule,
3-
DiscoveryService,
4-
} from '@golevelup/nestjs-discovery';
51
import { Injectable } from '@nestjs/common';
6-
import { Command } from 'clipanion';
2+
import { type Command } from 'clipanion';
3+
import { MetadataDiscovery } from '~/core/discovery';
4+
import { CommandWatermark } from './decorator';
75

86
@Injectable()
97
export class CommandDiscovery {
10-
constructor(private readonly discovery: DiscoveryService) {}
8+
constructor(private readonly discovery: MetadataDiscovery) {}
119

1210
async discover() {
13-
const discovered = await this.discovery.providersWithMetaAtKey(
14-
Command as any,
15-
);
16-
return discovered.map((d) => {
17-
const command = d.discoveredClass as DiscoveredModule<Command>;
18-
return new Proxy(command.dependencyType, {
19-
construct() {
20-
return command.instance;
21-
},
22-
});
23-
});
11+
return this.discovery
12+
.discover(CommandWatermark)
13+
.classes<Command>()
14+
.map(
15+
({ instance: command }) =>
16+
new Proxy(command.constructor, {
17+
construct() {
18+
return command;
19+
},
20+
}),
21+
);
2422
}
2523
}

src/core/cli/decorator.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { applyDecorators, Injectable, SetMetadata } from '@nestjs/common';
2-
import { Command } from 'clipanion';
1+
import { Injectable } from '@nestjs/common';
2+
import { createMetadataDecorator } from '@seedcompany/nest';
3+
import { type Command } from 'clipanion';
34
import { type AbstractClass } from 'type-fest';
45

6+
export const CommandWatermark = createMetadataDecorator({
7+
types: ['class'],
8+
additionalDecorators: [Injectable()],
9+
});
10+
511
export const InjectableCommand = () => (cls: AbstractClass<Command>) =>
6-
applyDecorators(Injectable(), SetMetadata(Command, true))(cls);
12+
CommandWatermark()(cls);

src/core/gel/schema-ast/access-policy.commands.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import { Injectable } from '@nestjs/common';
12
import { Command, Option } from 'clipanion';
23
import { $, execa } from 'execa';
34
import { realpath } from 'node:fs/promises';
45
import { tmpdir as getTempDir } from 'node:os';
56
import { InjectableCommand } from '~/core';
67
import { GelAccessPolicyInjector } from './access-policy.injector';
78

8-
@InjectableCommand()
9+
@Injectable()
910
abstract class ApCommand extends Command {
1011
constructor(protected readonly injector: GelAccessPolicyInjector) {
1112
super();
1213
}
1314
}
1415

16+
@InjectableCommand()
1517
export class GelAccessPolicyWrapCommand extends ApCommand {
1618
static paths = [['gel']];
1719
static usage = Command.Usage({
@@ -51,6 +53,7 @@ export class GelAccessPolicyWrapCommand extends ApCommand {
5153
}
5254
}
5355

56+
@InjectableCommand()
5457
export class GelAccessPolicyInjectCommand extends ApCommand {
5558
static paths = [['gel', 'ap', 'inject']];
5659
static usage = Command.Usage({
@@ -63,6 +66,7 @@ export class GelAccessPolicyInjectCommand extends ApCommand {
6366
}
6467
}
6568

69+
@InjectableCommand()
6670
export class GelAccessPolicyEjectCommand extends ApCommand {
6771
static paths = [['gel', 'ap', 'eject']];
6872
static usage = Command.Usage({

0 commit comments

Comments
 (0)