forked from riccardoperra/codeimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-env.ts
More file actions
260 lines (223 loc) · 6.6 KB
/
make-env.ts
File metadata and controls
260 lines (223 loc) · 6.6 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import chalk from 'chalk';
import {prompt} from 'enquirer';
import * as fs from 'fs';
import {execSync} from 'node:child_process';
import path, {join} from 'node:path';
import {makeEnvFile} from './env-utils';
const log = console.log;
const appEnvLocalDir = join(__dirname, '..', 'apps', 'codeimage', '.env.local');
const apiEnvDir = join(__dirname, '..', 'apps', 'api', '.env');
const apiEnvTestDir = join(__dirname, '..', 'apps', 'api', '.env.test');
const runOnCodeSandbox = process.env.RUN_ON_CODESANDBOX;
async function run() {
try {
await buildAppEnvLocal();
await buildApiEnv();
await buildApiTestEnv();
log(chalk.bgGreen('All variables created successfully.'));
await askForPrismaMigrations();
} catch (e) {
process.exit(0);
}
}
async function buildAppEnvLocal() {
log(chalk.cyan('Make variables for @codeimage/app'));
const {exists, override} = await askIfWantOverride(appEnvLocalDir);
if (exists && !override) {
return;
}
const env = {
// Mocks
VITE_ENABLE_MSW: true,
VITE_MOCK_AUTH: true,
// Api
VITE_API_BASE_URL: '',
// Auth0,
VITE_PUBLIC_AUTH0_DOMAIN: '',
VITE_PUBLIC_AUTH0_CLIENT_ID: '',
VITE_PUBLIC_MY_CALLBACK_URL: '',
VITE_PUBLIC_AUTH0_AUDIENCE: '',
};
writeEnv(env, appEnvLocalDir);
if (exists && override) {
await askForBackup(appEnvLocalDir);
}
}
async function buildApiEnv() {
log(chalk.cyan('Make variables for @codeimage/api'));
const {exists, override} = await askIfWantOverride(apiEnvDir);
if (exists && !override) {
return;
}
const defaultDatabase =
'postgres://postgres:postgres@localhost:5432/codeimage?schema=public';
const env = {
DATABASE_URL: defaultDatabase,
CLIENT_ID_AUTH0: 'clientId',
CLIENT_SECRET_AUTH0: 'clientSecret',
DOMAIN_AUTH0: 'dev',
AUTH0_CLIENT_CLAIMS: 'https://example.com/',
AUDIENCE_AUTH0: 'https://example.com/',
GRANT_TYPE_AUTH0: 'client_credentials',
MOCK_AUTH: true,
MOCK_AUTH_EMAIL: 'dev@example.it',
ALLOWED_ORIGINS: '*',
};
if (!runOnCodeSandbox) {
const {dbUrl} = await prompt<{dbUrl: string}>({
type: 'input',
initial: defaultDatabase,
required: true,
name: 'dbUrl',
message: 'Please provide a db url connection (postgres)',
});
if (dbUrl === defaultDatabase) {
log(
chalk.yellow(
'You are using the default url connection. Make sure to run the docker-compose.dev.yml or to have an existing postgres container.',
),
);
} else {
log(
chalk.yellow(
'You are using a custom db url connection. Make sure your db is running and reachable',
),
);
}
const {mockAuth} = await prompt<{mockAuth: boolean}>({
type: 'confirm',
initial: true,
name: 'mockAuth',
message: 'Do you want to mock Auth0? (recommended)',
});
if (!mockAuth) {
log(
chalk.yellow(
'Auth0 will not be mocked. Follow the /docs/auth0.md guide to configure the authentication flow',
),
);
}
env.MOCK_AUTH = mockAuth ?? true;
env.DATABASE_URL = dbUrl ?? defaultDatabase;
}
writeEnv(env, apiEnvDir);
if (exists && override) {
await askForBackup(apiEnvDir);
}
}
async function buildApiTestEnv() {
const {exists, override} = await askIfWantOverride(apiEnvTestDir);
if (exists && !override) {
return;
}
const defaultDatabase =
'postgresql://postgres:postgres@localhost:5433/codeimage_test';
const env = {
DATABASE_URL: defaultDatabase,
CLIENT_ID_AUTH0: '<client-id-auth>',
CLIENT_SECRET_AUTH0: '<client-secret-auth>',
DOMAIN_AUTH0: 'https://example.com',
AUTH0_CLIENT_CLAIMS: 'https://example.com',
AUDIENCE_AUTH0: '<audience>',
GRANT_TYPE_AUTH0: 'client_credentials',
MOCK_AUTH: false,
MOCK_AUTH_EMAIL: 'dev@example.it',
ALLOWED_ORIGINS: '*',
};
if (!runOnCodeSandbox) {
const {dbUrl} = await prompt<{dbUrl: string}>({
type: 'input',
initial: defaultDatabase,
required: true,
name: 'dbUrl',
message: 'Please provide a test db url connection (postgres)',
});
if (dbUrl === defaultDatabase) {
log(
chalk.green(
'You are using the default url connection. Make sure to run the docker-compose.dev.yml or to have an existing postgres container.',
),
);
} else {
log(
chalk.yellow(
'You are using a custom db url connection. Make sure your db is running and reachable',
),
);
}
env.DATABASE_URL = dbUrl ?? defaultDatabase;
}
writeEnv(env, apiEnvTestDir);
if (exists && override) {
await askForBackup(apiEnvTestDir);
}
}
async function askForPrismaMigrations() {
let runMigrations = true;
if (!runOnCodeSandbox) {
const data = await prompt<{runMigrations: boolean}>({
type: 'confirm',
name: 'runMigrations',
message: 'Do you want to run prisma migrations? (recommended)',
initial: true,
});
runMigrations = data.runMigrations;
}
if (runMigrations) {
execSync('pnpm --filter=@codeimage/api prisma:migrate:deploy', {
stdio: 'inherit',
});
execSync('pnpm --filter=@codeimage/api prisma:migrate:deploy-test', {
stdio: 'inherit',
});
execSync('pnpm --filter=@codeimage/api prisma:generate', {
stdio: 'inherit',
});
}
}
function writeEnv(env: Record<string, string | number | boolean>, dir: string) {
fs.writeFileSync(dir, makeEnvFile(env), {encoding: 'utf8'});
const message = `Variables created successfully in ${dir}`;
log(chalk.bgCyan(message));
log(chalk(JSON.stringify(env, undefined, 2)));
}
async function askForBackup(dir: string) {
if (runOnCodeSandbox || !fs.existsSync(dir)) {
return;
}
const {backup} = await prompt<{backup: boolean}>({
name: 'backup',
type: 'confirm',
message: `Do you want to make a backup of your previous configuration?`,
});
if (!backup) return;
const dirName = path.parse(dir).dir;
fs.copyFileSync(dir, `${dirName}/${path.basename(dir)}.backup`);
}
async function askIfWantOverride(
filePath: string,
): Promise<{override: boolean; exists: boolean}> {
if (runOnCodeSandbox) {
return {
exists: false,
override: true,
};
}
if (fs.existsSync(filePath)) {
const fileName = path.basename(filePath);
const result = await prompt<{override: boolean}>({
name: 'override',
type: 'confirm',
message: `Do you want to override existing ${fileName}? Your configuration will be reset.`,
});
return {
exists: true,
override: result.override,
};
}
return {
exists: false,
override: true,
};
}
run();