-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathdev.ts
More file actions
324 lines (266 loc) · 7.71 KB
/
dev.ts
File metadata and controls
324 lines (266 loc) · 7.71 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env bun
/**
* Start the full development environment
*
* Usage:
* bun dev # Start services + CLI
* bun dev -- --debug # Pass args to CLI
*
* This starts all services (db, studio, sdk, web), waits for them to be ready,
* then runs the CLI in the foreground. When you exit the CLI (Ctrl+C),
* all services are stopped.
*
* For running services without CLI:
* bun start-services # Start services in background
* bun stop-services # Stop services
*/
import { spawn, spawnSync, type ChildProcess } from 'child_process'
import { existsSync, mkdirSync, writeFileSync, readFileSync, unlinkSync, openSync } from 'fs'
import { join, resolve } from 'path'
const PROJECT_ROOT = resolve(import.meta.dir, '..')
const LOG_DIR = join(PROJECT_ROOT, 'debug', 'console')
const PID_FILE = join(LOG_DIR, 'services.json')
const BUN_PATH = join(PROJECT_ROOT, '.bin', 'bun')
// Get config from environment (Bun loads .env files automatically)
const APP_URL = process.env.NEXT_PUBLIC_CODEBUFF_APP_URL || 'http://localhost:3000'
const PORT = process.env.NEXT_PUBLIC_WEB_PORT || '3000'
const SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
interface ServicePids {
studio?: number
sdk?: number
web?: number
port: string
}
// Track spawned processes for cleanup
let servicePids: ServicePids = { port: PORT }
let cliProcess: ChildProcess | null = null
let isShuttingDown = false
function ensureLogDir(): void {
if (!existsSync(LOG_DIR)) {
mkdirSync(LOG_DIR, { recursive: true })
}
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
function ok(name: string, message: string): void {
console.log(` \x1b[32m✓\x1b[0m ${name.padEnd(10)} ${message}`)
}
function fail(name: string, message: string): void {
console.log(` \x1b[31m✗\x1b[0m ${name.padEnd(10)} ${message}`)
}
function killPid(pid: number): boolean {
try {
process.kill(pid, 0)
process.kill(pid, 'SIGTERM')
return true
} catch {
return false
}
}
function killProcessesOnPort(port: string): void {
try {
const result = spawnSync('lsof', ['-ti', `:${port}`], { encoding: 'utf-8' })
if (result.stdout) {
const pids = result.stdout.trim().split('\n').filter(Boolean)
for (const pidStr of pids) {
const pid = parseInt(pidStr, 10)
if (!isNaN(pid)) killPid(pid)
}
}
} catch {
// Ignore
}
}
async function cleanup(): Promise<void> {
if (isShuttingDown) return
isShuttingDown = true
console.log('')
console.log('Shutting down...')
console.log('')
// Kill CLI first
if (cliProcess && cliProcess.pid) {
killPid(cliProcess.pid)
ok('cli', 'stopped')
}
// Kill services
if (servicePids.web) {
killPid(servicePids.web)
ok('web', 'stopped')
}
if (servicePids.studio) {
killPid(servicePids.studio)
ok('studio', 'stopped')
}
if (servicePids.sdk) {
killPid(servicePids.sdk)
ok('sdk', 'stopped')
}
// Kill by port as fallback
killProcessesOnPort(servicePids.port)
// Kill drizzle studio
spawnSync('pkill', ['-f', `drizzle-kit.*${PROJECT_ROOT}`])
// Clean up PID file
try {
unlinkSync(PID_FILE)
} catch {
// Ignore
}
console.log('')
}
async function killExistingServices(): Promise<void> {
if (!existsSync(PID_FILE)) return
try {
const existing: ServicePids = JSON.parse(readFileSync(PID_FILE, 'utf-8'))
if (existing.web) killPid(existing.web)
if (existing.studio) killPid(existing.studio)
if (existing.sdk) killPid(existing.sdk)
killProcessesOnPort(existing.port)
unlinkSync(PID_FILE)
} catch {
// Ignore
}
await sleep(500)
}
function startDb(): boolean {
console.log('')
process.stdout.write(` ${SPINNER[0]} db starting...\r`)
const logFile = openSync(join(LOG_DIR, 'db.log'), 'w')
const result = spawnSync(BUN_PATH, ['--cwd', 'packages/internal', 'db:start'], {
cwd: PROJECT_ROOT,
stdio: ['ignore', logFile, logFile],
env: process.env,
})
if (result.status !== 0) {
fail('db', 'failed to start')
console.log(` Check logs: tail -f ${join(LOG_DIR, 'db.log')}`)
return false
}
ok('db', 'ready!')
return true
}
function spawnBackgroundProcess(
command: string,
args: string[],
logFileName: string,
): ChildProcess {
const logFile = openSync(join(LOG_DIR, logFileName), 'w')
const child = spawn(command, args, {
cwd: PROJECT_ROOT,
detached: true,
stdio: ['ignore', logFile, logFile],
env: process.env,
})
child.unref()
return child
}
function startBackgroundServices(): void {
// Start SDK build
const sdk = spawnBackgroundProcess(BUN_PATH, ['run', '--cwd', 'sdk', 'build'], 'sdk.log')
if (sdk.pid) servicePids.sdk = sdk.pid
ok('sdk', '(building)')
// Start Drizzle Studio
const studio = spawnBackgroundProcess(
BUN_PATH,
['--cwd', 'packages/internal', 'db:studio'],
'studio.log',
)
if (studio.pid) servicePids.studio = studio.pid
ok('studio', '(background)')
// Kill any existing web server on this port
killProcessesOnPort(PORT)
// Start web server
const web = spawnBackgroundProcess(BUN_PATH, ['--cwd', 'web', 'dev'], 'web.log')
if (web.pid) servicePids.web = web.pid
}
async function checkHealth(): Promise<boolean> {
try {
const response = await fetch(`${APP_URL}/api/healthz`)
return response.ok
} catch {
return false
}
}
async function waitForHealth(timeoutSeconds: number = 60): Promise<boolean> {
const startTime = Date.now()
const timeoutMs = timeoutSeconds * 1000
let frame = 0
process.stdout.write('\x1B[?25l') // Hide cursor
while (Date.now() - startTime < timeoutMs) {
const isHealthy = await checkHealth()
if (isHealthy) {
process.stdout.write('\r\x1B[K')
process.stdout.write('\x1B[?25h')
ok('web', 'ready!')
return true
}
process.stdout.write(`\r ${SPINNER[frame]} web starting...`)
frame = (frame + 1) % SPINNER.length
await sleep(500)
}
process.stdout.write('\r\x1B[K')
process.stdout.write('\x1B[?25h')
fail('web', 'timeout')
return false
}
function startCli(args: string[]): Promise<number> {
return new Promise((resolve) => {
console.log('')
console.log('Starting CLI...')
cliProcess = spawn(BUN_PATH, ['--cwd', 'cli', 'dev', ...args], {
cwd: PROJECT_ROOT,
stdio: 'inherit',
env: process.env,
})
cliProcess.on('close', (code) => {
resolve(code || 0)
})
cliProcess.on('error', (error) => {
console.error('Failed to start CLI:', error)
resolve(1)
})
})
}
async function main(): Promise<void> {
// Get CLI args (everything after --)
const args = process.argv.slice(2)
ensureLogDir()
// Set up signal handlers for cleanup
process.on('SIGINT', async () => {
await cleanup()
process.exit(0)
})
process.on('SIGTERM', async () => {
await cleanup()
process.exit(0)
})
console.log('Starting development environment...')
// Kill any existing services first
await killExistingServices()
// Start database (blocking)
if (!startDb()) {
process.exit(1)
}
// Start background services
startBackgroundServices()
// Save PIDs for stop-services (in case CLI crashes)
writeFileSync(PID_FILE, JSON.stringify(servicePids, null, 2))
// Wait for web to be healthy
const healthy = await waitForHealth(60)
if (!healthy) {
console.log('')
console.log(` Check logs: tail -f ${join(LOG_DIR, 'web.log')}`)
await cleanup()
process.exit(1)
}
// Start CLI in foreground
const exitCode = await startCli(args)
// CLI exited, clean up everything
await cleanup()
process.exit(exitCode)
}
main().catch(async (error) => {
console.error('Error:', error)
await cleanup()
process.exit(1)
})