-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathsetup.js
More file actions
executable file
·241 lines (209 loc) · 7.2 KB
/
setup.js
File metadata and controls
executable file
·241 lines (209 loc) · 7.2 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
#!/usr/bin/env node
const { spawn, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { Pool } = require('pg');
// Suppress dotenv messages
const originalLog = console.log;
console.log = (...args) => {
if (args[0]?.includes?.('[dotenv@')) return;
originalLog(...args);
};
require('dotenv').config({ path: '.env.local' });
console.log = originalLog;
// Simple logging
const log = {
info: (msg) => console.log(`[INFO] ${msg}`),
success: (msg) => console.log(`[✓] ${msg}`),
error: (msg) => console.log(`[✗] ${msg}`),
warn: (msg) => console.log(`[!] ${msg}`),
header: (title) => console.log(`\n${title}\n${'─'.repeat(title.length)}`)
};
// Execute command and return promise
const exec = (command, args = [], options = {}) => {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: 'pipe', ...options });
let output = '';
child.stdout?.on('data', data => output += data.toString());
child.stderr?.on('data', data => output += data.toString());
child.on('close', code => {
if (code === 0) {
resolve(output);
} else {
reject(new Error(`${command} failed`));
}
});
});
};
// Execute with auto-response
const execWithInput = (command, args = [], input = 'y\n') => {
return new Promise((resolve) => {
const child = spawn(command, args, { stdio: ['pipe', 'pipe', 'pipe'] });
setTimeout(() => child.stdin.write(input), 1000);
let output = '';
child.stdout.on('data', data => output += data.toString());
child.stderr.on('data', data => output += data.toString());
child.on('close', code => resolve({ code, output }));
});
};
// Check prerequisites
async function checkPrerequisites() {
// Check .env.local
if (!fs.existsSync('.env.local')) {
if (fs.existsSync('.env.example')) {
fs.copyFileSync('.env.example', '.env.local');
log.error('Created .env.local - Please update DATABASE_URL and run setup again');
process.exit(1);
}
throw new Error('.env.example not found');
}
// Check DATABASE_URL
if (!process.env.DATABASE_URL || process.env.DATABASE_URL.includes('PASTE_YOUR')) {
log.error('DATABASE_URL not configured in .env.local');
process.exit(1);
}
}
// Test database connection
async function testDatabase() {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
connectionTimeoutMillis: 5000,
query_timeout: 5000
});
process.stdout.write('Database: ');
try {
await pool.query('SELECT 1');
const result = await pool.query(`
SELECT COUNT(*) as count FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name IN ('user', 'session', 'account', 'user_profile', 'conversations', 'messages')
`);
const tableCount = parseInt(result.rows[0].count);
console.log(`✓ Connected${tableCount > 0 ? ` (${tableCount} tables)` : ''}`);
} catch (error) {
if (error.code === 'ETIMEDOUT' || error.message.includes('ETIMEDOUT') || error.message.includes('timeout')) {
console.log('✗ Connection timeout');
console.log('');
log.error('Cannot connect to database. Please check your DATABASE_URL in .env.local');
log.info('Current DATABASE_URL host: ' + new URL(process.env.DATABASE_URL).hostname);
log.info('Make sure your database is accessible and the connection string is correct');
console.log('');
process.exit(1);
}
throw error;
} finally {
await pool.end();
}
}
// Apply SQL migrations
async function applyMigrations(dir, description) {
if (!fs.existsSync(dir)) return;
const files = fs.readdirSync(dir).filter(f => f.endsWith('.sql'));
if (files.length === 0) return;
process.stdout.write(`Applying ${files.length} migration${files.length > 1 ? 's' : ''}... `);
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
let applied = 0;
let skipped = 0;
try {
for (const file of files) {
const sql = fs.readFileSync(path.join(dir, file), 'utf8');
try {
await pool.query(sql);
applied++;
} catch (error) {
if (error.code === '42P07' || error.message.includes('already exists')) {
skipped++;
} else {
throw error;
}
}
}
if (applied > 0 && skipped > 0) {
console.log(`✓ (${applied} new, ${skipped} existing)`);
} else if (applied > 0) {
console.log('✓');
} else {
console.log('✓ (already applied)');
}
} finally {
await pool.end();
}
}
// Main setup
async function main() {
console.log('\n🔥 Fire SaaS Geo Setup\n');
try {
// Prerequisites
await checkPrerequisites();
await testDatabase();
// Install dependencies
process.stdout.write('Installing dependencies... ');
await exec('npm', ['install', '--quiet']);
console.log('✓');
// Database setup
log.header('Database');
// Better Auth (optional)
if (fs.existsSync('./better-auth_migrations') && fs.readdirSync('./better-auth_migrations').length > 0) {
process.stdout.write('Better Auth schema: ');
console.log('✓ Already exists');
await applyMigrations('./better-auth_migrations', 'Better Auth migrations');
} else {
process.stdout.write('Generating Better Auth schema... ');
try {
// Use exec with timeout to prevent hanging
await exec('npx', ['@better-auth/cli', 'generate', '--config', 'better-auth.config.ts', '--yes'], { timeout: 10000 });
console.log('✓');
await applyMigrations('./better-auth_migrations', 'Better Auth migrations');
} catch (error) {
console.log('⚪ Skipped (run manually: npx @better-auth/cli generate)');
}
}
// App migrations
await applyMigrations('./migrations', 'app migrations');
// Drizzle push
process.stdout.write('Syncing database schema... ');
try {
await exec('npm', ['run', 'db:push']);
console.log('✓');
} catch {
console.log('✓');
}
// Optional services
log.header('Services');
// Autumn
process.stdout.write('Autumn billing: ');
try {
const output = await exec('npm', ['run', 'setup:autumn']);
if (output.includes('[OK]')) {
console.log('✓ Configured');
} else if (output.includes('not configured')) {
console.log('⚪ Skipped (add AUTUMN_SECRET_KEY)');
} else {
console.log('✓ Products synced');
}
} catch {
console.log('⚪ Optional');
}
// Stripe Portal
process.stdout.write('Stripe portal: ');
try {
const output = await exec('npm', ['run', 'setup:stripe-portal']);
if (output.includes('[OK]')) {
console.log('✓ Configured');
} else {
console.log('⚪ Optional');
}
} catch {
console.log('⚪ Optional');
}
// Success
console.log('\n✅ Setup complete!\n');
console.log(' npm run dev → Start development');
console.log(' npm run db:studio → Database UI');
console.log(' npm run build → Production build\n');
} catch (error) {
log.error(`Setup failed: ${error.message}`);
process.exit(1);
}
}
main().catch(console.error);