aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/e2e/src/utils/server.ts
blob: 7089a5c9cdd127d8a791333f62a4b34cbd194000 (plain) (blame)
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
import { exec, spawn } from 'child_process'
import { join, resolve } from 'path'

function runServer (appInstance: string, config: any = {}) {
  const env = Object.create(process.env)
  env['NODE_ENV'] = 'test'
  env['NODE_APP_INSTANCE'] = appInstance

  env['NODE_CONFIG'] = JSON.stringify({
    rates_limit: {
      api: {
        max: 5000
      },
      login: {
        max: 5000
      }
    },
    log: {
      level: 'warn'
    },
    signup: {
      enabled: false
    },
    transcoding: {
      enabled: false
    },

    ...config
  })

  const forkOptions = {
    env,
    cwd: getRootCWD(),
    detached: false
  }

  const p = spawn('node', [ join('dist', 'server.js') ], forkOptions)
  p.stderr.on('data', data => console.error(data.toString()))
  p.stdout.on('data', data => console.error(data.toString()))

  return p
}

function runCommand (command: string) {
  return new Promise<void>((res, rej) => {
    const p = exec(command, { cwd: getRootCWD() })

    p.stderr.on('data', data => console.error(data.toString()))
    p.on('error', err => rej(err))
    p.on('exit', () => res())
  })
}

export {
  runServer,
  runCommand
}

// ---------------------------------------------------------------------------

function getRootCWD () {
  return resolve('../..')
}