forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trpc.ts
122 lines (109 loc) · 3.24 KB
/
trpc.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import path from "path";
import fs from "fs-extra";
import { PKG_ROOT } from "~/consts.js";
import { type Installer } from "~/installers/index.js";
import { addPackageDependency } from "~/utils/addPackageDependency.js";
export const trpcInstaller: Installer = ({
projectDir,
packages,
appRouter,
}) => {
addPackageDependency({
projectDir,
dependencies: [
"@tanstack/react-query",
"superjson",
"@trpc/server",
"@trpc/client",
"@trpc/next",
"@trpc/react-query",
],
devMode: false,
});
const usingAuth = packages?.nextAuth.inUse;
const usingPrisma = packages?.prisma.inUse;
const usingDrizzle = packages?.drizzle.inUse;
const usingDb = usingPrisma || usingDrizzle;
const extrasDir = path.join(PKG_ROOT, "template/extras");
const apiHandlerFile = "src/pages/api/trpc/[trpc].ts";
const routeHandlerFile = "src/app/api/trpc/[trpc]/route.ts";
const srcToUse = appRouter ? routeHandlerFile : apiHandlerFile;
const apiHandlerSrc = path.join(extrasDir, srcToUse);
const apiHandlerDest = path.join(projectDir, srcToUse);
const trpcFile =
usingAuth && usingDb
? "with-auth-db.ts"
: usingAuth
? "with-auth.ts"
: usingDb
? "with-db.ts"
: "base.ts";
const trpcSrc = path.join(
extrasDir,
"src/server/api",
appRouter ? "trpc-app" : "trpc-pages",
trpcFile
);
const trpcDest = path.join(projectDir, "src/server/api/trpc.ts");
const rootRouterSrc = path.join(extrasDir, "src/server/api/root.ts");
const rootRouterDest = path.join(projectDir, "src/server/api/root.ts");
const exampleRouterFile =
usingAuth && usingPrisma
? "with-auth-prisma.ts"
: usingAuth && usingDrizzle
? "with-auth-drizzle.ts"
: usingAuth
? "with-auth.ts"
: usingPrisma
? "with-prisma.ts"
: usingDrizzle
? "with-drizzle.ts"
: "base.ts";
const exampleRouterSrc = path.join(
extrasDir,
"src/server/api/routers/post",
exampleRouterFile
);
const exampleRouterDest = path.join(
projectDir,
"src/server/api/routers/post.ts"
);
const copySrcDest: [string, string][] = [
[apiHandlerSrc, apiHandlerDest],
[trpcSrc, trpcDest],
[rootRouterSrc, rootRouterDest],
[exampleRouterSrc, exampleRouterDest],
];
if (appRouter) {
const trpcDir = path.join(extrasDir, "src/trpc");
copySrcDest.push(
[
path.join(trpcDir, "server.ts"),
path.join(projectDir, "src/trpc/server.ts"),
],
[
path.join(trpcDir, "react.tsx"),
path.join(projectDir, "src/trpc/react.tsx"),
],
[
path.join(trpcDir, "shared.ts"),
path.join(projectDir, "src/trpc/shared.ts"),
],
[
path.join(
extrasDir,
"src/app/_components",
packages?.tailwind.inUse ? "create-post-tw.tsx" : "create-post.tsx"
),
path.join(projectDir, "src/app/_components/create-post.tsx"),
]
);
} else {
const utilsSrc = path.join(extrasDir, "src/utils/api.ts");
const utilsDest = path.join(projectDir, "src/utils/api.ts");
copySrcDest.push([utilsSrc, utilsDest]);
}
copySrcDest.forEach(([src, dest]) => {
fs.copySync(src, dest);
});
};