-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestServer.cjs
More file actions
39 lines (38 loc) · 1.18 KB
/
testServer.cjs
File metadata and controls
39 lines (38 loc) · 1.18 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
const path = require('path')
const childProcess = require('child_process')
let server
module.exports = {
start: async () => new Promise((resolve, reject) => {
console.log('Starting spliffy server')
const timeout = 5_000
const rejectTimeout = setTimeout(() => {
reject(new Error(`Server was not initialized within ${timeout}ms`))
}, timeout)
server = childProcess.spawn('node', [path.resolve(__dirname, 'example', 'serve.mjs')])
server.on('error', err => {
console.log('got error from server', err)
clearTimeout(rejectTimeout)
reject(err)
})
server.on('exit', (code) => {
clearTimeout(rejectTimeout)
if (code === 0) {
resolve()
} else {
reject(new Error('Server exited with status: ' + code))
}
})
server.stdout.setEncoding('utf-8')
server.stdout.on('data', data => {
console.log(data)
if (data.match('Server initialized')) {
clearTimeout(rejectTimeout)
// give it a little extra time to initialize
setTimeout(resolve, 250)
}
})
server.stderr.setEncoding('utf-8')
server.stderr.on('data', console.error)
}),
stop: async () => server.kill()
}