Repository navigation
Expand file tree
/
Copy pathsetup-dev.mjs
More file actions
79 lines (68 loc) · 2.45 KB
/
Copy pathsetup-dev.mjs
File metadata and controls
79 lines (68 loc) · 2.45 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
#!/usr/bin/env node
import { randomBytes } from 'node:crypto';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
const root = resolve(import.meta.dirname, '..');
const devVarsPath = resolve(root, '.dev.vars');
const examplePath = resolve(root, '.env.example');
const REQUIRED_KEYS = [
'BETTER_AUTH_SECRET',
'GOOGLE_CLIENT_ID',
'GOOGLE_CLIENT_SECRET',
'BETTER_AUTH_URL',
];
function parseEnvFile(contents) {
const values = new Map();
for (const line of contents.split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const idx = trimmed.indexOf('=');
if (idx === -1) continue;
values.set(trimmed.slice(0, idx), trimmed.slice(idx + 1));
}
return values;
}
function serializeEnvFile(values) {
const lines = [
'# Local wrangler dev secrets. Generated/updated by pnpm setup:dev.',
'# Add Google OAuth credentials from Google Cloud Console to sign in locally.',
'',
'NODE_ENV=development',
`BETTER_AUTH_URL=${values.get('BETTER_AUTH_URL') ?? 'http://localhost:8787'}`,
`BETTER_AUTH_SECRET=${values.get('BETTER_AUTH_SECRET') ?? randomBytes(32).toString('base64')}`,
`GOOGLE_CLIENT_ID=${values.get('GOOGLE_CLIENT_ID') ?? ''}`,
`GOOGLE_CLIENT_SECRET=${values.get('GOOGLE_CLIENT_SECRET') ?? ''}`,
'',
];
return `${lines.join('\n')}`;
}
function main() {
const seed = existsSync(devVarsPath)
? parseEnvFile(readFileSync(devVarsPath, 'utf8'))
: existsSync(examplePath)
? parseEnvFile(readFileSync(examplePath, 'utf8'))
: new Map();
if (!seed.get('BETTER_AUTH_SECRET')) {
seed.set('BETTER_AUTH_SECRET', randomBytes(32).toString('base64'));
}
seed.set('BETTER_AUTH_URL', 'http://localhost:8787');
seed.set('NODE_ENV', 'development');
writeFileSync(devVarsPath, serializeEnvFile(seed), 'utf8');
const missing = REQUIRED_KEYS.filter((key) => {
if (key === 'BETTER_AUTH_URL' || key === 'BETTER_AUTH_SECRET') return false;
return !seed.get(key);
});
console.log(`[setup-dev] wrote ${devVarsPath}`);
if (missing.length > 0) {
console.log(
`[setup-dev] still missing: ${missing.join(', ')} — copy values from Google Cloud Console into .dev.vars`
);
console.log(
'[setup-dev] register OAuth redirect: http://localhost:8787/api/auth/callback/google'
);
process.exitCode = 1;
return;
}
console.log('[setup-dev] local auth is ready. Run: pnpm dev');
}
main();