From 66a5df738ff44d71b15402ac0e5934a866f8b133 Mon Sep 17 00:00:00 2001 From: Geng Yan Date: Thu, 26 Sep 2024 14:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20fix=20some=20name=20err?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 ++-- src/generateEntity.ts | 7 +++++-- src/generateEnum.ts | 2 +- src/generateService.ts | 2 +- src/index.ts | 7 +++++-- src/utils/import.ts | 5 +++-- src/utils/propertyMap.ts | 4 +++- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 0109eb5..ccda5fb 100644 --- a/package.json +++ b/package.json @@ -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\"", diff --git a/src/generateEntity.ts b/src/generateEntity.ts index c64cb00..6365843 100644 --- a/src/generateEntity.ts +++ b/src/generateEntity.ts @@ -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, }; } @@ -122,7 +122,10 @@ export function ${entityName}PickType(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( diff --git a/src/generateEnum.ts b/src/generateEnum.ts index cd2c33e..068ec15 100644 --- a/src/generateEnum.ts +++ b/src/generateEnum.ts @@ -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); diff --git a/src/generateService.ts b/src/generateService.ts index 9bbbccc..24e8492 100644 --- a/src/generateService.ts +++ b/src/generateService.ts @@ -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), diff --git a/src/index.ts b/src/index.ts index c4a93c8..46dab97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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", }, { @@ -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); diff --git a/src/utils/import.ts b/src/utils/import.ts index bc90c9e..f4934c3 100644 --- a/src/utils/import.ts +++ b/src/utils/import.ts @@ -1,3 +1,4 @@ +import { strings } from "@angular-devkit/core"; import { createdEnumMap } from "../generateEnum"; export function importFile(importName: string, useRelativePath = false) { @@ -5,9 +6,9 @@ export function importFile(importName: string, useRelativePath = false) { 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() { diff --git a/src/utils/propertyMap.ts b/src/utils/propertyMap.ts index 25084ad..b79c568 100644 --- a/src/utils/propertyMap.ts +++ b/src/utils/propertyMap.ts @@ -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`; }