-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstart.mjs
More file actions
105 lines (89 loc) · 2.91 KB
/
start.mjs
File metadata and controls
105 lines (89 loc) · 2.91 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
#!/usr/bin/env node
/**
* Starts the API server & dashboard UI locally instead of in docker
*
* Set AGENTS_OBSERVE_RUNTIME=local|dev in env or claude settings.json to enable auto start to use this script
* Reads all config from hooks/scripts/lib/config.mjs (central source of truth).
*
* Modes:
* local — install deps, build client, start server (production-like)
* dev — install deps, start server + client with hot reload
*/
import { execFileSync, spawn } from 'node:child_process'
import { resolve } from 'node:path'
import {
getConfig,
getServerEnv,
getClientEnv,
initLocalDataDirs,
} from './hooks/scripts/lib/config.mjs'
import { saveServerPortFile, removeServerPortFile } from './hooks/scripts/lib/fs.mjs'
const config = getConfig()
const serverDir = resolve(config.installDir, 'app/server')
const clientDir = resolve(config.installDir, 'app/client')
const isDev = config.isDevRuntime
function run(cmd, args, cwd, env = {}) {
const rel = cwd.replace(config.installDir + '/', '') || '.'
console.log(`\n> ${cmd} ${args.join(' ')} (in ${rel})`)
execFileSync(cmd, args, { cwd, stdio: 'inherit', env: { ...process.env, ...env } })
}
// 1. Install dependencies
run('npm', ['install'], serverDir)
run('npm', ['install'], clientDir)
// 2. Build client (skip in dev — vite serves it directly)
if (!isDev) {
run('npm', ['run', 'build'], clientDir, getClientEnv(config))
}
// 3. Initialize the local data dirs before starting the server
initLocalDataDirs(config)
// 4. Start server
const serverEnv = getServerEnv(config)
saveServerPortFile(config, config.serverPort)
if (isDev) {
console.log(`\nStarting dev server on http://localhost:${config.serverPort} (API)\n`)
console.log(`Starting dev client on http://localhost:${config.clientPort} (UI + proxy)\n`)
const server = spawn('npm', ['run', 'dev'], {
cwd: serverDir,
stdio: 'inherit',
env: { ...process.env, ...serverEnv },
})
const client = spawn('npm', ['run', 'dev'], {
cwd: clientDir,
stdio: 'inherit',
env: { ...process.env, ...getClientEnv(config) },
})
function cleanup() {
removeServerPortFile(config)
server.kill('SIGINT')
client.kill('SIGINT')
}
server.on('close', (code) => {
removeServerPortFile(config)
client.kill()
process.exit(code ?? 0)
})
client.on('close', () => {
server.kill()
})
process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup)
} else {
console.log(`\nStarting server on http://localhost:${config.serverPort} (API + UI)\n`)
const server = spawn('npx', ['tsx', 'src/index.ts'], {
cwd: serverDir,
stdio: 'inherit',
env: { ...process.env, ...serverEnv },
})
server.on('close', (code) => {
removeServerPortFile(config)
process.exit(code ?? 0)
})
process.on('SIGINT', () => {
removeServerPortFile(config)
server.kill('SIGINT')
})
process.on('SIGTERM', () => {
removeServerPortFile(config)
server.kill('SIGTERM')
})
}