forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envVars.ts
104 lines (87 loc) · 2.73 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
import path from "path";
import fs from "fs-extra";
import { PKG_ROOT } from "~/consts.js";
import { type Installer } from "~/installers/index.js";
export const envVariablesInstaller: Installer = ({ projectDir, packages }) => {
const usingAuth = packages?.nextAuth.inUse;
const usingPrisma = packages?.prisma.inUse;
const usingDrizzle = packages?.drizzle.inUse;
const usingDb = usingPrisma || usingDrizzle;
const envContent = getEnvContent(!!usingAuth, !!usingPrisma, !!usingDrizzle);
const envFile =
usingAuth && usingDb
? "with-auth-db.js"
: usingAuth
? "with-auth.js"
: usingDb
? "with-db.js"
: "";
if (envFile !== "") {
const envSchemaSrc = path.join(
PKG_ROOT,
"template/extras/src/env",
envFile
);
const envSchemaDest = path.join(projectDir, "src/env.js");
fs.copySync(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
) => {
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
DATABASE_URL="file:./db.sqlite"
`;
if (usingDrizzle) {
content += `
# Drizzle
# 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}'
`;
}
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");