-
Notifications
You must be signed in to change notification settings - Fork 66
/
dev-server.ts
177 lines (158 loc) · 4.6 KB
/
dev-server.ts
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
import express from 'express'
import cors from 'cors'
import fs from 'fs'
import { Server } from 'socket.io'
import { createServer } from 'http'
const app = express()
app.use(cors())
const server = createServer(app)
const port = 4000
const io = new Server(server, {
cors: {
origin: 'http://localhost:5173'
}
})
console.log('Initializing server...')
compileRules()
compilePersonas()
generateMigrationReport()
generateSituationCoverage()
io.on('connection', (_socket) => {
console.log('a user connected')
})
///------------------ Compiles rules on change ---------------------
// TODO:
// - [ ] Add a flag to only watch and compile but don't start the server
// TODO:
// - [ ] could be faster if we track the rules in memory and only compile the ones that changed
const compilationWatcher = fs.watch(
'./data/',
{ recursive: true },
(evt, name) => {
if (name?.includes('.publicodes')) {
console.log(`[rules:watcher] ${evt} ${name}`)
console.log(`[rules:watcher] compiling rules...`)
compileRules()
generateSituationCoverage()
generateMigrationReport()
}
}
)
const personaWatcher = fs.watch('./personas/', (evt, name) => {
if (name?.includes('.yaml')) {
console.log(`[personas:watcher] ${evt} ${name}`)
console.log(`[personas:watcher] compiling personas...`)
compilePersonas()
generateSituationCoverage()
}
})
const migrationWatcher = fs.watch('./migration/migration.yaml', (evt, name) => {
console.log(`[migration:watcher] ${evt} ${name}`)
console.log(`[migration:watcher] report generation...`)
generateMigrationReport()
console.log(`[migration:watcher] report generated`)
})
process.on('SIGINT', () => {
console.log('Caught interrupt signal, closing watchers...')
compilationWatcher.close()
personaWatcher.close()
migrationWatcher.close()
process.exit(0)
})
///-------------------- Dev server for testing personas ----------------
// TODO:
// - [ ] add a way to cancel execution?
app.get('/testPersonas/:version/:persona', (req, res) => {
const persona = req.params.persona
const version = req.params.version
console.log(`get /${version}/${persona}`)
const start = Date.now()
const proc = Bun.spawn(
[
'bun',
'./tests/testPersonas.mjs',
'-p',
persona,
'-v',
version,
'--markdown'
],
{
onExit: async ({ exitCode }) => {
// TODO: find a way to send the error message to the client
if (exitCode !== 0) {
res.send({ error: `Erreur lors de l'exécution du test` })
}
}
}
)
new Response(proc.stdout).text().then((report) => {
const timeElapsed = Date.now() - start
res.send({ timeElapsed: timeElapsed, report })
console.log(`Sent response for /${version}/${persona}`)
})
})
server.listen(port, () => {
console.log(`Server is running on port ${port}`)
})
function compileRules() {
io.emit('compilation-status', {
type: 'compiling',
message: 'Règles en cours de compilation'
})
const proc = Bun.spawn(
['bun', './scripts/rulesToJSON.mjs', '-o', 'FR', '-t', 'fr', '-n'],
{
onExit: async ({ exitCode }) => {
// TODO: find a way to send the error message to the client
if (exitCode !== 0) {
io.emit('compilation-status', {
type: 'error',
message: 'Impossible de compiler les règles'
})
}
}
}
)
new Response(proc.stdout).text().then((stdout) => {
console.log(`[rules:watcher] done:\n${stdout}`)
io.emit('compilation-status', {
type: 'ok',
message: 'Règles mises à jour'
})
})
}
function compilePersonas() {
io.emit('compilation-status', {
type: 'compiling',
message: 'Personas en cours de compilation'
})
const proc = Bun.spawn(['bun', './scripts/personasToJSON.js'], {
onExit: async ({ exitCode }) => {
// TODO: find a way to send the error message to the client
if (exitCode !== 0) {
io.emit('compilation-status', {
type: 'error',
message: 'Impossible de compiler les personas'
})
}
}
})
new Response(proc.stdout).text().then((stdout) => {
console.log(`[personas:watcher] done:\n${stdout}`)
io.emit('compilation-status', {
type: 'ok',
message: 'Personas mis à jour'
})
})
}
function generateSituationCoverage() {
Bun.spawn(['bun', './tests/testSituationCoverage.mjs', '-m'], {
stdout: Bun.file('./quick-doc/reports/situation-coverage.md')
})
}
function generateMigrationReport() {
Bun.spawn(['bun', './scripts/migrationToJSON.mjs', '-m'], {
stdout: Bun.file('./quick-doc/reports/migration-report.md')
})
}