Skip to content

Commit

Permalink
✨ feat: ver 0.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
gylove1994 committed Sep 30, 2024
1 parent 2c76385 commit 3c6bf9a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gylove1994/npg",
"version": "0.2.4",
"version": "0.2.5",
"description": "A command-line tool for generating NestJS code form schema.prisma file.",
"main": "dist/index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/generateEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export function generateEnumFile(
prisma: Schema,
outputPath: string,
dryRun: boolean,
silent = false,
) {
const enumList = prisma.list.filter((item) => item.type === "enum");
for (const e of enumList) {
const { name, content } = generateEnum(e);
mkFile(outputPath, name, content, dryRun);
mkFile(outputPath, name, content, dryRun, silent);
}
}
43 changes: 37 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ program
generateEnumFile(prisma, answers.outputPath, answers.dryRun);
generateEntityFile(prisma, answers.outputPath, answers.dryRun);
} else if (answers.type === "nestModule") {
generateEnumFile(prisma, answers.outputPath, true, true);
generateNestModuleFile(prisma, answers.outputPath, answers.dryRun);
generateNestControllerFile(prisma, answers.outputPath, answers.dryRun);
generateNestDtoFile(prisma, answers.outputPath, answers.dryRun);
Expand All @@ -75,7 +76,6 @@ program
} else if (answers.type === "all") {
generateEnumFile(prisma, answers.outputPath, answers.dryRun);
generateEntityFile(prisma, answers.outputPath, answers.dryRun);
generatePickEntityFile(prisma, answers.outputPath, answers.dryRun);
generateNestModuleFile(prisma, answers.outputPath, answers.dryRun);
generateNestControllerFile(prisma, answers.outputPath, answers.dryRun);
generateNestDtoFile(prisma, answers.outputPath, answers.dryRun);
Expand All @@ -85,10 +85,41 @@ program
console.log(`${chalk.green("SUCCESS")} ${mkFileCount} files created 🔥`);
});

// program
// .command("generate")
// .alias("g")
// .description("generate code")
// .addArgument(new Argument("type", "The type of the code to generate"));
program
.command("generate")
.alias("g")
.description("generate code")
.argument("type", "The type of the code to generate")
.argument("prismaPath", "The path to the prisma schema file")
.argument("outputPath", "The path to the output directory")
.argument("dryRun", "Whether to run in dry run mode")
.action(async (type, prismaPath, outputPath, dryRun) => {
const schemaFile = await fs.readFile(prismaPath, "utf-8");
const prisma = getSchema(schemaFile);
if (type === "entity-with-pick") {
generateEnumFile(prisma, outputPath, dryRun);
generateEntityFile(prisma, outputPath, dryRun);
generatePickEntityFile(prisma, outputPath, dryRun);
} else if (type === "entity") {
generateEnumFile(prisma, outputPath, dryRun);
generateEntityFile(prisma, outputPath, dryRun);
} else if (type === "nestModule") {
generateNestModuleFile(prisma, outputPath, dryRun);
generateNestControllerFile(prisma, outputPath, dryRun);
generateNestDtoFile(prisma, outputPath, dryRun);
generateNestTypesFile(prisma, outputPath, dryRun);
generateServiceFile(prisma, outputPath, dryRun);
} else if (type === "all") {
generateEnumFile(prisma, outputPath, dryRun);
generateEntityFile(prisma, outputPath, dryRun);
generatePickEntityFile(prisma, outputPath, dryRun);
generateNestModuleFile(prisma, outputPath, dryRun);
generateNestControllerFile(prisma, outputPath, dryRun);
generateNestDtoFile(prisma, outputPath, dryRun);
generateNestTypesFile(prisma, outputPath, dryRun);
generateServiceFile(prisma, outputPath, dryRun);
}
console.log(`${chalk.green("SUCCESS")} ${mkFileCount} files created 🔥`);
});

program.parse(process.argv);
7 changes: 5 additions & 2 deletions src/utils/classValidatorMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ export function classValidatorMap(field: Field) {
const type = classValidatorTypeMap(field.fieldType).toString();
const res = `${
type !== "@IsEnum"
? `${type}({message: "${field.name} 类型错误,请传入 ${typeMap(field.fieldType)} 类型",${field.array ? "each: true" : ""}})\n${field.optional ? "@IsOptional()\n" : ""}${field.array ? `@IsArray({message: "${field.name} 类型错误,请传入 ${typeMap(field.fieldType)}[] 类型"})\n` : ""}`
? `${type}({message: "${field.name} 类型错误,请传入 ${typeMap(field.fieldType)} 类型"${
field.optional || field.array ? ", " : ""
}${field.array ? "each: true" : ""}})\n${
field.optional ? "@IsOptional()\n" : ""
}${field.array ? `@IsArray({message: "${field.name} 类型错误,请传入 ${typeMap(field.fieldType)}[] 类型"})\n` : ""}`
: `${type}({enum: ${field.fieldType} ,message: "${field.name} 类型错误,请传入 ${typeMap(field.fieldType)} 类型"})\n${field.optional ? "@IsOptional()\n" : ""}${field.array ? `@IsArray({message: "${field.name} 类型错误,请传入 ${typeMap(field.fieldType)}[] 类型"})\n` : ""}`
}`;
console.log(res);
return res;
}
1 change: 1 addition & 0 deletions src/utils/mkFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function mkFile(
fileName: string,
content: string,
dryRun = false,
silent = false,
) {
if (!dryRun) {
fs.ensureDirSync(path);
Expand Down
9 changes: 6 additions & 3 deletions src/utils/propertyMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Field, Func } from "@mrleebo/prisma-ast";
import { classValidatorMap } from "./classValidatorMap";
import { getRelation } from "./getRelation";
import { getRelation, getRelationMap } from "./getRelation";
import { swaggerMap } from "./swaggerMap";
import { classValidatorTypeMap, typeMap } from "./typeMap";

export function propertyMap(field: Field) {
Expand Down Expand Up @@ -48,12 +49,14 @@ export function dtoPropertyMap(field: Field, array: Field[]) {
return v.name === `${field.name}Id`;
}) ?? false;
if (isRelation && field.array === true) {
return `@IsString({ each: true, message: "${field.name}Ids 类型错误,请传入 string[] 类型" })\n//@IsUUID("4", { each: true, message: "${field.name}Ids 类型错误,请传入 uuid[] 类型" })\n@IsOptional()\n${field.name}Ids${field.optional ? "?" : ""}: string[] ${field.optional ? "| null" : ""}\n`;
return `${swaggerMap(field, { setType: "String", setArray: true, setRequired: false, setDescription: `${field.name}Ids` })}@IsString({ each: true, message: "${field.name}Ids 类型错误,请传入 string[] 类型" })\n//@IsUUID("4", { each: true, message: "${field.name}Ids 类型错误,请传入 uuid[] 类型" })\n@IsOptional()\n${field.name}Ids${field.optional ? "?" : ""}: string[] ${field.optional ? "| null" : ""}\n`;
}
if (isRelation && hasRelationId) {
return "";
}
return `${classValidatorMap(field)}${field.name}${field.optional ? "?" : ""}: ${typeMap(
return `${swaggerMap(field, { setRequired: !field.optional, setDescription: `${field.name}` })}${classValidatorMap(
field,
)}${field.name}${field.optional ? "?" : ""}: ${typeMap(
field.fieldType,
)} ${field.optional ? "| null" : ""}\n`;
}
30 changes: 25 additions & 5 deletions src/utils/swaggerMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import type { Field } from "@mrleebo/prisma-ast";
import { createdEnumMap } from "../generateEnum";
import { swaggerTypeMap, typeMap } from "./typeMap";

export function swaggerMap(field: Field) {
export type SwaggerMapOption = {
isEnum?: boolean;
setType?: string;
setArray?: boolean;
setRequired?: boolean;
setDescription?: string;
};

export function swaggerMap(field: Field, setOption?: SwaggerMapOption) {
if (typeof field.fieldType !== "string") {
return "";
}
Expand All @@ -15,9 +23,21 @@ export function swaggerMap(field: Field) {
field.optional || true
} })\n`;
}
return `@ApiProperty({ type: ${swaggerTypeMap(field.fieldType)}, description: "${
field.comment || ""
}", isArray: ${field.array || false}, required: ${
field.optional || true
return `@ApiProperty({ ${setOption?.isEnum ? "enum: " : "type: "}${
setOption?.setType !== undefined
? setOption.setType
: swaggerTypeMap(field.fieldType)
}, description: "${field.comment || ""}", isArray: ${
setOption?.setArray !== undefined
? setOption.setArray
: field.array !== undefined
? field.array
: false
}, required: ${
setOption?.setRequired !== undefined
? setOption.setRequired
: field.optional !== undefined
? field.optional === false
: true
} })\n`;
}

0 comments on commit 3c6bf9a

Please sign in to comment.