aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/servers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/server/servers.ts')
-rw-r--r--shared/extra-utils/server/servers.ts93
1 files changed, 48 insertions, 45 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 5288d253a..b0cb869f4 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -58,18 +58,17 @@ function flushAndRunMultipleServers (totalServers: number, configOverride?: Obje
58 } 58 }
59 } 59 }
60 60
61 flushTests() 61 for (let j = 1; j <= totalServers; j++) {
62 .then(() => { 62 flushAndRunServer(j, configOverride).then(app => anotherServerDone(j, app))
63 for (let j = 1; j <= totalServers; j++) { 63 }
64 runServer(j, configOverride).then(app => anotherServerDone(j, app))
65 }
66 })
67 }) 64 })
68} 65}
69 66
70function flushTests () { 67function flushTests (serverNumber?: number) {
71 return new Promise<void>((res, rej) => { 68 return new Promise<void>((res, rej) => {
72 return exec('npm run clean:server:test', err => { 69 const suffix = serverNumber ? ` -- ${serverNumber}` : ''
70
71 return exec('npm run clean:server:test' + suffix, err => {
73 if (err) return rej(err) 72 if (err) return rej(err)
74 73
75 return res() 74 return res()
@@ -77,7 +76,7 @@ function flushTests () {
77 }) 76 })
78} 77}
79 78
80function runServer (serverNumber: number, configOverride?: Object, args = []) { 79function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) {
81 const server: ServerInfo = { 80 const server: ServerInfo = {
82 app: null, 81 app: null,
83 serverNumber: serverNumber, 82 serverNumber: serverNumber,
@@ -123,47 +122,51 @@ function runServer (serverNumber: number, configOverride?: Object, args = []) {
123 } 122 }
124 123
125 return new Promise<ServerInfo>(res => { 124 return new Promise<ServerInfo>(res => {
126 server.app = fork(join(root(), 'dist', 'server.js'), args, options) 125 flushTests(serverNumber)
127 server.app.stdout.on('data', function onStdout (data) { 126 .then(() => {
128 let dontContinue = false
129
130 // Capture things if we want to
131 for (const key of Object.keys(regexps)) {
132 const regexp = regexps[key]
133 const matches = data.toString().match(regexp)
134 if (matches !== null) {
135 if (key === 'client_id') server.client.id = matches[1]
136 else if (key === 'client_secret') server.client.secret = matches[1]
137 else if (key === 'user_username') server.user.username = matches[1]
138 else if (key === 'user_password') server.user.password = matches[1]
139 }
140 }
141
142 // Check if all required sentences are here
143 for (const key of Object.keys(serverRunString)) {
144 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
145 if (serverRunString[key] === false) dontContinue = true
146 }
147
148 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
149 if (dontContinue === true) return
150
151 server.app.stdout.removeListener('data', onStdout)
152 127
153 process.on('exit', () => { 128 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
154 try { 129 server.app.stdout.on('data', function onStdout (data) {
155 process.kill(server.app.pid) 130 let dontContinue = false
156 } catch { /* empty */ } 131
132 // Capture things if we want to
133 for (const key of Object.keys(regexps)) {
134 const regexp = regexps[ key ]
135 const matches = data.toString().match(regexp)
136 if (matches !== null) {
137 if (key === 'client_id') server.client.id = matches[ 1 ]
138 else if (key === 'client_secret') server.client.secret = matches[ 1 ]
139 else if (key === 'user_username') server.user.username = matches[ 1 ]
140 else if (key === 'user_password') server.user.password = matches[ 1 ]
141 }
142 }
143
144 // Check if all required sentences are here
145 for (const key of Object.keys(serverRunString)) {
146 if (data.toString().indexOf(key) !== -1) serverRunString[ key ] = true
147 if (serverRunString[ key ] === false) dontContinue = true
148 }
149
150 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
151 if (dontContinue === true) return
152
153 server.app.stdout.removeListener('data', onStdout)
154
155 process.on('exit', () => {
156 try {
157 process.kill(server.app.pid)
158 } catch { /* empty */
159 }
160 })
161
162 res(server)
163 })
157 }) 164 })
158
159 res(server)
160 })
161
162 }) 165 })
163} 166}
164 167
165async function reRunServer (server: ServerInfo, configOverride?: any) { 168async function reRunServer (server: ServerInfo, configOverride?: any) {
166 const newServer = await runServer(server.serverNumber, configOverride) 169 const newServer = await flushAndRunServer(server.serverNumber, configOverride)
167 server.app = newServer.app 170 server.app = newServer.app
168 171
169 return server 172 return server
@@ -212,7 +215,7 @@ export {
212 ServerInfo, 215 ServerInfo,
213 flushAndRunMultipleServers, 216 flushAndRunMultipleServers,
214 flushTests, 217 flushTests,
215 runServer, 218 flushAndRunServer,
216 killallServers, 219 killallServers,
217 reRunServer, 220 reRunServer,
218 waitUntilLog 221 waitUntilLog