From 54eb21d5e76df1a00d271ae5e3856b5912c4c5b5 Mon Sep 17 00:00:00 2001 From: Adrian Date: Thu, 28 Nov 2024 11:24:57 +0100 Subject: [PATCH 1/2] add prettier config option --- README.md | 41 ++++++++++--------- generator.ts | 15 +++++-- package-lock.json | 4 +- tests/prettier-options/.prettierrc | 4 ++ tests/prettier-options/expected/interfaces.ts | 9 ++++ tests/prettier-options/schema.prisma | 28 +++++++++++++ 6 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 tests/prettier-options/.prettierrc create mode 100644 tests/prettier-options/expected/interfaces.ts create mode 100644 tests/prettier-options/schema.prisma diff --git a/README.md b/README.md index f9a55c4..ca6dd02 100644 --- a/README.md +++ b/README.md @@ -44,26 +44,27 @@ Note that `bigint` types don't have a default `toJSON` method, so the above assu ## Options -| **Option** | **Type** | **Default** | **Description** | -| ----------------- | :----------------------------------------------------: | :------------------------------------------------------------------------: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| output | `string` | `"interfaces.ts"` | The output location for the generated TypeScript interfaces. | -| enumPrefix | `string` | `""` | Prefix to add to enum types. | -| enumSuffix | `string` | `""` | Suffix to add to enum types. | -| modelPrefix | `string` | `""` | Prefix to add to model types. | -| modelSuffix | `string` | `""` | Suffix to add to model types. | -| typePrefix | `string` | `""` | Prefix to add to [type](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-composite-types) types (MongoDB only). | -| typeSuffix | `string` | `""` | Suffix to add to [type](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-composite-types) types (MongoDB only). | -| headerComment | `string` | `"This file was auto-generated by prisma-generator-typescript-interfaces"` | Sets the header comment added to the top of the generated file. Set this to an empty string to disable the header comment. Supports multiple lines with `"\n"`. | -| modelType | `"interface" \| "type"` | `"interface"` | Controls how model definitions are generated. `"interface"` will create TypeScript interfaces, `"type"` will create TypeScript types. If using MongoDB, this also affects `type` definitions. | -| enumType | `"stringUnion" \| "enum" \| "object"` | `"stringUnion"` | Controls how enums are generated. `"object"` will create an object and type like the Prisma client, `"enum"` will create TypeScript enums, `"stringUnion"` will create a string union type. | -| dateType | `"Date" \| "string" \| "number"` | `"Date"` | The type to use for DateTime model fields. | -| bigIntType | `"bigint" \| "string" \| "number"` | `"bigint"` | The type to use for BigInt model fields. | -| decimalType | `"Decimal" \| "string" \| "number"` | `"Decimal"` | The type to use for Decimal model fields. The `Decimal` type here is just an interface with a `valueOf()` function. You will need to cast to an actual Decimal type if you want to use other methods. | -| bytesType | `"Buffer" \| "BufferObject" \| "string" \| "number[]"` | `"Buffer"` | The type to use for Bytes model fields. `BufferObject` is a type definition which matches the output of `Buffer.toJSON()`, which is called when running `JSON.stringify()` on a Buffer. | -| optionalRelations | `boolean` | `true` | Controls whether model relation fields are optional. If `true`, all model relation fields will use `?:` in the field definition. | -| omitRelations | `boolean` | `false` | Controls whether model relation fields are omitted. If `true`, model definitions will not include their relations. | -| optionalNullables | `boolean` | `false` | Controls whether nullable fields are optional. Nullable fields are always defined with `\| null` in their type definition, but if this is `true`, they will also use `?:`. | -| prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. | +| **Option** | **Type** | **Default** | **Description** | +| --------------------- | :----------------------------------------------------: | :------------------------------------------------------------------------: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| output | `string` | `"interfaces.ts"` | The output location for the generated TypeScript interfaces. | +| enumPrefix | `string` | `""` | Prefix to add to enum types. | +| enumSuffix | `string` | `""` | Suffix to add to enum types. | +| modelPrefix | `string` | `""` | Prefix to add to model types. | +| modelSuffix | `string` | `""` | Suffix to add to model types. | +| typePrefix | `string` | `""` | Prefix to add to [type](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-composite-types) types (MongoDB only). | +| typeSuffix | `string` | `""` | Suffix to add to [type](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-composite-types) types (MongoDB only). | +| headerComment | `string` | `"This file was auto-generated by prisma-generator-typescript-interfaces"` | Sets the header comment added to the top of the generated file. Set this to an empty string to disable the header comment. Supports multiple lines with `"\n"`. | +| modelType | `"interface" \| "type"` | `"interface"` | Controls how model definitions are generated. `"interface"` will create TypeScript interfaces, `"type"` will create TypeScript types. If using MongoDB, this also affects `type` definitions. | +| enumType | `"stringUnion" \| "enum" \| "object"` | `"stringUnion"` | Controls how enums are generated. `"object"` will create an object and type like the Prisma client, `"enum"` will create TypeScript enums, `"stringUnion"` will create a string union type. | +| dateType | `"Date" \| "string" \| "number"` | `"Date"` | The type to use for DateTime model fields. | +| bigIntType | `"bigint" \| "string" \| "number"` | `"bigint"` | The type to use for BigInt model fields. | +| decimalType | `"Decimal" \| "string" \| "number"` | `"Decimal"` | The type to use for Decimal model fields. The `Decimal` type here is just an interface with a `valueOf()` function. You will need to cast to an actual Decimal type if you want to use other methods. | +| bytesType | `"Buffer" \| "BufferObject" \| "string" \| "number[]"` | `"Buffer"` | The type to use for Bytes model fields. `BufferObject` is a type definition which matches the output of `Buffer.toJSON()`, which is called when running `JSON.stringify()` on a Buffer. | +| optionalRelations | `boolean` | `true` | Controls whether model relation fields are optional. If `true`, all model relation fields will use `?:` in the field definition. | +| omitRelations | `boolean` | `false` | Controls whether model relation fields are omitted. If `true`, model definitions will not include their relations. | +| optionalNullables | `boolean` | `false` | Controls whether nullable fields are optional. Nullable fields are always defined with `\| null` in their type definition, but if this is `true`, they will also use `?:`. | +| prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. | +| resolvePrettierConfig | `boolean` | `false` | Tries to find and use prettier config file in current project | ## Example diff --git a/generator.ts b/generator.ts index b4979cf..f27b1d9 100755 --- a/generator.ts +++ b/generator.ts @@ -24,6 +24,7 @@ interface Config { omitRelations: boolean; optionalNullables: boolean; prettier: boolean; + resolvePrettierConfig: boolean; } // Map of Prisma scalar types to Typescript type getters @@ -194,6 +195,7 @@ generatorHandler({ omitRelations: baseConfig.omitRelations === "true", // Default false optionalNullables: baseConfig.optionalNullables === "true", // Default false prettier: baseConfig.prettier === "true", // Default false + resolvePrettierConfig: baseConfig.resolvePrettierConfig === "true", // Default false }; validateConfig(config); @@ -232,6 +234,9 @@ generatorHandler({ ts = `${headerContent}\n\n${ts}`; } + const outputFile = options.generator.output?.value as string; + const outputDir = dirname(outputFile); + if (config.prettier) { // Prettier is imported inside this if so that it's not a required dependency let prettier: typeof import("prettier"); @@ -241,11 +246,15 @@ generatorHandler({ throw new Error("Unable import Prettier. Is it installed?"); } - ts = await prettier.format(ts, { parser: "typescript" }); + let prettierOptions = config.resolvePrettierConfig + ? await prettier.resolveConfig(outputFile) + : {}; + + prettierOptions ??= {}; + + ts = await prettier.format(ts, { ...prettierOptions, parser: "typescript" }); } - const outputFile = options.generator.output?.value as string; - const outputDir = dirname(outputFile); await mkdir(outputDir, { recursive: true }); await writeFile(outputFile, ts); }, diff --git a/package-lock.json b/package-lock.json index b0f58a2..9ab6327 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prisma-generator-typescript-interfaces", - "version": "1.6.0", + "version": "1.6.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "prisma-generator-typescript-interfaces", - "version": "1.6.0", + "version": "1.6.1", "license": "MIT", "dependencies": { "@prisma/generator-helper": "^5.0.0" diff --git a/tests/prettier-options/.prettierrc b/tests/prettier-options/.prettierrc new file mode 100644 index 0000000..7d76aa4 --- /dev/null +++ b/tests/prettier-options/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": false, + "tabWidth": 4 +} diff --git a/tests/prettier-options/expected/interfaces.ts b/tests/prettier-options/expected/interfaces.ts new file mode 100644 index 0000000..faeed11 --- /dev/null +++ b/tests/prettier-options/expected/interfaces.ts @@ -0,0 +1,9 @@ +// This file was auto-generated by prisma-generator-typescript-interfaces + +export interface User { + id: string + name: string + email: string + createdAt: Date + updatedAt: Date +} diff --git a/tests/prettier-options/schema.prisma b/tests/prettier-options/schema.prisma new file mode 100644 index 0000000..c77ff0e --- /dev/null +++ b/tests/prettier-options/schema.prisma @@ -0,0 +1,28 @@ +// Test if generator uses prettier config file when using prettier option + +// ======================== +// Generator Tests +// ======================== + +generator typescriptInterfaces { + provider = "node --loader ts-node/esm generator.ts" + prettier = true + resolvePrettierConfig = true +} + +// ======================== +// Prisma Schema +// ======================== + +datasource db { + provider = "postgresql" + url = "" +} + +model User { + id String @id @default(cuid()) + name String + email String @unique + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} From 953ed371f7ac8f320fdaf3f13baf550a940180b6 Mon Sep 17 00:00:00 2001 From: Adrian Date: Fri, 29 Nov 2024 09:52:12 +0100 Subject: [PATCH 2/2] change prettier config defaults --- README.md | 2 +- generator.ts | 2 +- .../expected/{interfaces.ts => withConfig.ts} | 0 tests/prettier-options/expected/withoutConfig.ts | 9 +++++++++ tests/prettier-options/schema.prisma | 11 +++++++++-- 5 files changed, 20 insertions(+), 4 deletions(-) rename tests/prettier-options/expected/{interfaces.ts => withConfig.ts} (100%) create mode 100644 tests/prettier-options/expected/withoutConfig.ts diff --git a/README.md b/README.md index ca6dd02..71107dc 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Note that `bigint` types don't have a default `toJSON` method, so the above assu | omitRelations | `boolean` | `false` | Controls whether model relation fields are omitted. If `true`, model definitions will not include their relations. | | optionalNullables | `boolean` | `false` | Controls whether nullable fields are optional. Nullable fields are always defined with `\| null` in their type definition, but if this is `true`, they will also use `?:`. | | prettier | `boolean` | `false` | Formats the output using Prettier. Setting this to `true` requires that the `prettier` package is available. | -| resolvePrettierConfig | `boolean` | `false` | Tries to find and use prettier config file in current project | +| resolvePrettierConfig | `boolean` | `true` | Tries to find and use a Prettier config file relative to the output location. | ## Example diff --git a/generator.ts b/generator.ts index f27b1d9..2d8d6d5 100755 --- a/generator.ts +++ b/generator.ts @@ -195,7 +195,7 @@ generatorHandler({ omitRelations: baseConfig.omitRelations === "true", // Default false optionalNullables: baseConfig.optionalNullables === "true", // Default false prettier: baseConfig.prettier === "true", // Default false - resolvePrettierConfig: baseConfig.resolvePrettierConfig === "true", // Default false + resolvePrettierConfig: baseConfig.resolvePrettierConfig !== "false", // Default true }; validateConfig(config); diff --git a/tests/prettier-options/expected/interfaces.ts b/tests/prettier-options/expected/withConfig.ts similarity index 100% rename from tests/prettier-options/expected/interfaces.ts rename to tests/prettier-options/expected/withConfig.ts diff --git a/tests/prettier-options/expected/withoutConfig.ts b/tests/prettier-options/expected/withoutConfig.ts new file mode 100644 index 0000000..7ad5d2a --- /dev/null +++ b/tests/prettier-options/expected/withoutConfig.ts @@ -0,0 +1,9 @@ +// This file was auto-generated by prisma-generator-typescript-interfaces + +export interface User { + id: string; + name: string; + email: string; + createdAt: Date; + updatedAt: Date; +} diff --git a/tests/prettier-options/schema.prisma b/tests/prettier-options/schema.prisma index c77ff0e..515ed7b 100644 --- a/tests/prettier-options/schema.prisma +++ b/tests/prettier-options/schema.prisma @@ -4,10 +4,17 @@ // Generator Tests // ======================== -generator typescriptInterfaces { +generator withConfig { + provider = "node --loader ts-node/esm generator.ts" + output = "withConfig.ts" + prettier = true +} + +generator withoutConfig { provider = "node --loader ts-node/esm generator.ts" + output = "withoutConfig.ts" prettier = true - resolvePrettierConfig = true + resolvePrettierConfig = false } // ========================