-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum-generator.js
41 lines (34 loc) · 1.26 KB
/
enum-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* source: https://gist.github.com/juliusmarminge/b06a3e421117a56ba1fea54e2a4c0fcb
*/
import GeneratorHelper from "@prisma/generator-helper";
import fs from "node:fs/promises";
import path from "node:path";
const header = `// This file was generated by a custom prisma generator, do not edit manually.\n`;
GeneratorHelper.generatorHandler({
onManifest() {
return {
defaultOutput: "./../src/lib/prisma-enums/index.ts",
prettyName: "Prisma Enum Generator",
};
},
async onGenerate(options) {
const enums = options.dmmf.datamodel.enums;
const output = enums.map((e) => {
let enumString = `export const ${e.name} = {\n`;
e.values.forEach(({ name: value }) => {
enumString += ` ${value}: "${value}",\n`;
});
enumString += `} as const;\n\n`;
enumString += `export type ${e.name} = (typeof ${e.name})[keyof typeof ${e.name}];\n`;
return enumString;
});
const outputFile = options.generator.output;
if (!outputFile || !outputFile.value) {
throw new Error("No output file specified");
}
const outputPath = path.resolve(outputFile.value);
await fs.mkdir(path.dirname(outputPath), { recursive: true });
await fs.writeFile(outputPath, header + output.join("\n"), "utf-8");
},
});