diff --git a/package.json b/package.json index bb5adabb..edbb01f2 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,8 @@ "get-relative-path": "^1.0.2", "pupa": "^2.0.1", "to-kebab": "^1.0.7", - "ts-morph": "^7.2.0" + "ts-morph": "^7.3.0", + "typescript-equals": "^1.0.0" }, "peerDependencies": { "@prisma/client": "2" @@ -64,14 +65,14 @@ "@prisma/client": "^2.4.1", "@semantic-release/changelog": "^5.0.1", "@semantic-release/git": "^9.0.0", - "@types/mocha": "^8.0.1", + "@types/mocha": "^8.0.2", "@types/node": "^14.0.27", "@typescript-eslint/eslint-plugin": "^3.9.0", "@typescript-eslint/parser": "^3.9.0", "c8": "^7.3.0", - "eslint": "^7.6.0", + "eslint": "^7.7.0", "eslint-import-resolver-node": "^0.3.4", - "eslint-plugin-etc": "0.0.1-beta.38", + "eslint-plugin-etc": "0.0.1-beta.40", "eslint-plugin-import": "^2.22.0", "eslint-plugin-only-warn": "^1.0.2", "eslint-plugin-prettier": "^3.1.4", @@ -80,7 +81,7 @@ "eslint-plugin-simple-import-sort": "^5.0.3", "eslint-plugin-sonarjs": "^0.5.0", "eslint-plugin-sort-class-members": "^1.8.0", - "eslint-plugin-total-functions": "^1.40.0", + "eslint-plugin-total-functions": "^1.42.1", "eslint-plugin-unicorn": "^21.0.0", "eslint-plugin-wix-editor": "^3.2.0", "git-branch-is": "^4.0.0", diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1f085429..b6c84cf6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,6 +10,7 @@ generator nestgraphql { model User { id String @id @default(cuid()) email String @unique + /// User's name name String @unique password String bio String? diff --git a/src/@generated/user/user.model.ts b/src/@generated/user/user.model.ts index 0a413fd3..c54bd4a1 100644 --- a/src/@generated/user/user.model.ts +++ b/src/@generated/user/user.model.ts @@ -20,7 +20,7 @@ export class User { @Field(() => String, { nullable: false, - description: undefined, + description: "User's name", }) name!: string; diff --git a/src/generate-enum.spec.ts b/src/generate-enum.spec.ts index 5fe92400..2782c644 100644 --- a/src/generate-enum.spec.ts +++ b/src/generate-enum.spec.ts @@ -89,11 +89,11 @@ describe('generate enum', () => { registerEnumType(Role, { name: 'Role' }) `, }); - assert.equal( + assert.strictEqual( sourceText.match(/registerEnumType/g)?.length, 2, 'Only import and call once should exists', ); - assert.equal(sourceText.match(/enum Role/g)?.length, 1); + assert.strictEqual(sourceText.match(/enum Role/g)?.length, 1); }); }); diff --git a/src/generate-model.ts b/src/generate-model.ts index 0548ed88..d169c0d6 100644 --- a/src/generate-model.ts +++ b/src/generate-model.ts @@ -1,13 +1,5 @@ import { DMMF as PrismaDMMF } from '@prisma/client/runtime/dmmf-types'; -import assert from 'assert'; -import { - ClassDeclaration, - Node, - ObjectLiteralExpression, - PropertyAssignment, - SourceFile, - StructureKind, -} from 'ts-morph'; +import { SourceFile } from 'ts-morph'; import { generateClass } from './generate-class'; import { generateProperty } from './generate-property'; diff --git a/src/generate-property.ts b/src/generate-property.ts index a290c347..aadf18db 100644 --- a/src/generate-property.ts +++ b/src/generate-property.ts @@ -1,4 +1,3 @@ -import { DMMF as PrismaDMMF } from '@prisma/client/runtime/dmmf-types'; import assert from 'assert'; import { ClassDeclaration, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph'; @@ -23,6 +22,7 @@ type GeneratePropertyArgs = { isId?: boolean; default?: any; isReadOnly?: boolean; + documentation?: string; }; }; @@ -113,6 +113,6 @@ export function generateProperty(args: GeneratePropertyArgs) { setObjectProperty({ expression: optionsExpression, name: 'description', - value: (field as any).documentation, + value: field.documentation, }); } diff --git a/src/index.ts b/src/index.ts index faaf83ad..1b929b01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,13 @@ import { generatorHandler } from '@prisma/generator-helper'; import { generate } from './generate'; +import { remove } from './remove'; import { update } from './update'; -// import { remove } from './remove'; generatorHandler({ async onGenerate(options) { const project = await generate(options); - // await remove(project, options); + await remove(project, options); await update(project, options); }, onManifest() { diff --git a/src/remove.ts b/src/remove.ts index 9a42ea5f..945e88c0 100644 --- a/src/remove.ts +++ b/src/remove.ts @@ -2,38 +2,23 @@ import { GeneratorOptions } from '@prisma/generator-helper'; import assert from 'assert'; import { existsSync, promises as fs } from 'fs'; import { resolve } from 'path'; -import { Project, SourceFile } from 'ts-morph'; +import { Project } from 'ts-morph'; +import { equals } from 'typescript-equals'; /** * Remove unchanged files */ export async function remove(project: Project, options: GeneratorOptions) { assert(options.generator.output); - let testSourceFile: SourceFile | undefined; for (const sourceFile of project.getSourceFiles()) { const filePath = sourceFile.getFilePath(); const outfile = resolve(options.generator.output, `.${filePath}`); if (existsSync(outfile)) { - const testText = await fs.readFile(outfile, { encoding: 'utf8' }); - testSourceFile = project.createSourceFile('0.ts', testText, { - overwrite: true, - }); - testSourceFile.formatText({ ensureNewLineAtEndOfFile: true }); - sourceFile.formatText({ ensureNewLineAtEndOfFile: true }); - if (testSourceFile.getText() === sourceFile.getText()) { + const outfileText = await fs.readFile(outfile, { encoding: 'utf8' }); + const sourceFileText = sourceFile.getText(); + if (equals(outfileText, sourceFileText)) { sourceFile.deleteImmediatelySync(); - project.removeSourceFile(sourceFile); - project.removeSourceFile(testSourceFile); - console.log('length', project.getSourceFiles().length); - } else { - if (outfile.includes('tag.model.ts')) { - console.log('testSourceFile.getText()', testSourceFile.getText()); - console.log('sourceFile.getText()', sourceFile.getText()); - } } } } - if (testSourceFile) { - testSourceFile.deleteImmediatelySync(); - } }