UNPKG

@tsed/cli

Version:
91 lines (90 loc) 3.17 kB
import { command, inject, ProjectPackageJson } from "@tsed/cli-core"; import { CliProjectService } from "../../services/CliProjectService.js"; import { CliTemplatesService } from "../../services/CliTemplatesService.js"; import { addContextMethods } from "../../services/mappers/addContextMethods.js"; import { mapDefaultTemplateOptions } from "../../services/mappers/mapDefaultTemplateOptions.js"; const mapChoices = (list) => { return list.map((item) => ({ label: item.label, value: item.id })); }; export class GenerateCmd { constructor() { this.projectPackageJson = inject(ProjectPackageJson); this.projectService = inject(CliProjectService); this.templates = inject(CliTemplatesService); } async $prompt(data) { data = addContextMethods(data); const templates = this.templates.find(); const templatesPrompts = await Promise.all(templates.filter((template) => template.prompts).map((template) => template.prompts(data))); const additionalPrompts = templatesPrompts.flat(); return [ { type: "autocomplete", name: "type", message: "Which template you want to use?", default: data.type, when: () => templates.length > 1, choices: mapChoices(this.templates.find(data.type)) }, { type: "input", name: "name", message: "Which name?", default: data.getName, when: !data.name }, ...additionalPrompts ]; } $mapContext(ctx) { return mapDefaultTemplateOptions(ctx); } $exec(ctx) { const { symbolPath, type } = ctx; return [ { title: `Generate ${ctx.type} file to '${symbolPath}.ts'`, skip: !this.templates.get(type), task: () => this.projectService.createFromTemplate(type, ctx) }, { title: "Transform generated files", task: () => this.projectService.transformFiles(ctx) } ]; } } command({ token: GenerateCmd, name: "generate", alias: "g", description: "Generate a new provider class", args: { type: { description: "Type of the provider (Injectable, Controller, Pipe, etc...)", type: String }, name: { description: "Name of the class", type: String } }, options: { "--route <route>": { type: String, description: "The route for the controller generated file" }, "-d, --directory <directory>": { description: "Directory where the file must be generated", type: String }, "-t, --template-type <templateType>": { description: "Directory where the file must be generated", type: String }, "-m, --middleware-position <templateType>": { description: "Middleware position (before, after)", type: String } } });