1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const { serve } = require('reserve')
|
6 | const { fromCmdLine } = require('./src/job')
|
7 | const { getOutput } = require('./src/output')
|
8 | const reserveConfigurationFactory = require('./src/reserve')
|
9 | const { execute } = require('./src/tests')
|
10 | const { capabilities } = require('./src/capabilities')
|
11 | const { watch } = require('fs')
|
12 | const { recreateDir } = require('./src/tools')
|
13 |
|
14 | function send (message) {
|
15 | if (process.send) {
|
16 | process.send({
|
17 | pid: process.pid,
|
18 | ...message
|
19 | })
|
20 | }
|
21 | }
|
22 |
|
23 | async function notifyAndExecuteTests (job, output) {
|
24 | send({ msg: 'begin' })
|
25 | try {
|
26 | await execute(job)
|
27 | let status
|
28 | if (job.failed) {
|
29 | status = -1
|
30 | } else {
|
31 | status = 0
|
32 | }
|
33 | send({ msg: 'end', status })
|
34 | } catch (error) {
|
35 | getOutput(job).genericError(error)
|
36 | send({ msg: 'error', error })
|
37 | }
|
38 | }
|
39 |
|
40 | let job
|
41 | let output
|
42 |
|
43 | async function main () {
|
44 | job = fromCmdLine(process.cwd(), process.argv.slice(2))
|
45 | output = getOutput(job)
|
46 | await recreateDir(job.reportDir)
|
47 | if (job.mode === 'capabilities') {
|
48 | output.version()
|
49 | return capabilities(job)
|
50 | }
|
51 | const configuration = await reserveConfigurationFactory(job)
|
52 | const server = serve(configuration)
|
53 | if (job.logServer) {
|
54 | server.on('redirected', output.redirected)
|
55 | }
|
56 | server
|
57 | .on('ready', async ({ url, port }) => {
|
58 | job.port = port
|
59 | send({ msg: 'ready', port: job.port })
|
60 | output.serving(url)
|
61 | if (job.serveOnly) {
|
62 | return
|
63 | }
|
64 | output.reportOnJobProgress()
|
65 | await notifyAndExecuteTests(job)
|
66 | if (job.watch) {
|
67 | delete job.start
|
68 | if (!job.watching) {
|
69 | output.watching(job.webapp)
|
70 | watch(job.webapp, { recursive: true }, (eventType, filename) => {
|
71 | output.changeDetected(eventType, filename)
|
72 | if (!job.start) {
|
73 | notifyAndExecuteTests(job)
|
74 | }
|
75 | })
|
76 | job.watching = true
|
77 | }
|
78 | } else if (job.keepAlive) {
|
79 | job.status = 'Serving'
|
80 | } else if (job.failed) {
|
81 | output.stop()
|
82 | process.exit(-1)
|
83 | } else {
|
84 | output.stop()
|
85 | process.exit(0)
|
86 | }
|
87 | })
|
88 | .on('error', error => {
|
89 | output.serverError(error)
|
90 | send({ msg: 'error', error })
|
91 | })
|
92 | }
|
93 |
|
94 | main()
|
95 | .catch(reason => {
|
96 | if (output) {
|
97 | output.genericError(reason)
|
98 | output.stop()
|
99 | } else if (reason.name !== 'CommanderError') {
|
100 | console.error(reason)
|
101 | }
|
102 | process.exit(-1)
|
103 | })
|