@tsed/cli 4.0.1 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/commands/generate/GenerateCmd.js +23 -4
- package/lib/cjs/commands/generate/GenerateCmd.js.map +1 -1
- package/lib/esm/commands/generate/GenerateCmd.js +23 -4
- package/lib/esm/commands/generate/GenerateCmd.js.map +1 -1
- package/lib/types/commands/generate/GenerateCmd.d.ts +2 -1
- package/package.json +2 -2
|
@@ -93,7 +93,7 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
93
93
|
name: "route",
|
|
94
94
|
message: "Which route?",
|
|
95
95
|
when(state) {
|
|
96
|
-
return ["controller", "server"].includes(state.type || initialOptions.type);
|
|
96
|
+
return !!(["controller", "server"].includes(state.type || initialOptions.type) || initialOptions.route);
|
|
97
97
|
},
|
|
98
98
|
default: (state) => {
|
|
99
99
|
return state.type === "server" ? "/rest" : this.routePipe.transform(getName(state));
|
|
@@ -104,7 +104,7 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
104
104
|
name: "directory",
|
|
105
105
|
message: "Which directory?",
|
|
106
106
|
when(state) {
|
|
107
|
-
return ["controller"].includes(state.type || initialOptions.type);
|
|
107
|
+
return ["controller"].includes(state.type || initialOptions.type) || initialOptions.directory;
|
|
108
108
|
},
|
|
109
109
|
choices: this.getDirectories("controllers")
|
|
110
110
|
},
|
|
@@ -113,7 +113,7 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
113
113
|
name: "templateType",
|
|
114
114
|
message: (state) => `Which type of ${state.type || initialOptions.type}?`,
|
|
115
115
|
when(state) {
|
|
116
|
-
return ["decorator"].includes(state.type || initialOptions.type);
|
|
116
|
+
return !!(["decorator"].includes(state.type || initialOptions.type) || initialOptions.templateType);
|
|
117
117
|
},
|
|
118
118
|
source: searchFactory(DECORATOR_TYPES)
|
|
119
119
|
},
|
|
@@ -126,7 +126,8 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
126
126
|
{ name: "After the endpoint", value: "after" }
|
|
127
127
|
],
|
|
128
128
|
when(state) {
|
|
129
|
-
return ["decorator"].includes(state.type || initialOptions.type) && ["middleware"].includes(state.templateType)
|
|
129
|
+
return !!((["decorator"].includes(state.type || initialOptions.type) && ["middleware"].includes(state.templateType)) ||
|
|
130
|
+
initialOptions.middlewarePosition);
|
|
130
131
|
}
|
|
131
132
|
}
|
|
132
133
|
];
|
|
@@ -236,6 +237,24 @@ GenerateCmd = GenerateCmd_1 = tslib_1.__decorate([
|
|
|
236
237
|
description: "Name of the class",
|
|
237
238
|
type: String
|
|
238
239
|
}
|
|
240
|
+
},
|
|
241
|
+
options: {
|
|
242
|
+
"-r, --route <route>": {
|
|
243
|
+
type: String,
|
|
244
|
+
description: "The route for the controller generated file"
|
|
245
|
+
},
|
|
246
|
+
"-d, --directory <directory>": {
|
|
247
|
+
description: "Directory where the file must be generated",
|
|
248
|
+
type: String
|
|
249
|
+
},
|
|
250
|
+
"-t, --template-type <templateType>": {
|
|
251
|
+
description: "Directory where the file must be generated",
|
|
252
|
+
type: String
|
|
253
|
+
},
|
|
254
|
+
"-m, --middleware-position <templateType>": {
|
|
255
|
+
description: "Middleware position (before, after)",
|
|
256
|
+
type: String
|
|
257
|
+
}
|
|
239
258
|
}
|
|
240
259
|
}),
|
|
241
260
|
tslib_1.__metadata("design:paramtypes", [ProvidersInfoService_1.ProvidersInfoService])
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenerateCmd.js","sourceRoot":"","sources":["../../../../src/commands/generate/GenerateCmd.ts"],"names":[],"mappings":";;;;;AAAA,6CAA2H;AAC3H,6CAAkD;AAClD,+BAA6C;AAC7C,4DAA4B;AAC5B,4EAA2C;AAC3C,6DAAwD;AACxD,uEAAkE;AAClE,qDAAgD;AAChD,8EAAyE;AACzE,mDAA+C;AAC/C,0EAAqE;AACrE,yDAAoD;AAepD,M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,MAAM;aACb;SACF;KACF,CAAC;6CAiBmC,2CAAoB;GAhB5C,WAAW,CAwMvB;AAxMY,kCAAW","sourcesContent":["import {CliDefaultOptions, Command, CommandProvider, Inject, ProjectPackageJson, SrcRendererService} from \"@tsed/cli-core\";\nimport {paramCase, pascalCase} from \"change-case\";\nimport {basename, dirname, join} from \"path\";\nimport globby from \"globby\";\nimport normalizePath from \"normalize-path\";\nimport {ClassNamePipe} from \"../../pipes/ClassNamePipe\";\nimport {OutputFilePathPipe} from \"../../pipes/OutputFilePathPipe\";\nimport {RoutePipe} from \"../../pipes/RoutePipe\";\nimport {ProvidersInfoService} from \"../../services/ProvidersInfoService\";\nimport {PROVIDER_TYPES} from \"./ProviderTypes\";\nimport {ProjectConvention} from \"../../interfaces/ProjectConvention\";\nimport {fillImports} from \"../../utils/fillImports\";\n\nexport interface GenerateCmdContext extends CliDefaultOptions {\n type: string;\n name: string;\n route: string;\n directory: string;\n platform: string;\n templateType: string;\n symbolName: string;\n symbolPath: string;\n symbolPathBasename: string;\n convention: ProjectConvention;\n}\n\nconst DECORATOR_TYPES = [\n {name: \"Class decorator\", value: \"class\"},\n {name: \"Ts.ED middleware and its decorator\", value: \"middleware\"},\n {name: \"Ts.ED endpoint decorator\", value: \"endpoint\"},\n {name: \"Ts.ED property decorator\", value: \"prop\"},\n {name: \"Ts.ED parameter decorator\", value: \"param\"},\n {name: \"Vanilla Method decorator\", value: \"method\"},\n {name: \"Vanilla Property decorator\", value: \"property\"},\n {name: \"Vanilla Parameter decorator\", value: \"parameter\"},\n {name: \"Generic decorator\", value: \"generic\"}\n];\n\nconst searchFactory = (list: any) => {\n return async (state: any, keyword: string) => {\n if (keyword) {\n return list.filter((item: any) => item.name.toLowerCase().includes(keyword.toLowerCase()));\n }\n\n return list;\n };\n};\n\n@Command({\n name: \"generate\",\n alias: \"g\",\n description: \"Generate a new provider class\",\n args: {\n type: {\n description: \"Type of the provider (Injectable, Controller, Pipe, etc...)\",\n type: String\n },\n name: {\n description: \"Name of the class\",\n type: String\n }\n }\n})\nexport class GenerateCmd implements CommandProvider {\n @Inject()\n classNamePipe: ClassNamePipe;\n\n @Inject()\n outputFilePathPipe: OutputFilePathPipe;\n\n @Inject()\n routePipe: RoutePipe;\n\n @Inject()\n srcRenderService: SrcRendererService;\n\n @Inject()\n projectPackageJson: ProjectPackageJson;\n\n constructor(private providersList: ProvidersInfoService) {\n PROVIDER_TYPES.forEach((info) => {\n this.providersList.add(\n {\n ...info\n },\n GenerateCmd\n );\n });\n }\n\n $prompt(initialOptions: Partial<GenerateCmdContext>) {\n const getName = (state: any) =>\n initialOptions.name || pascalCase(state.name || initialOptions.name || state.type || initialOptions.type);\n\n const proposedProviders = this.providersList.findProviders(initialOptions.type);\n\n return [\n {\n type: \"autocomplete\",\n name: \"type\",\n message: \"Which type of provider?\",\n default: initialOptions.type,\n when: () => proposedProviders.length > 1,\n source: searchFactory(proposedProviders)\n },\n {\n type: \"input\",\n name: \"name\",\n message: \"Which name?\",\n default: getName,\n when: !initialOptions.name\n },\n {\n message: \"Which platform?\",\n type: \"list\",\n name: \"platform\",\n when(state: any) {\n return [\"server\"].includes(state.type || initialOptions.type);\n },\n choices: [\n {\n name: \"Express.js\",\n checked: true,\n value: \"express\"\n },\n {\n name: \"Koa.js\",\n checked: false,\n value: \"koa\"\n }\n ]\n },\n {\n type: \"input\",\n name: \"route\",\n message: \"Which route?\",\n when(state: any) {\n return [\"controller\", \"server\"].includes(state.type || initialOptions.type);\n },\n default: (state: GenerateCmdContext) => {\n return state.type === \"server\" ? \"/rest\" : this.routePipe.transform(getName(state));\n }\n },\n {\n type: \"list\",\n name: \"directory\",\n message: \"Which directory?\",\n when(state: any) {\n return [\"controller\"].includes(state.type || initialOptions.type);\n },\n choices: this.getDirectories(\"controllers\")\n },\n {\n type: \"autocomplete\",\n name: \"templateType\",\n message: (state: any) => `Which type of ${state.type || initialOptions.type}?`,\n when(state: any) {\n return [\"decorator\"].includes(state.type || initialOptions.type);\n },\n source: searchFactory(DECORATOR_TYPES)\n },\n {\n type: \"list\",\n name: \"middlewarePosition\",\n message: () => `The middleware should be called:`,\n choices: [\n {name: \"Before the endpoint\", value: \"before\"},\n {name: \"After the endpoint\", value: \"after\"}\n ],\n when(state: any) {\n return [\"decorator\"].includes(state.type || initialOptions.type) && [\"middleware\"].includes(state.templateType);\n }\n }\n ];\n }\n\n $mapContext(ctx: Partial<GenerateCmdContext>): GenerateCmdContext {\n const {name = \"\"} = ctx;\n let {type = \"\"} = ctx;\n type = type.toLowerCase();\n\n if (ctx.name === \"prisma\" && ctx.name) {\n type = \"prisma.service\";\n }\n\n const symbolName = this.classNamePipe.transform({name, type, format: ProjectConvention.DEFAULT});\n const symbolParamName = paramCase(symbolName);\n\n return fillImports({\n ...ctx,\n type,\n route: ctx.route ? this.routePipe.transform(ctx.route) : \"\",\n symbolName,\n symbolParamName,\n symbolPath: normalizePath(\n this.outputFilePathPipe.transform({\n name,\n type,\n subDir: ctx.directory\n })\n ),\n symbolPathBasename: normalizePath(this.classNamePipe.transform({name, type})),\n platformSymbol: ctx.platform && pascalCase(`Platform ${ctx.platform}`)\n }) as GenerateCmdContext;\n }\n\n async $exec(ctx: GenerateCmdContext) {\n const {symbolPath} = ctx;\n\n if (this.providersList.isMyProvider(ctx.type, GenerateCmd)) {\n const type = [ctx.type, ctx.templateType].filter(Boolean).join(\".\");\n\n const template = `generate/${type}.hbs`;\n\n return [\n {\n title: `Generate ${ctx.type} file to '${symbolPath}.ts'`,\n task: () =>\n this.srcRenderService.render(template, ctx, {\n output: `${symbolPath}.ts`\n })\n },\n {\n title: `Update bin/index`,\n skip() {\n return ctx.type !== \"command\";\n },\n task: () => {\n return this.srcRenderService.update(\"bin/index.ts\", [\n {\n type: \"import\",\n content: `import {${ctx.symbolName}} from \"./${basename(symbolPath)}\";`\n },\n {\n type: \"insert-after\",\n pattern: /commands: \\[/,\n content: ` ${ctx.symbolName}`\n }\n ]);\n }\n }\n ];\n }\n\n return [];\n }\n\n getDirectories(dir: string) {\n const directories = globby.sync(\"**/*\", {\n cwd: join(this.srcRenderService.rootDir, dir),\n ignore: [\"__*\"]\n });\n\n const set = new Set(\n directories.map((dir) => {\n return dirname(dir);\n })\n );\n\n set.delete(\".\");\n\n return [...set];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"GenerateCmd.js","sourceRoot":"","sources":["../../../../src/commands/generate/GenerateCmd.ts"],"names":[],"mappings":";;;;;AAAA,6CAA2H;AAC3H,6CAAkD;AAClD,+BAA6C;AAC7C,4DAA4B;AAC5B,4EAA2C;AAC3C,6DAAwD;AACxD,uEAAkE;AAClE,qDAAgD;AAChD,8EAAyE;AACzE,mDAA+C;AAC/C,0EAAqE;AACrE,yDAAoD;AAgBpD,MAAM,eAAe,GAAG;IACtB,EAAC,IAAI,EAAE,iBAAiB,EAAE,KAAK,EAAE,OAAO,EAAC;IACzC,EAAC,IAAI,EAAE,oCAAoC,EAAE,KAAK,EAAE,YAAY,EAAC;IACjE,EAAC,IAAI,EAAE,0BAA0B,EAAE,KAAK,EAAE,UAAU,EAAC;IACrD,EAAC,IAAI,EAAE,0BAA0B,EAAE,KAAK,EAAE,MAAM,EAAC;IACjD,EAAC,IAAI,EAAE,2BAA2B,EAAE,KAAK,EAAE,OAAO,EAAC;IACnD,EAAC,IAAI,EAAE,0BAA0B,EAAE,KAAK,EAAE,QAAQ,EAAC;IACnD,EAAC,IAAI,EAAE,4BAA4B,EAAE,KAAK,EAAE,UAAU,EAAC;IACvD,EAAC,IAAI,EAAE,6BAA6B,EAAE,KAAK,EAAE,WAAW,EAAC;IACzD,EAAC,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,SAAS,EAAC;CAC9C,CAAC;AAEF,MAAM,aAAa,GAAG,CAAC,IAAS,EAAE,EAAE;IAClC,OAAO,KAAK,EAAE,KAAU,EAAE,OAAe,EAAE,EAAE;QAC3C,IAAI,OAAO,EAAE;YACX,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,IAAS,EA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EF,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAEhB,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC;IAClB,CAAC;CACF,CAAA;AAzMC;IADC,IAAA,iBAAM,GAAE;sCACM,6BAAa;kDAAC;AAG7B;IADC,IAAA,iBAAM,GAAE;sCACW,uCAAkB;uDAAC;AAGvC;IADC,IAAA,iBAAM,GAAE;sCACE,qBAAS;8CAAC;AAGrB;IADC,IAAA,iBAAM,GAAE;sCACS,6BAAkB;qDAAC;AAGrC;IADC,IAAA,iBAAM,GAAE;sCACW,6BAAkB;uDAAC;AAd5B,WAAW;IAjCvB,IAAA,kBAAO,EAAC;QACP,IAAI,EAAE,UAAU;QAChB,KAAK,EAAE,GAAG;QACV,WAAW,EAAE,+BAA+B;QAC5C,IAAI,EAAE;YACJ,IAAI,EAAE;gBACJ,WAAW,EAAE,6DAA6D;gBAC1E,IAAI,EAAE,MAAM;aACb;YACD,IAAI,EAAE;gBACJ,WAAW,EAAE,mBAAmB;gBAChC,IAAI,EAAE,MAAM;aACb;SACF;QACD,OAAO,EAAE;YACP,qBAAqB,EAAE;gBACrB,IAAI,EAAE,MAAM;gBACZ,WAAW,EAAE,6CAA6C;aAC3D;YACD,6BAA6B,EAAE;gBAC7B,WAAW,EAAE,4CAA4C;gBACzD,IAAI,EAAE,MAAM;aACb;YACD,oCAAoC,EAAE;gBACpC,WAAW,EAAE,4CAA4C;gBACzD,IAAI,EAAE,MAAM;aACb;YACD,0CAA0C,EAAE;gBAC1C,WAAW,EAAE,qCAAqC;gBAClD,IAAI,EAAE,MAAM;aACb;SACF;KACF,CAAC;6CAiBmC,2CAAoB;GAhB5C,WAAW,CA2MvB;AA3MY,kCAAW","sourcesContent":["import {CliDefaultOptions, Command, CommandProvider, Inject, ProjectPackageJson, SrcRendererService} from \"@tsed/cli-core\";\nimport {paramCase, pascalCase} from \"change-case\";\nimport {basename, dirname, join} from \"path\";\nimport globby from \"globby\";\nimport normalizePath from \"normalize-path\";\nimport {ClassNamePipe} from \"../../pipes/ClassNamePipe\";\nimport {OutputFilePathPipe} from \"../../pipes/OutputFilePathPipe\";\nimport {RoutePipe} from \"../../pipes/RoutePipe\";\nimport {ProvidersInfoService} from \"../../services/ProvidersInfoService\";\nimport {PROVIDER_TYPES} from \"./ProviderTypes\";\nimport {ProjectConvention} from \"../../interfaces/ProjectConvention\";\nimport {fillImports} from \"../../utils/fillImports\";\n\nexport interface GenerateCmdContext extends CliDefaultOptions {\n type: string;\n name: string;\n route: string;\n directory: string;\n platform: string;\n templateType: string;\n middlewarePosition: \"before\" | \"after\";\n symbolName: string;\n symbolPath: string;\n symbolPathBasename: string;\n convention: ProjectConvention;\n}\n\nconst DECORATOR_TYPES = [\n {name: \"Class decorator\", value: \"class\"},\n {name: \"Ts.ED middleware and its decorator\", value: \"middleware\"},\n {name: \"Ts.ED endpoint decorator\", value: \"endpoint\"},\n {name: \"Ts.ED property decorator\", value: \"prop\"},\n {name: \"Ts.ED parameter decorator\", value: \"param\"},\n {name: \"Vanilla Method decorator\", value: \"method\"},\n {name: \"Vanilla Property decorator\", value: \"property\"},\n {name: \"Vanilla Parameter decorator\", value: \"parameter\"},\n {name: \"Generic decorator\", value: \"generic\"}\n];\n\nconst searchFactory = (list: any) => {\n return async (state: any, keyword: string) => {\n if (keyword) {\n return list.filter((item: any) => item.name.toLowerCase().includes(keyword.toLowerCase()));\n }\n\n return list;\n };\n};\n\n@Command({\n name: \"generate\",\n alias: \"g\",\n description: \"Generate a new provider class\",\n args: {\n type: {\n description: \"Type of the provider (Injectable, Controller, Pipe, etc...)\",\n type: String\n },\n name: {\n description: \"Name of the class\",\n type: String\n }\n },\n options: {\n \"-r, --route <route>\": {\n type: String,\n description: \"The route for the controller generated file\"\n },\n \"-d, --directory <directory>\": {\n description: \"Directory where the file must be generated\",\n type: String\n },\n \"-t, --template-type <templateType>\": {\n description: \"Directory where the file must be generated\",\n type: String\n },\n \"-m, --middleware-position <templateType>\": {\n description: \"Middleware position (before, after)\",\n type: String\n }\n }\n})\nexport class GenerateCmd implements CommandProvider {\n @Inject()\n classNamePipe: ClassNamePipe;\n\n @Inject()\n outputFilePathPipe: OutputFilePathPipe;\n\n @Inject()\n routePipe: RoutePipe;\n\n @Inject()\n srcRenderService: SrcRendererService;\n\n @Inject()\n projectPackageJson: ProjectPackageJson;\n\n constructor(private providersList: ProvidersInfoService) {\n PROVIDER_TYPES.forEach((info) => {\n this.providersList.add(\n {\n ...info\n },\n GenerateCmd\n );\n });\n }\n\n $prompt(initialOptions: Partial<GenerateCmdContext>) {\n const getName = (state: any) =>\n initialOptions.name || pascalCase(state.name || initialOptions.name || state.type || initialOptions.type);\n\n const proposedProviders = this.providersList.findProviders(initialOptions.type);\n\n return [\n {\n type: \"autocomplete\",\n name: \"type\",\n message: \"Which type of provider?\",\n default: initialOptions.type,\n when: () => proposedProviders.length > 1,\n source: searchFactory(proposedProviders)\n },\n {\n type: \"input\",\n name: \"name\",\n message: \"Which name?\",\n default: getName,\n when: !initialOptions.name\n },\n {\n message: \"Which platform?\",\n type: \"list\",\n name: \"platform\",\n when(state: any) {\n return [\"server\"].includes(state.type || initialOptions.type);\n },\n choices: [\n {\n name: \"Express.js\",\n checked: true,\n value: \"express\"\n },\n {\n name: \"Koa.js\",\n checked: false,\n value: \"koa\"\n }\n ]\n },\n {\n type: \"input\",\n name: \"route\",\n message: \"Which route?\",\n when(state: any) {\n return !!([\"controller\", \"server\"].includes(state.type || initialOptions.type) || initialOptions.route);\n },\n default: (state: GenerateCmdContext) => {\n return state.type === \"server\" ? \"/rest\" : this.routePipe.transform(getName(state));\n }\n },\n {\n type: \"list\",\n name: \"directory\",\n message: \"Which directory?\",\n when(state: any) {\n return [\"controller\"].includes(state.type || initialOptions.type) || initialOptions.directory;\n },\n choices: this.getDirectories(\"controllers\")\n },\n {\n type: \"autocomplete\",\n name: \"templateType\",\n message: (state: any) => `Which type of ${state.type || initialOptions.type}?`,\n when(state: any) {\n return !!([\"decorator\"].includes(state.type || initialOptions.type) || initialOptions.templateType);\n },\n source: searchFactory(DECORATOR_TYPES)\n },\n {\n type: \"list\",\n name: \"middlewarePosition\",\n message: () => `The middleware should be called:`,\n choices: [\n {name: \"Before the endpoint\", value: \"before\"},\n {name: \"After the endpoint\", value: \"after\"}\n ],\n when(state: any) {\n return !!(\n ([\"decorator\"].includes(state.type || initialOptions.type) && [\"middleware\"].includes(state.templateType)) ||\n initialOptions.middlewarePosition\n );\n }\n }\n ];\n }\n\n $mapContext(ctx: Partial<GenerateCmdContext>): GenerateCmdContext {\n const {name = \"\"} = ctx;\n let {type = \"\"} = ctx;\n type = type.toLowerCase();\n\n if (ctx.name === \"prisma\" && ctx.name) {\n type = \"prisma.service\";\n }\n\n const symbolName = this.classNamePipe.transform({name, type, format: ProjectConvention.DEFAULT});\n const symbolParamName = paramCase(symbolName);\n\n return fillImports({\n ...ctx,\n type,\n route: ctx.route ? this.routePipe.transform(ctx.route) : \"\",\n symbolName,\n symbolParamName,\n symbolPath: normalizePath(\n this.outputFilePathPipe.transform({\n name,\n type,\n subDir: ctx.directory\n })\n ),\n symbolPathBasename: normalizePath(this.classNamePipe.transform({name, type})),\n platformSymbol: ctx.platform && pascalCase(`Platform ${ctx.platform}`)\n }) as GenerateCmdContext;\n }\n\n async $exec(ctx: GenerateCmdContext) {\n const {symbolPath} = ctx;\n\n if (this.providersList.isMyProvider(ctx.type, GenerateCmd)) {\n const type = [ctx.type, ctx.templateType].filter(Boolean).join(\".\");\n\n const template = `generate/${type}.hbs`;\n\n return [\n {\n title: `Generate ${ctx.type} file to '${symbolPath}.ts'`,\n task: () =>\n this.srcRenderService.render(template, ctx, {\n output: `${symbolPath}.ts`\n })\n },\n {\n title: `Update bin/index`,\n skip() {\n return ctx.type !== \"command\";\n },\n task: () => {\n return this.srcRenderService.update(\"bin/index.ts\", [\n {\n type: \"import\",\n content: `import {${ctx.symbolName}} from \"./${basename(symbolPath)}\";`\n },\n {\n type: \"insert-after\",\n pattern: /commands: \\[/,\n content: ` ${ctx.symbolName}`\n }\n ]);\n }\n }\n ];\n }\n\n return [];\n }\n\n getDirectories(dir: string) {\n const directories = globby.sync(\"**/*\", {\n cwd: join(this.srcRenderService.rootDir, dir),\n ignore: [\"__*\"]\n });\n\n const set = new Set(\n directories.map((dir) => {\n return dirname(dir);\n })\n );\n\n set.delete(\".\");\n\n return [...set];\n }\n}\n"]}
|
|
@@ -90,7 +90,7 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
90
90
|
name: "route",
|
|
91
91
|
message: "Which route?",
|
|
92
92
|
when(state) {
|
|
93
|
-
return ["controller", "server"].includes(state.type || initialOptions.type);
|
|
93
|
+
return !!(["controller", "server"].includes(state.type || initialOptions.type) || initialOptions.route);
|
|
94
94
|
},
|
|
95
95
|
default: (state) => {
|
|
96
96
|
return state.type === "server" ? "/rest" : this.routePipe.transform(getName(state));
|
|
@@ -101,7 +101,7 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
101
101
|
name: "directory",
|
|
102
102
|
message: "Which directory?",
|
|
103
103
|
when(state) {
|
|
104
|
-
return ["controller"].includes(state.type || initialOptions.type);
|
|
104
|
+
return ["controller"].includes(state.type || initialOptions.type) || initialOptions.directory;
|
|
105
105
|
},
|
|
106
106
|
choices: this.getDirectories("controllers")
|
|
107
107
|
},
|
|
@@ -110,7 +110,7 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
110
110
|
name: "templateType",
|
|
111
111
|
message: (state) => `Which type of ${state.type || initialOptions.type}?`,
|
|
112
112
|
when(state) {
|
|
113
|
-
return ["decorator"].includes(state.type || initialOptions.type);
|
|
113
|
+
return !!(["decorator"].includes(state.type || initialOptions.type) || initialOptions.templateType);
|
|
114
114
|
},
|
|
115
115
|
source: searchFactory(DECORATOR_TYPES)
|
|
116
116
|
},
|
|
@@ -123,7 +123,8 @@ let GenerateCmd = GenerateCmd_1 = class GenerateCmd {
|
|
|
123
123
|
{ name: "After the endpoint", value: "after" }
|
|
124
124
|
],
|
|
125
125
|
when(state) {
|
|
126
|
-
return ["decorator"].includes(state.type || initialOptions.type) && ["middleware"].includes(state.templateType)
|
|
126
|
+
return !!((["decorator"].includes(state.type || initialOptions.type) && ["middleware"].includes(state.templateType)) ||
|
|
127
|
+
initialOptions.middlewarePosition);
|
|
127
128
|
}
|
|
128
129
|
}
|
|
129
130
|
];
|
|
@@ -233,6 +234,24 @@ GenerateCmd = GenerateCmd_1 = __decorate([
|
|
|
233
234
|
description: "Name of the class",
|
|
234
235
|
type: String
|
|
235
236
|
}
|
|
237
|
+
},
|
|
238
|
+
options: {
|
|
239
|
+
"-r, --route <route>": {
|
|
240
|
+
type: String,
|
|
241
|
+
description: "The route for the controller generated file"
|
|
242
|
+
},
|
|
243
|
+
"-d, --directory <directory>": {
|
|
244
|
+
description: "Directory where the file must be generated",
|
|
245
|
+
type: String
|
|
246
|
+
},
|
|
247
|
+
"-t, --template-type <templateType>": {
|
|
248
|
+
description: "Directory where the file must be generated",
|
|
249
|
+
type: String
|
|
250
|
+
},
|
|
251
|
+
"-m, --middleware-position <templateType>": {
|
|
252
|
+
description: "Middleware position (before, after)",
|
|
253
|
+
type: String
|
|
254
|
+
}
|
|
236
255
|
}
|
|
237
256
|
}),
|
|
238
257
|
__metadata("design:paramtypes", [ProvidersInfoService])
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenerateCmd.js","sourceRoot":"","sources":["../../../../src/commands/generate/GenerateCmd.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAoB,OAAO,EAAmB,MAAM,EAAE,kBAAkB,EAAE,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AAC3H,OAAO,EAAC,SAAS,EAAE,UAAU,EAAC,MAAM,aAAa,CAAC;AAClD,OAAO,EAAC,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAC,MAAM,MAAM,CAAC;AAC7C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,aAAa,MA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kBAAkB;uDAAC;AAd5B,WAAW;IAfvB,OAAO,CAAC;QACP,IAAI,EAAE,UAAU;QAChB,KAAK,EAAE,GAAG;QACV,WAAW,EAAE,+BAA+B;QAC5C,IAAI,EAAE;YACJ,IAAI,EAAE;gBACJ,WAAW,EAAE,6DAA6D;gBAC1E,IAAI,EAAE,MAAM;aACb;YACD,IAAI,EAAE;gBACJ,WAAW,EAAE,mBAAmB;gBAChC,IAAI,EAAE,MAAM;aACb;SACF;KACF,CAAC;qCAiBmC,oBAAoB;GAhB5C,WAAW,CAwMvB;SAxMY,WAAW","sourcesContent":["import {CliDefaultOptions, Command, CommandProvider, Inject, ProjectPackageJson, SrcRendererService} from \"@tsed/cli-core\";\nimport {paramCase, pascalCase} from \"change-case\";\nimport {basename, dirname, join} from \"path\";\nimport globby from \"globby\";\nimport normalizePath from \"normalize-path\";\nimport {ClassNamePipe} from \"../../pipes/ClassNamePipe\";\nimport {OutputFilePathPipe} from \"../../pipes/OutputFilePathPipe\";\nimport {RoutePipe} from \"../../pipes/RoutePipe\";\nimport {ProvidersInfoService} from \"../../services/ProvidersInfoService\";\nimport {PROVIDER_TYPES} from \"./ProviderTypes\";\nimport {ProjectConvention} from \"../../interfaces/ProjectConvention\";\nimport {fillImports} from \"../../utils/fillImports\";\n\nexport interface GenerateCmdContext extends CliDefaultOptions {\n type: string;\n name: string;\n route: string;\n directory: string;\n platform: string;\n templateType: string;\n symbolName: string;\n symbolPath: string;\n symbolPathBasename: string;\n convention: ProjectConvention;\n}\n\nconst DECORATOR_TYPES = [\n {name: \"Class decorator\", value: \"class\"},\n {name: \"Ts.ED middleware and its decorator\", value: \"middleware\"},\n {name: \"Ts.ED endpoint decorator\", value: \"endpoint\"},\n {name: \"Ts.ED property decorator\", value: \"prop\"},\n {name: \"Ts.ED parameter decorator\", value: \"param\"},\n {name: \"Vanilla Method decorator\", value: \"method\"},\n {name: \"Vanilla Property decorator\", value: \"property\"},\n {name: \"Vanilla Parameter decorator\", value: \"parameter\"},\n {name: \"Generic decorator\", value: \"generic\"}\n];\n\nconst searchFactory = (list: any) => {\n return async (state: any, keyword: string) => {\n if (keyword) {\n return list.filter((item: any) => item.name.toLowerCase().includes(keyword.toLowerCase()));\n }\n\n return list;\n };\n};\n\n@Command({\n name: \"generate\",\n alias: \"g\",\n description: \"Generate a new provider class\",\n args: {\n type: {\n description: \"Type of the provider (Injectable, Controller, Pipe, etc...)\",\n type: String\n },\n name: {\n description: \"Name of the class\",\n type: String\n }\n }\n})\nexport class GenerateCmd implements CommandProvider {\n @Inject()\n classNamePipe: ClassNamePipe;\n\n @Inject()\n outputFilePathPipe: OutputFilePathPipe;\n\n @Inject()\n routePipe: RoutePipe;\n\n @Inject()\n srcRenderService: SrcRendererService;\n\n @Inject()\n projectPackageJson: ProjectPackageJson;\n\n constructor(private providersList: ProvidersInfoService) {\n PROVIDER_TYPES.forEach((info) => {\n this.providersList.add(\n {\n ...info\n },\n GenerateCmd\n );\n });\n }\n\n $prompt(initialOptions: Partial<GenerateCmdContext>) {\n const getName = (state: any) =>\n initialOptions.name || pascalCase(state.name || initialOptions.name || state.type || initialOptions.type);\n\n const proposedProviders = this.providersList.findProviders(initialOptions.type);\n\n return [\n {\n type: \"autocomplete\",\n name: \"type\",\n message: \"Which type of provider?\",\n default: initialOptions.type,\n when: () => proposedProviders.length > 1,\n source: searchFactory(proposedProviders)\n },\n {\n type: \"input\",\n name: \"name\",\n message: \"Which name?\",\n default: getName,\n when: !initialOptions.name\n },\n {\n message: \"Which platform?\",\n type: \"list\",\n name: \"platform\",\n when(state: any) {\n return [\"server\"].includes(state.type || initialOptions.type);\n },\n choices: [\n {\n name: \"Express.js\",\n checked: true,\n value: \"express\"\n },\n {\n name: \"Koa.js\",\n checked: false,\n value: \"koa\"\n }\n ]\n },\n {\n type: \"input\",\n name: \"route\",\n message: \"Which route?\",\n when(state: any) {\n return [\"controller\", \"server\"].includes(state.type || initialOptions.type);\n },\n default: (state: GenerateCmdContext) => {\n return state.type === \"server\" ? \"/rest\" : this.routePipe.transform(getName(state));\n }\n },\n {\n type: \"list\",\n name: \"directory\",\n message: \"Which directory?\",\n when(state: any) {\n return [\"controller\"].includes(state.type || initialOptions.type);\n },\n choices: this.getDirectories(\"controllers\")\n },\n {\n type: \"autocomplete\",\n name: \"templateType\",\n message: (state: any) => `Which type of ${state.type || initialOptions.type}?`,\n when(state: any) {\n return [\"decorator\"].includes(state.type || initialOptions.type);\n },\n source: searchFactory(DECORATOR_TYPES)\n },\n {\n type: \"list\",\n name: \"middlewarePosition\",\n message: () => `The middleware should be called:`,\n choices: [\n {name: \"Before the endpoint\", value: \"before\"},\n {name: \"After the endpoint\", value: \"after\"}\n ],\n when(state: any) {\n return [\"decorator\"].includes(state.type || initialOptions.type) && [\"middleware\"].includes(state.templateType);\n }\n }\n ];\n }\n\n $mapContext(ctx: Partial<GenerateCmdContext>): GenerateCmdContext {\n const {name = \"\"} = ctx;\n let {type = \"\"} = ctx;\n type = type.toLowerCase();\n\n if (ctx.name === \"prisma\" && ctx.name) {\n type = \"prisma.service\";\n }\n\n const symbolName = this.classNamePipe.transform({name, type, format: ProjectConvention.DEFAULT});\n const symbolParamName = paramCase(symbolName);\n\n return fillImports({\n ...ctx,\n type,\n route: ctx.route ? this.routePipe.transform(ctx.route) : \"\",\n symbolName,\n symbolParamName,\n symbolPath: normalizePath(\n this.outputFilePathPipe.transform({\n name,\n type,\n subDir: ctx.directory\n })\n ),\n symbolPathBasename: normalizePath(this.classNamePipe.transform({name, type})),\n platformSymbol: ctx.platform && pascalCase(`Platform ${ctx.platform}`)\n }) as GenerateCmdContext;\n }\n\n async $exec(ctx: GenerateCmdContext) {\n const {symbolPath} = ctx;\n\n if (this.providersList.isMyProvider(ctx.type, GenerateCmd)) {\n const type = [ctx.type, ctx.templateType].filter(Boolean).join(\".\");\n\n const template = `generate/${type}.hbs`;\n\n return [\n {\n title: `Generate ${ctx.type} file to '${symbolPath}.ts'`,\n task: () =>\n this.srcRenderService.render(template, ctx, {\n output: `${symbolPath}.ts`\n })\n },\n {\n title: `Update bin/index`,\n skip() {\n return ctx.type !== \"command\";\n },\n task: () => {\n return this.srcRenderService.update(\"bin/index.ts\", [\n {\n type: \"import\",\n content: `import {${ctx.symbolName}} from \"./${basename(symbolPath)}\";`\n },\n {\n type: \"insert-after\",\n pattern: /commands: \\[/,\n content: ` ${ctx.symbolName}`\n }\n ]);\n }\n }\n ];\n }\n\n return [];\n }\n\n getDirectories(dir: string) {\n const directories = globby.sync(\"**/*\", {\n cwd: join(this.srcRenderService.rootDir, dir),\n ignore: [\"__*\"]\n });\n\n const set = new Set(\n directories.map((dir) => {\n return dirname(dir);\n })\n );\n\n set.delete(\".\");\n\n return [...set];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"GenerateCmd.js","sourceRoot":"","sources":["../../../../src/commands/generate/GenerateCmd.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAoB,OAAO,EAAmB,MAAM,EAAE,kBAAkB,EAAE,kBAAkB,EAAC,MAAM,gBAAgB,CAAC;AAC3H,OAAO,EAAC,SAAS,EAAE,UAAU,EAAC,MAAM,aAAa,CAAC;AAClD,OAAO,EAAC,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAC,MAAM,MAAM,CAAC;AAC7C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,aAAa,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAC,kBAAkB,EAAC,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAC,SAAS,EAAC,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAC,oBAAoB,EAAC,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAC,cAAc,EAAC,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAC,iBAAiB,EAAC,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAC,WAAW,EAAC,MAAM,yBAAyB,CAAC;AAgBpD,MAAM,e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M;aACb;YACD,IAAI,EAAE;gBACJ,WAAW,EAAE,mBAAmB;gBAChC,IAAI,EAAE,MAAM;aACb;SACF;QACD,OAAO,EAAE;YACP,qBAAqB,EAAE;gBACrB,IAAI,EAAE,MAAM;gBACZ,WAAW,EAAE,6CAA6C;aAC3D;YACD,6BAA6B,EAAE;gBAC7B,WAAW,EAAE,4CAA4C;gBACzD,IAAI,EAAE,MAAM;aACb;YACD,oCAAoC,EAAE;gBACpC,WAAW,EAAE,4CAA4C;gBACzD,IAAI,EAAE,MAAM;aACb;YACD,0CAA0C,EAAE;gBAC1C,WAAW,EAAE,qCAAqC;gBAClD,IAAI,EAAE,MAAM;aACb;SACF;KACF,CAAC;qCAiBmC,oBAAoB;GAhB5C,WAAW,CA2MvB;SA3MY,WAAW","sourcesContent":["import {CliDefaultOptions, Command, CommandProvider, Inject, ProjectPackageJson, SrcRendererService} from \"@tsed/cli-core\";\nimport {paramCase, pascalCase} from \"change-case\";\nimport {basename, dirname, join} from \"path\";\nimport globby from \"globby\";\nimport normalizePath from \"normalize-path\";\nimport {ClassNamePipe} from \"../../pipes/ClassNamePipe\";\nimport {OutputFilePathPipe} from \"../../pipes/OutputFilePathPipe\";\nimport {RoutePipe} from \"../../pipes/RoutePipe\";\nimport {ProvidersInfoService} from \"../../services/ProvidersInfoService\";\nimport {PROVIDER_TYPES} from \"./ProviderTypes\";\nimport {ProjectConvention} from \"../../interfaces/ProjectConvention\";\nimport {fillImports} from \"../../utils/fillImports\";\n\nexport interface GenerateCmdContext extends CliDefaultOptions {\n type: string;\n name: string;\n route: string;\n directory: string;\n platform: string;\n templateType: string;\n middlewarePosition: \"before\" | \"after\";\n symbolName: string;\n symbolPath: string;\n symbolPathBasename: string;\n convention: ProjectConvention;\n}\n\nconst DECORATOR_TYPES = [\n {name: \"Class decorator\", value: \"class\"},\n {name: \"Ts.ED middleware and its decorator\", value: \"middleware\"},\n {name: \"Ts.ED endpoint decorator\", value: \"endpoint\"},\n {name: \"Ts.ED property decorator\", value: \"prop\"},\n {name: \"Ts.ED parameter decorator\", value: \"param\"},\n {name: \"Vanilla Method decorator\", value: \"method\"},\n {name: \"Vanilla Property decorator\", value: \"property\"},\n {name: \"Vanilla Parameter decorator\", value: \"parameter\"},\n {name: \"Generic decorator\", value: \"generic\"}\n];\n\nconst searchFactory = (list: any) => {\n return async (state: any, keyword: string) => {\n if (keyword) {\n return list.filter((item: any) => item.name.toLowerCase().includes(keyword.toLowerCase()));\n }\n\n return list;\n };\n};\n\n@Command({\n name: \"generate\",\n alias: \"g\",\n description: \"Generate a new provider class\",\n args: {\n type: {\n description: \"Type of the provider (Injectable, Controller, Pipe, etc...)\",\n type: String\n },\n name: {\n description: \"Name of the class\",\n type: String\n }\n },\n options: {\n \"-r, --route <route>\": {\n type: String,\n description: \"The route for the controller generated file\"\n },\n \"-d, --directory <directory>\": {\n description: \"Directory where the file must be generated\",\n type: String\n },\n \"-t, --template-type <templateType>\": {\n description: \"Directory where the file must be generated\",\n type: String\n },\n \"-m, --middleware-position <templateType>\": {\n description: \"Middleware position (before, after)\",\n type: String\n }\n }\n})\nexport class GenerateCmd implements CommandProvider {\n @Inject()\n classNamePipe: ClassNamePipe;\n\n @Inject()\n outputFilePathPipe: OutputFilePathPipe;\n\n @Inject()\n routePipe: RoutePipe;\n\n @Inject()\n srcRenderService: SrcRendererService;\n\n @Inject()\n projectPackageJson: ProjectPackageJson;\n\n constructor(private providersList: ProvidersInfoService) {\n PROVIDER_TYPES.forEach((info) => {\n this.providersList.add(\n {\n ...info\n },\n GenerateCmd\n );\n });\n }\n\n $prompt(initialOptions: Partial<GenerateCmdContext>) {\n const getName = (state: any) =>\n initialOptions.name || pascalCase(state.name || initialOptions.name || state.type || initialOptions.type);\n\n const proposedProviders = this.providersList.findProviders(initialOptions.type);\n\n return [\n {\n type: \"autocomplete\",\n name: \"type\",\n message: \"Which type of provider?\",\n default: initialOptions.type,\n when: () => proposedProviders.length > 1,\n source: searchFactory(proposedProviders)\n },\n {\n type: \"input\",\n name: \"name\",\n message: \"Which name?\",\n default: getName,\n when: !initialOptions.name\n },\n {\n message: \"Which platform?\",\n type: \"list\",\n name: \"platform\",\n when(state: any) {\n return [\"server\"].includes(state.type || initialOptions.type);\n },\n choices: [\n {\n name: \"Express.js\",\n checked: true,\n value: \"express\"\n },\n {\n name: \"Koa.js\",\n checked: false,\n value: \"koa\"\n }\n ]\n },\n {\n type: \"input\",\n name: \"route\",\n message: \"Which route?\",\n when(state: any) {\n return !!([\"controller\", \"server\"].includes(state.type || initialOptions.type) || initialOptions.route);\n },\n default: (state: GenerateCmdContext) => {\n return state.type === \"server\" ? \"/rest\" : this.routePipe.transform(getName(state));\n }\n },\n {\n type: \"list\",\n name: \"directory\",\n message: \"Which directory?\",\n when(state: any) {\n return [\"controller\"].includes(state.type || initialOptions.type) || initialOptions.directory;\n },\n choices: this.getDirectories(\"controllers\")\n },\n {\n type: \"autocomplete\",\n name: \"templateType\",\n message: (state: any) => `Which type of ${state.type || initialOptions.type}?`,\n when(state: any) {\n return !!([\"decorator\"].includes(state.type || initialOptions.type) || initialOptions.templateType);\n },\n source: searchFactory(DECORATOR_TYPES)\n },\n {\n type: \"list\",\n name: \"middlewarePosition\",\n message: () => `The middleware should be called:`,\n choices: [\n {name: \"Before the endpoint\", value: \"before\"},\n {name: \"After the endpoint\", value: \"after\"}\n ],\n when(state: any) {\n return !!(\n ([\"decorator\"].includes(state.type || initialOptions.type) && [\"middleware\"].includes(state.templateType)) ||\n initialOptions.middlewarePosition\n );\n }\n }\n ];\n }\n\n $mapContext(ctx: Partial<GenerateCmdContext>): GenerateCmdContext {\n const {name = \"\"} = ctx;\n let {type = \"\"} = ctx;\n type = type.toLowerCase();\n\n if (ctx.name === \"prisma\" && ctx.name) {\n type = \"prisma.service\";\n }\n\n const symbolName = this.classNamePipe.transform({name, type, format: ProjectConvention.DEFAULT});\n const symbolParamName = paramCase(symbolName);\n\n return fillImports({\n ...ctx,\n type,\n route: ctx.route ? this.routePipe.transform(ctx.route) : \"\",\n symbolName,\n symbolParamName,\n symbolPath: normalizePath(\n this.outputFilePathPipe.transform({\n name,\n type,\n subDir: ctx.directory\n })\n ),\n symbolPathBasename: normalizePath(this.classNamePipe.transform({name, type})),\n platformSymbol: ctx.platform && pascalCase(`Platform ${ctx.platform}`)\n }) as GenerateCmdContext;\n }\n\n async $exec(ctx: GenerateCmdContext) {\n const {symbolPath} = ctx;\n\n if (this.providersList.isMyProvider(ctx.type, GenerateCmd)) {\n const type = [ctx.type, ctx.templateType].filter(Boolean).join(\".\");\n\n const template = `generate/${type}.hbs`;\n\n return [\n {\n title: `Generate ${ctx.type} file to '${symbolPath}.ts'`,\n task: () =>\n this.srcRenderService.render(template, ctx, {\n output: `${symbolPath}.ts`\n })\n },\n {\n title: `Update bin/index`,\n skip() {\n return ctx.type !== \"command\";\n },\n task: () => {\n return this.srcRenderService.update(\"bin/index.ts\", [\n {\n type: \"import\",\n content: `import {${ctx.symbolName}} from \"./${basename(symbolPath)}\";`\n },\n {\n type: \"insert-after\",\n pattern: /commands: \\[/,\n content: ` ${ctx.symbolName}`\n }\n ]);\n }\n }\n ];\n }\n\n return [];\n }\n\n getDirectories(dir: string) {\n const directories = globby.sync(\"**/*\", {\n cwd: join(this.srcRenderService.rootDir, dir),\n ignore: [\"__*\"]\n });\n\n const set = new Set(\n directories.map((dir) => {\n return dirname(dir);\n })\n );\n\n set.delete(\".\");\n\n return [...set];\n }\n}\n"]}
|
|
@@ -11,6 +11,7 @@ export interface GenerateCmdContext extends CliDefaultOptions {
|
|
|
11
11
|
directory: string;
|
|
12
12
|
platform: string;
|
|
13
13
|
templateType: string;
|
|
14
|
+
middlewarePosition: "before" | "after";
|
|
14
15
|
symbolName: string;
|
|
15
16
|
symbolPath: string;
|
|
16
17
|
symbolPathBasename: string;
|
|
@@ -64,7 +65,7 @@ export declare class GenerateCmd implements CommandProvider {
|
|
|
64
65
|
type: string;
|
|
65
66
|
name: string;
|
|
66
67
|
message: string;
|
|
67
|
-
when(state: any):
|
|
68
|
+
when(state: any): string | true | undefined;
|
|
68
69
|
choices: string[];
|
|
69
70
|
default?: undefined;
|
|
70
71
|
source?: undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/cli",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/cjs/index.js",
|
|
6
6
|
"module": "./lib/esm/index.js",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"node": ">=14"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@tsed/cli-core": "4.0
|
|
62
|
+
"@tsed/cli-core": "4.2.0",
|
|
63
63
|
"@tsed/core": ">=7.0.0",
|
|
64
64
|
"@tsed/di": ">=7.0.0",
|
|
65
65
|
"@tsed/logger": ">=6.2.1",
|