|
1 |
| -import { GeneratorOptions } from '@prisma/generator-helper'; |
2 |
| -import path from 'path'; |
| 1 | +import { DMMF, GeneratorOptions } from '@prisma/generator-helper'; |
3 | 2 | import fs from 'fs';
|
4 |
| -import * as child_process from 'child_process'; |
5 |
| -import os from 'os'; |
6 | 3 |
|
7 |
| -type SchemaEnum = { |
8 |
| - name: string; |
9 |
| - values: string[]; |
10 |
| -}; |
11 |
| - |
12 |
| -const enumRegex = /enum [\s\S]*?\}/g; |
13 |
| -const modelRegex = /model [\s\S]*?\}/g; |
14 |
| -const curlyBracesRegex = /\{([^{}]+)\}/g; |
15 |
| - |
16 |
| -function parseEnums(dataModel: string) { |
17 |
| - const enumStrings = dataModel.match(enumRegex); |
18 |
| - const modelStrings = dataModel.match(modelRegex); |
19 |
| - |
20 |
| - const enums: string[] = |
21 |
| - enumStrings?.map((enumString) => { |
22 |
| - const name = enumString.split(' ')[1]; |
23 |
| - |
24 |
| - // const valueString = enumString.substring(enumString.indexOf('{')).replace('{', '').replace('}', '').trim(); |
25 |
| - // const values = valueString.split(/\s/).filter(Boolean); |
26 |
| - |
27 |
| - return name; |
28 |
| - }) ?? []; |
| 4 | +function extractEnums(dataModel: DMMF.Datamodel): string[] { |
| 5 | + const enums = dataModel.enums.map((e) => e.name); |
29 | 6 |
|
30 |
| - const enumUsage: any = {}; |
| 7 | + const enumUsage: Record<string, number> = {}; |
31 | 8 | enums.forEach((e) => (enumUsage[e] = 0));
|
32 | 9 |
|
33 |
| - const ignoreTokens = ['@@', '//']; |
34 |
| - |
35 |
| - for (const modelString of modelStrings ?? []) { |
36 |
| - const fieldsString = modelString.substring(modelString.indexOf('{')).replace('{', '').replace('}', '').trim(); |
37 |
| - const fields = fieldsString |
38 |
| - .split('\n') |
39 |
| - .map((f) => f.trim()) |
40 |
| - .filter((f) => f && !ignoreTokens.some((it) => f.startsWith(it))); |
41 |
| - for (const field of fields) { |
42 |
| - const tokens = field.split(/\s/).filter(Boolean); |
43 |
| - |
44 |
| - const fieldType = tokens[1] ?? null; |
45 |
| - |
46 |
| - if (!fieldType) continue; |
47 |
| - |
48 |
| - if (enums.includes(fieldType)) { |
49 |
| - enumUsage[fieldType]++; |
50 |
| - } |
| 10 | + for (const model of dataModel.models) { |
| 11 | + for (const field of model.fields) { |
| 12 | + if (enums.includes(field.type)) enumUsage[field.type]++; |
51 | 13 | }
|
52 | 14 | }
|
53 | 15 |
|
@@ -84,9 +46,13 @@ export default async (options: GeneratorOptions) => {
|
84 | 46 |
|
85 | 47 | const isTs = !!config.useTs ? config.useTs === 'true' : true;
|
86 | 48 |
|
87 |
| - const output = options.generator.output?.value || `./prisma/enum-validators.${isTs ? 'ts' : 'js'}`; |
88 |
| - const enums = parseEnums(options.datamodel); |
| 49 | + let output = options.generator.output?.value || `./prisma/enum-validators.${isTs ? 'ts' : 'js'}`; |
| 50 | + |
| 51 | + if (!isTs && !output.endsWith('.js')) { |
| 52 | + output = `${output.substring(0, output.lastIndexOf('.'))}.js`; |
| 53 | + } |
89 | 54 |
|
| 55 | + const enums = extractEnums(options.dmmf.datamodel); |
90 | 56 | const fileContent = generateFileContent(enums, isTs);
|
91 | 57 |
|
92 | 58 | fs.writeFileSync(output, fileContent);
|
|
0 commit comments