Skip to content

Commit

Permalink
🐞 fix: fix some name err
Browse files Browse the repository at this point in the history
  • Loading branch information
gylove1994 committed Sep 26, 2024
1 parent 47b1bfb commit 66a5df7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@gylove1994/npg",
"version": "0.2.1",
"version": "0.2.2",
"description": "A command line tool for nestjs-prisma code generation",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"lint": "biome check --write ./src",
"dev": "nodemon --watch src --ext ts --exec \"rimraf dist && tsc && cp .env dist\"",
Expand Down
7 changes: 5 additions & 2 deletions src/generateEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function generateEntity(model: Model) {
model.name,
)} {\n${propertiesContent}}\n`;
return {
name: `${strings.classify(model.name)}Entity.ts`,
name: `${strings.camelize(model.name)}Entity.ts`,
content,
};
}
Expand Down Expand Up @@ -122,7 +122,10 @@ export function ${entityName}PickType<T extends keyof ${entityName}>(keys: T[])
const content = `${imports}\n${c}\n${pickTypeFunction}`;

// 返回生成的文件名和内容
return { name: `${entityName}PickTypeEntities.ts`, content };
return {
name: `${strings.camelize(entityName)}PickTypeEntities.ts`,
content,
};
}

export function generatePickEntityFile(
Expand Down
2 changes: 1 addition & 1 deletion src/generateEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function generateEnum(enumModel: Enum) {
enumModel.name,
)} {\n${enumContent.map((v) => `${v.name} = "${v.name}"`).join(",\n")}\n}`;
const res = {
name: `${strings.classify(enumModel.name)}Enum.ts`,
name: `${strings.camelize(enumModel.name)}Enum.ts`,
content,
};
createdEnumMap.set(enumModel.name, res);
Expand Down
2 changes: 1 addition & 1 deletion src/generateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function generateService(model: Schema) {
const modelNameCamelize = strings.camelize(v.name);
const modelNameCapitalize = strings.capitalize(modelNameCamelize);
return {
name: `${modelNameCapitalize}`,
name: `${modelNameCamelize}`,
content: serviceTemplate
.replace(/{_@modelName@_}/g, v.name)
.replace(/{_@modelNameCapitalize@_}/g, modelNameCapitalize),
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ program
type: "list",
name: "type",
message: "What type of code do you want to generate?",
choices: ["entity", "nestModule", "all"],
choices: ["entity", "entity-with-pick", "nestModule", "all"],
default: process.env.NPG_LIST_DEFAULT || "all",
},
{
Expand All @@ -59,10 +59,13 @@ program
]);
const schemaFile = await fs.readFile(answers.prismaPath, "utf-8");
const prisma = getSchema(schemaFile);
if (answers.type === "entity") {
if (answers.type === "entity-with-pick") {
generateEnumFile(prisma, answers.outputPath, answers.dryRun);
generateEntityFile(prisma, answers.outputPath, answers.dryRun);
generatePickEntityFile(prisma, answers.outputPath, answers.dryRun);
} else if (answers.type === "entity") {
generateEnumFile(prisma, answers.outputPath, answers.dryRun);
generateEntityFile(prisma, answers.outputPath, answers.dryRun);
} else if (answers.type === "nestModule") {
generateNestModuleFile(prisma, answers.outputPath, answers.dryRun);
generateNestControllerFile(prisma, answers.outputPath, answers.dryRun);
Expand Down
5 changes: 3 additions & 2 deletions src/utils/import.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { strings } from "@angular-devkit/core";
import { createdEnumMap } from "../generateEnum";

export function importFile(importName: string, useRelativePath = false) {
if (
createdEnumMap.has(importName) ||
createdEnumMap.has(importName.toUpperCase())
) {
return `import { ${importName} } from "${useRelativePath ? "@entity/" : "./"}${importName}Enum";\n`;
return `import { ${importName} } from "${useRelativePath ? "@entity/" : "./"}${strings.camelize(importName)}Enum";\n`;
}
return `import { ${importName} } from "${useRelativePath ? "@entity/" : "./"}${importName}Entity";\n`;
return `import { ${importName} } from "${useRelativePath ? "@entity/" : "./"}${strings.camelize(importName)}Entity";\n`;
}

export function importApiProperty() {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/propertyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ import type { Field } from "@mrleebo/prisma-ast";
import { typeMap } from "./typeMap";

export function propertyMap(field: Field) {
return `${field.name}: ${typeMap(field.fieldType)}\n`;
return `${field.name}${field.optional ? "?" : ""}: ${typeMap(
field.fieldType,
)} ${field.optional ? "| null" : ""}\n`;
}

0 comments on commit 66a5df7

Please sign in to comment.