-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathenv-cli.js
More file actions
224 lines (191 loc) · 5.58 KB
/
env-cli.js
File metadata and controls
224 lines (191 loc) · 5.58 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// @ts-check
import * as crypto from "node:crypto";
import * as fs from "node:fs/promises";
import {
confirm,
group,
intro,
isCancel,
log,
multiselect,
outro,
text,
} from "@clack/prompts";
const DOCKER_S3_ENVS = {
accessKey: "capS3root",
secretKey: "capS3root",
bucket: "capso",
region: "us-east-1",
endpoint: "http://localhost:9000",
};
const DOCKER_DB_ENVS = {
url: "mysql://root:@localhost:3306/planetscale",
};
async function main() {
intro("Welcome to the Cap env setup CLI!");
const targets = await multiselect({
message: "Which apps will you be working on?",
options: [
{ value: "desktop", label: "Desktop" },
{ value: "web", label: "Web" },
],
required: true,
});
if (isCancel(targets)) return;
const file = await fs
.readFile("./target/env-profiles/default.json", "utf8")
.catch(() => null);
let allEnvs = file ? JSON.parse(file) : {};
let envs = {
NODE_ENV: "development",
};
const hasWeb = targets.includes("web");
const hasDesktop = targets.includes("desktop");
/** @type {boolean | symbol} */
let usingDockerEnvironment = false;
if (hasWeb) {
envs.VITE_SERVER_URL = "http://localhost:3000";
envs.WEB_URL = "http://localhost:3000";
envs.NEXTAUTH_URL = envs.WEB_URL;
envs.WORKFLOWS_RPC_SECRET = crypto.randomBytes(32).toString("base64");
if (!allEnvs.NEXTAUTH_SECRET) {
allEnvs.NEXTAUTH_SECRET = crypto.randomBytes(32).toString("base64");
log.info("Generated NEXTAUTH_SECRET");
}
envs.NEXTAUTH_SECRET = allEnvs.NEXTAUTH_SECRET;
if (!allEnvs.DATABASE_ENCRYPTION_KEY) {
allEnvs.DATABASE_ENCRYPTION_KEY = crypto.randomBytes(32).toString("hex");
log.info("Generated DATABASE_ENCRYPTION_KEY");
}
envs.DATABASE_ENCRYPTION_KEY = allEnvs.DATABASE_ENCRYPTION_KEY;
usingDockerEnvironment = await confirm({
message: "Will you be running S3 and MySQL via Docker?",
});
if (isCancel(usingDockerEnvironment)) return;
if (!usingDockerEnvironment) {
log.info("Database Envs");
const dbValues = await group({
DATABASE_URL: () =>
text({
message:
"DATABASE_URL - Can be plain mysql:// or a PlanetScale https:// URL",
placeholder:
allEnvs.DATABASE_URL ??
"mysql://root:@localhost:3306/planetscale",
defaultValue:
allEnvs.DATABASE_URL ??
"mysql://root:@localhost:3306/planetscale",
}),
});
envs.DATABASE_URL = dbValues.DATABASE_URL;
log.info("S3 Envs");
const s3Values = await group(
{
CAP_AWS_ACCESS_KEY: () =>
text({
message: "CAP_AWS_ACCESS_KEY",
placeholder: allEnvs.CAP_AWS_ACCESS_KEY,
defaultValue: allEnvs.CAP_AWS_ACCESS_KEY,
}),
CAP_AWS_SECRET_KEY: () =>
text({
message: "CAP_AWS_SECRET_KEY",
placeholder: allEnvs.CAP_AWS_SECRET_KEY,
defaultValue: allEnvs.CAP_AWS_SECRET_KEY,
}),
CAP_AWS_BUCKET: () =>
text({
message: "CAP_AWS_BUCKET",
defaultValue: allEnvs.CAP_AWS_BUCKET,
placeholder: allEnvs.CAP_AWS_BUCKET,
}),
CAP_AWS_BUCKET_URL: () => text({ message: "CAP_AWS_BUCKET_URL" }),
CAP_CLOUDFRONT_DISTRIBUTION_ID: () =>
text({ message: "CAP_CLOUDFRONT_DISTRIBUTION_ID" }),
},
{ onCancel: () => process.exit(0) },
);
envs = { ...envs, ...s3Values };
} else {
envs.DATABASE_URL = DOCKER_DB_ENVS.url;
envs.CAP_AWS_ACCESS_KEY = DOCKER_S3_ENVS.accessKey;
envs.CAP_AWS_SECRET_KEY = DOCKER_S3_ENVS.secretKey;
envs.CAP_AWS_BUCKET = DOCKER_S3_ENVS.bucket;
envs.CAP_AWS_REGION = DOCKER_S3_ENVS.region;
envs.CAP_AWS_ENDPOINT = DOCKER_S3_ENVS.endpoint;
}
envs.NEXT_PUBLIC_WEB_URL = envs.WEB_URL;
if (!allEnvs.TINYBIRD_HOST) {
allEnvs.TINYBIRD_HOST = "https://api.tinybird.co";
}
envs.TINYBIRD_HOST = allEnvs.TINYBIRD_HOST;
if (!allEnvs.TINYBIRD_TOKEN) {
allEnvs.TINYBIRD_TOKEN = "";
}
envs.TINYBIRD_TOKEN = allEnvs.TINYBIRD_TOKEN;
}
if (hasDesktop) {
envs.RUST_BACKTRACE = "1";
const values = await group(
{
VITE_SERVER_URL: () => {
if (!hasWeb)
return text({
message: "VITE_SERVER_URL",
placeholder: "https://cap.so",
defaultValue: "https://cap.so",
});
},
VITE_VERCEL_AUTOMATION_BYPASS_SECRET: () => {
if (!hasWeb)
return text({
message:
"VITE_VERCEL_AUTOMATION_BYPASS_SECRET - skip if you're not a Cap team member",
placeholder: allEnvs.VITE_VERCEL_AUTOMATION_BYPASS_SECRET,
defaultValue: allEnvs.VITE_VERCEL_AUTOMATION_BYPASS_SECRET,
});
},
},
{ onCancel: () => process.exit(0) },
);
for (const [key, value] of Object.entries(values)) {
if (value === undefined || value === "undefined") continue;
envs[key] = value;
}
}
await fs.writeFile(
".env",
Object.entries(envs)
.map(([key, value]) => `${key}=${value}`)
.join("\n"),
);
log.info(`Written ${Object.keys(envs).length} envs`);
allEnvs = { ...allEnvs, ...envs };
await fs.mkdir("./target/env-profiles", { recursive: true });
await fs.writeFile(
"./target/env-profiles/default.json",
JSON.stringify(allEnvs, null, 4),
);
const DESKTOP_MSG = "'pnpm dev:desktop' to start the desktop app";
const WEB_DOCKER_MSG =
"'pnpm dev:web' to start the web app + Docker services";
const WEB_MSG = "'pnpm web dev' to start the web app";
if (hasWeb) {
if (hasDesktop) {
if (usingDockerEnvironment) {
outro(`Run ${DESKTOP_MSG}, and ${WEB_DOCKER_MSG}`);
} else {
outro(`Run ${DESKTOP_MSG}, and ${WEB_MSG}`);
}
} else {
if (usingDockerEnvironment) {
outro(`Run ${WEB_DOCKER_MSG}`);
} else {
outro(`Run ${WEB_MSG}`);
}
}
} else if (hasDesktop) {
outro(`Run ${DESKTOP_MSG}`);
}
}
await main();