-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.go
75 lines (58 loc) · 2.06 KB
/
hooks.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package entber
import (
"path"
"entgo.io/ent/entc/gen"
)
func (e *extension) generate(next gen.Generator) gen.Generator {
return gen.GenerateFunc(func(g *gen.Graph) error {
e.data.Graph = g
destination := rootDir()
if e.data.AppConfig != nil {
destination = path.Join(destination, e.data.AppConfig.Path)
}
if e.data.EntConfig.Edges {
writeFile(path.Join(destination, "ent/input.go"), parseTemplate("ent/input", e.data))
} else {
writeFile(path.Join(destination, "ent/input.go"), parseTemplate("ent/input_only", e.data))
}
if e.data.EntConfig.Query {
s := parseTemplate("ent/query", e.data)
writeFile(path.Join(destination, "ent/query.go"), s)
}
if e.data.EntConfig.Edges {
s := parseTemplate("ent/errors", e.data)
writeFile(path.Join(destination, "ent/errors.go"), s)
}
if e.data.WithFiber {
s := parseTemplate("fiber/routes/routes", e.data)
writeFile(path.Join(destination, "routes/routes.go"), s)
s = parseTemplate("fiber/handlers/helper", e.data)
writeFile(path.Join(destination, "handlers/helper.go"), s)
s = parseTemplate("fiber/handlers/ws", e.data)
writeFile(path.Join(destination, "handlers/ws.go"), s)
s = parseTemplate("fiber/utils/utils", e.data)
writeFile(path.Join(destination, "utils/utils.go"), s)
for _, schema := range g.Schemas {
e.data.CurrentSchema = schema
s := parseTemplate("fiber/handlers/handler", e.data)
writeFile(path.Join(destination, "handlers", snake(plural(schema.Name))+".go"), s)
}
}
if e.data.DBConfig != nil {
s := parseTemplate("ent/db", e.data)
writeFile(path.Join(destination, e.data.DBConfig.Path, "db.go"), s)
}
if e.data.TSConfig != nil {
if e.data.TSConfig.TypesOnly {
s := parseTemplate("ts/types_only", e.data)
writeFile(path.Join(e.data.TSConfig.ApiPath, "types.ts"), s)
} else {
s := parseTemplate("ts/api", e.data)
writeFile(path.Join(e.data.TSConfig.ApiPath, "api.ts"), s)
s = parseTemplate("ts/types", e.data)
writeFile(path.Join(e.data.TSConfig.ApiPath, "types.ts"), s)
}
}
return next.Generate(g)
})
}