Repository navigation
Expand file tree
/
Copy pathdev.mjs
More file actions
84 lines (75 loc) · 2.71 KB
/
Copy pathdev.mjs
File metadata and controls
84 lines (75 loc) · 2.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
import { spawn, spawnSync } from 'node:child_process'
import { existsSync } from 'node:fs'
import { homedir } from 'node:os'
import { delimiter, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const backendDir = fileURLToPath(new URL('./backend/', import.meta.url))
const frontendDir = fileURLToPath(new URL('./frontend/', import.meta.url))
const vite = join(frontendDir, 'node_modules', 'vite', 'bin', 'vite.js')
const isWindows = process.platform === 'win32'
const services = []
let stopping = false
function stop(exitCode) {
if (stopping) return
stopping = true
process.exitCode = exitCode
for (const child of services) {
if (!child.pid) continue
if (isWindows) {
// Stop the whole tree, including Uvicorn's reload worker.
spawnSync('taskkill.exe', ['/pid', String(child.pid), '/t', '/f'], {
stdio: 'ignore',
windowsHide: true,
})
} else {
try {
process.kill(-child.pid, 'SIGTERM')
} catch (error) {
if (error.code !== 'ESRCH') throw error
}
}
}
}
function start(name, command, args, cwd, env = process.env) {
const child = spawn(command, args, {
cwd,
env,
detached: !isWindows,
stdio: ['ignore', 'inherit', 'inherit'],
})
services.push(child)
child.once('error', (error) => {
console.error(`${name} could not start. ${error.message}`)
if (name === 'Backend' && error.code === 'ENOENT') {
console.error('Install uv and add it to your PATH, then try again.')
}
stop(1)
})
child.once('exit', (code, signal) => {
if (stopping) return
console.error(`${name} stopped (${signal ?? `exit ${code}`}). Stopping the dev servers.`)
stop(code || 1)
})
}
if (!existsSync(vite)) {
console.error('Install frontend dependencies with npm ci --prefix frontend, then try again.')
process.exitCode = 1
} else {
process.on('SIGINT', () => stop(0))
process.on('SIGTERM', () => stop(0))
const backendEnv = { ...process.env }
if (isWindows) {
// uv's standalone installer may place it here before PATH is refreshed.
const pathKey = Object.keys(backendEnv).find((key) => key.toLowerCase() === 'path') ?? 'Path'
backendEnv[pathKey] = [backendEnv[pathKey], join(homedir(), '.local', 'bin')]
.filter(Boolean).join(delimiter)
}
console.log('Starting ResearchOS. Press Ctrl+C to stop both servers.')
console.log('Frontend http://localhost:5173')
console.log('API docs http://127.0.0.1:8000/docs')
start('Backend', isWindows ? 'uv.exe' : 'uv', [
'run', '--locked', 'uvicorn', 'app:app', '--reload', '--reload-exclude', '.venv',
'--port', '8000',
], backendDir, backendEnv)
start('Frontend', process.execPath, [vite, '--port', '5173', '--strictPort'], frontendDir)
}