Skip to content

Commit 8b210a4

Browse files
committed
refactor: replace Command decorator with command function for improved consistency
1 parent 47cc6b1 commit 8b210a4

File tree

22 files changed

+245
-241
lines changed

22 files changed

+245
-241
lines changed

packages/cli-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Inquirer from "inquirer";
44

55
export * from "./CliCore.js";
66
export * from "./decorators/index.js";
7+
export * from "./fn/command.js";
78
export * from "./interfaces/index.js";
89
export * from "./packageManagers/index.js";
910
export * from "./services/index.js";

packages/cli-generate-http-client/src/commands/GenerateHttpClientCmd.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import path, {join, resolve} from "node:path";
1+
import {join, resolve} from "node:path";
22

3-
import {CliFs, Command, type CommandProvider, constant, inject, Type} from "@tsed/cli-core";
3+
import {CliFs, command, type CommandProvider, constant, inject, Type} from "@tsed/cli-core";
44
import {isString} from "@tsed/core";
55
import {InjectorService} from "@tsed/di";
66
import {camelCase} from "change-case";
@@ -19,35 +19,6 @@ export interface GenerateHttpClientOpts {
1919
transformOperationId?(operationId: string, routeNameInfo: RouteNameInfo, raw: RawRouteInfo): string;
2020
}
2121

22-
@Command({
23-
name: "generate-http-client",
24-
description: "Generate the client API from swagger spec",
25-
options: {
26-
"-s, --suffix <suffix>": {
27-
required: false,
28-
type: String,
29-
defaultValue: "Model",
30-
description: "The suffix applied on model"
31-
},
32-
"-t, --type <type>": {
33-
required: false,
34-
type: String,
35-
defaultValue: "axios",
36-
description: "The client type by the Http client (axios or fetch)"
37-
},
38-
"-n, --name <name>": {
39-
required: false,
40-
type: String,
41-
defaultValue: "ApiClient",
42-
description: "The class name of the generated client"
43-
},
44-
"-o, --output <output>": {
45-
required: true,
46-
type: String,
47-
description: "Path to generate files"
48-
}
49-
}
50-
})
5122
export class GenerateHttpClientCmd implements CommandProvider {
5223
protected fs = inject(CliFs);
5324
protected serverModule = constant<Type<any>>("server");
@@ -195,3 +166,33 @@ export class GenerateHttpClientCmd implements CommandProvider {
195166
return routeNameInfo;
196167
}
197168
}
169+
170+
command(GenerateHttpClientCmd, {
171+
name: "generate-http-client",
172+
description: "Generate the client API from swagger spec",
173+
options: {
174+
"-s, --suffix <suffix>": {
175+
required: false,
176+
type: String,
177+
defaultValue: "Model",
178+
description: "The suffix applied on model"
179+
},
180+
"-t, --type <type>": {
181+
required: false,
182+
type: String,
183+
defaultValue: "axios",
184+
description: "The client type by the Http client (axios or fetch)"
185+
},
186+
"-n, --name <name>": {
187+
required: false,
188+
type: String,
189+
defaultValue: "ApiClient",
190+
description: "The class name of the generated client"
191+
},
192+
"-o, --output <output>": {
193+
required: true,
194+
type: String,
195+
description: "Path to generate files"
196+
}
197+
}
198+
});

packages/cli-generate-swagger/src/commands/GenerateSwaggerCmd.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
import path, {join, resolve} from "node:path";
22

3-
import {CliFs, CliYaml, Command, type CommandProvider, Constant, constant, Inject, inject, InjectorService, Type} from "@tsed/cli-core";
3+
import {CliFs, CliYaml, command, type CommandProvider, constant, inject, InjectorService, Type} from "@tsed/cli-core";
44

55
export interface GenerateSwaggerCtx {
66
output: string;
77
}
88

9-
@Command({
10-
name: "generate-swagger",
11-
description: "Generate the client API from swagger spec",
12-
options: {
13-
"-o, --output <output>": {
14-
required: true,
15-
type: String,
16-
description: "Path to generate files"
17-
}
18-
}
19-
})
209
export class GenerateSwaggerCmd implements CommandProvider {
2110
protected fs = inject(CliFs);
2211
protected cliYaml = inject(CliYaml);
@@ -103,3 +92,15 @@ export class GenerateSwaggerCmd implements CommandProvider {
10392
await Promise.all([this.fs.writeJson(fileJson, spec), this.cliYaml.write(fileYaml, spec)]);
10493
}
10594
}
95+
96+
command(GenerateSwaggerCmd, {
97+
name: "generate-swagger",
98+
description: "Generate the client API from swagger spec",
99+
options: {
100+
"-o, --output <output>": {
101+
required: true,
102+
type: String,
103+
description: "Path to generate files"
104+
}
105+
}
106+
});
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {Module} from "@tsed/cli-core";
1+
import {injectable} from "@tsed/di";
22

33
import {EslintInitHook} from "./hooks/EslintInitHook.js";
44

5-
@Module({
6-
imports: [EslintInitHook]
7-
})
85
export class CliPluginEslintModule {}
6+
7+
injectable(CliPluginEslintModule).imports([EslintInitHook]);

packages/cli/src/commands/add/AddCmd.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
type CliDefaultOptions,
33
CliPlugins,
4-
Command,
4+
command,
55
type CommandProvider,
66
createSubTasks,
77
inject,
@@ -15,16 +15,6 @@ export interface AddCmdOptions extends CliDefaultOptions {
1515
name: string;
1616
}
1717

18-
@Command({
19-
name: "add",
20-
description: "Add cli plugin to the current project",
21-
args: {
22-
name: {
23-
description: "Npm package name of the cli plugin",
24-
type: String
25-
}
26-
}
27-
})
2818
export class AddCmd implements CommandProvider {
2919
protected cliPlugins = inject(CliPlugins);
3020
protected packageJson = inject(ProjectPackageJson);
@@ -64,3 +54,14 @@ export class AddCmd implements CommandProvider {
6454
];
6555
}
6656
}
57+
58+
command(AddCmd, {
59+
name: "add",
60+
description: "Add cli plugin to the current project",
61+
args: {
62+
name: {
63+
description: "Npm package name of the cli plugin",
64+
type: String
65+
}
66+
}
67+
});

packages/cli/src/commands/generate/GenerateCmd.ts

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {basename, dirname, join} from "node:path";
22

3-
import {type CliDefaultOptions, Command, type CommandProvider, Inject, inject, ProjectPackageJson} from "@tsed/cli-core";
3+
import {type CliDefaultOptions, command, type CommandProvider, inject, ProjectPackageJson} from "@tsed/cli-core";
44
import {normalizePath} from "@tsed/normalize-path";
55
import {kebabCase, pascalCase} from "change-case";
66
import {globbySync} from "globby";
@@ -52,53 +52,15 @@ const searchFactory = (list: any) => {
5252
};
5353
};
5454

55-
@Command({
56-
name: "generate",
57-
alias: "g",
58-
description: "Generate a new provider class",
59-
args: {
60-
type: {
61-
description: "Type of the provider (Injectable, Controller, Pipe, etc...)",
62-
type: String
63-
},
64-
name: {
65-
description: "Name of the class",
66-
type: String
67-
}
68-
},
69-
options: {
70-
"--route <route>": {
71-
type: String,
72-
description: "The route for the controller generated file"
73-
},
74-
"-d, --directory <directory>": {
75-
description: "Directory where the file must be generated",
76-
type: String
77-
},
78-
"-t, --template-type <templateType>": {
79-
description: "Directory where the file must be generated",
80-
type: String
81-
},
82-
"-m, --middleware-position <templateType>": {
83-
description: "Middleware position (before, after)",
84-
type: String
85-
}
86-
}
87-
})
8855
export class GenerateCmd implements CommandProvider {
89-
@Inject()
90-
classNamePipe: ClassNamePipe;
91-
92-
@Inject()
93-
outputFilePathPipe: OutputFilePathPipe;
94-
95-
@Inject()
96-
routePipe: RoutePipe;
56+
protected classNamePipe = inject(ClassNamePipe);
57+
protected outputFilePathPipe = inject(OutputFilePathPipe);
58+
protected routePipe = inject(RoutePipe);
59+
protected srcRenderService = inject(SrcRendererService);
60+
protected projectPackageJson = inject(ProjectPackageJson);
61+
protected providersList = inject(ProvidersInfoService);
9762

98-
srcRenderService = inject(SrcRendererService);
99-
projectPackageJson = inject(ProjectPackageJson);
100-
101-
constructor(private providersList: ProvidersInfoService) {
63+
constructor() {
10264
PROVIDER_TYPES.forEach((info) => {
10365
this.providersList.add(
10466
{
@@ -288,3 +250,37 @@ export class GenerateCmd implements CommandProvider {
288250
return [...set];
289251
}
290252
}
253+
254+
command(GenerateCmd, {
255+
name: "generate",
256+
alias: "g",
257+
description: "Generate a new provider class",
258+
args: {
259+
type: {
260+
description: "Type of the provider (Injectable, Controller, Pipe, etc...)",
261+
type: String
262+
},
263+
name: {
264+
description: "Name of the class",
265+
type: String
266+
}
267+
},
268+
options: {
269+
"--route <route>": {
270+
type: String,
271+
description: "The route for the controller generated file"
272+
},
273+
"-d, --directory <directory>": {
274+
description: "Directory where the file must be generated",
275+
type: String
276+
},
277+
"-t, --template-type <templateType>": {
278+
description: "Directory where the file must be generated",
279+
type: String
280+
},
281+
"-m, --middleware-position <templateType>": {
282+
description: "Middleware position (before, after)",
283+
type: String
284+
}
285+
}
286+
});

0 commit comments

Comments
 (0)