Yeoman
Introduction
Yeoman is a generic scaffolding system allowing the creation of any kind of app. It allows for rapidly getting started on new projects and streamlines the maintenance of existing projects.
Templates
Create template
yo generator
Customize the template and declare the output format
controller.txt
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
} from "@nestjs/common";
import { ApiBearerAuth } from "@nestjs/swagger";
import { PageQueryDto } from "@/common/page-query.dto";
import { ApiPageResponse } from "@/common/page-response.decorator";
import { <%= entityUpper %> } from "@/<%= entityLower %>/entities/<%= entityLower %>.entity";
import { Create<%= entityUpper %>Dto } from "./dto/create-<%= entityLower %>.dto";
import { Update<%= entityUpper %>Dto } from "./dto/update-<%= entityLower %>.dto";
import { <%= entityUpper %>Service } from "./<%= entityLower %>.service";
@Controller("<%= entityPlural %>")
@ApiBearerAuth("JWT")
export class <%= entityUpper %>Controller {
constructor(private readonly <%= entityLower %>Service: <%= entityUpper %>Service) {}
@Post()
create(@Body() create<%= entityUpper %>Dto: Create<%= entityUpper %>Dto): Promise<<%= entityUpper %> | null> {
return this.<%= entityLower %>Service.create(create<%= entityUpper %>Dto);
}
@Get()
@ApiPageResponse(<%= entityUpper %>)
findMany(@Query() pageQueryDto: PageQueryDto) {
return this.<%= entityLower %>Service.findAndCount({}, pageQueryDto);
}
@Get(":id")
findOne(@Param("id") id: string): Promise<<%= entityUpper %> | null> {
return this.<%= entityLower %>Service.findById(id);
}
@Patch(":id")
update(@Param("id") id: string, @Body() update<%= entityUpper %>Dto: Update<%= entityUpper %>Dto) {
return this.<%= entityLower %>Service.update(id, update<%= entityUpper %>Dto);
}
@Delete(":id")
remove(@Param("id") id: string) {
return this.<%= entityLower %>Service.delete(id);
}
}
"use strict";
const Generator = require("yeoman-generator");
const chalk = require("chalk");
const yosay = require("yosay");
const pluralize = require("pluralize");
module.exports = class extends Generator {
prompting() {
// Have Yeoman greet the user.
this.log(
yosay(
`Welcome to the extraordinary ${chalk.red(
"generator-famchest-api-resource",
)} generator!`,
),
);
this.argument("entity", { type: String, required: false });
if (this.options.entity) {
this.props = {
entity: this.options.entity,
};
return;
}
const prompts = [
{
type: "input",
name: "entity",
message: "What is the entity name?",
},
];
return this.prompt(prompts).then((props) => {
// To access props later use this.props.someAnswer;
this.props = props;
});
}
writing() {
// declare the input and output file
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
const lowerCase = (str) => str.toLowerCase();
const entityLower = lowerCase(this.props.entity);
const entityUpper = capitalize(this.props.entity);
const destinations = [
{
from: "controller.txt",
to: `src/${entityLower}/${entityLower}.controller.ts`,
},
];
const entityPlural = pluralize.plural(entityLower);
destinations.forEach((destination) => {
this.fs.copyTpl(
this.templatePath(destination.from),
this.destinationPath(destination.to),
{ entityUpper, entityLower, entityPlural },
);
});
}
};
Generate the file
cd generator-peter-cheng-testing \
npm i \
npm link \
cd .. \
yo peter-cheng-testing
Reference
Last updated
Was this helpful?