forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envVars.ts
134 lines (116 loc) · 3.8 KB
/
envVars.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
123
124
125
126
127
128
129
130
131
132
133
134
import path from "path";
import fs from "fs-extra";
import { PKG_ROOT } from "~/consts.js";
import { type DatabaseProvider, type Installer } from "~/installers/index.js";
export const envVariablesInstaller: Installer = ({
projectDir,
packages,
databaseProvider,
projectName,
}) => {
const usingAuth = packages?.nextAuth.inUse;
const usingPrisma = packages?.prisma.inUse;
const usingDrizzle = packages?.drizzle.inUse;
const usingDb = usingPrisma || usingDrizzle;
const usingPlanetScale = databaseProvider === "planetscale";
const envContent = getEnvContent(
!!usingAuth,
!!usingPrisma,
!!usingDrizzle,
databaseProvider,
projectName
);
let envFile = "";
if (usingDb) {
if (usingPlanetScale) {
if (usingAuth) envFile = "with-auth-db-planetscale.js";
else envFile = "with-db-planetscale.js";
} else {
if (usingAuth) envFile = "with-auth-db.js";
else envFile = "with-db.js";
}
} else {
if (usingAuth) envFile = "with-auth.js";
}
if (envFile !== "") {
const envSchemaSrc = path.join(
PKG_ROOT,
"template/extras/src/env",
envFile
);
const envSchemaDest = path.join(projectDir, "src/env.js");
fs.copyFileSync(envSchemaSrc, envSchemaDest);
}
const envDest = path.join(projectDir, ".env");
const envExampleDest = path.join(projectDir, ".env.example");
fs.writeFileSync(envDest, envContent, "utf-8");
fs.writeFileSync(envExampleDest, exampleEnvContent + envContent, "utf-8");
};
const getEnvContent = (
usingAuth: boolean,
usingPrisma: boolean,
usingDrizzle: boolean,
databaseProvider: DatabaseProvider,
projectName: string
) => {
let content = `
# When adding additional environment variables, the schema in "/src/env.js"
# should be updated accordingly.
`
.trim()
.concat("\n");
if (usingPrisma)
content += `
# Prisma
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
`;
if (usingDrizzle) content += "\n# Drizzle\n";
if (usingPrisma || usingDrizzle) {
if (databaseProvider === "planetscale") {
if (usingDrizzle) {
content += `# Get the Database URL from the "prisma" dropdown selector in PlanetScale.
# Change the query params at the end of the URL to "?ssl={"rejectUnauthorized":true}"
DATABASE_URL='mysql:https://YOUR_MYSQL_URL_HERE?ssl={"rejectUnauthorized":true}'`;
} else {
content = `# Get the Database URL from the "prisma" dropdown selector in PlanetScale.
DATABASE_URL='mysql:https://YOUR_MYSQL_URL_HERE?sslaccept=strict'`;
}
} else if (databaseProvider === "mysql") {
content += `DATABASE_URL="mysql:https://root:password@localhost:3306/${projectName}"`;
} else if (databaseProvider === "postgres") {
content += `DATABASE_URL="postgresql:https://postgres:password@localhost:5432/${projectName}"`;
} else if (databaseProvider === "sqlite") {
content += 'DATABASE_URL="file:./db.sqlite"';
}
content += "\n";
}
if (usingAuth)
content += `
# Next Auth
# You can generate a new secret on the command line with:
# openssl rand -base64 32
# https://next-auth.js.org/configuration/options#secret
# NEXTAUTH_SECRET=""
NEXTAUTH_URL="http:https://localhost:3000"
# Next Auth Discord Provider
DISCORD_CLIENT_ID=""
DISCORD_CLIENT_SECRET=""
`;
if (!usingAuth && !usingPrisma)
content += `
# Example:
# SERVERVAR="foo"
# NEXT_PUBLIC_CLIENTVAR="bar"
`;
return content;
};
const exampleEnvContent = `
# Since the ".env" file is gitignored, you can use the ".env.example" file to
# build a new ".env" file when you clone the repo. Keep this file up-to-date
# when you add new variables to \`.env\`.
# This file will be committed to version control, so make sure not to have any
# secrets in it. If you are cloning this repo, create a copy of this file named
# ".env" and populate it with your secrets.
`
.trim()
.concat("\n\n");