1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const colors = require('./detect/colors')
|
6 | const { read } = require('./configuration')
|
7 | const log = require('./log')
|
8 | const serve = require('./serve')
|
9 |
|
10 |
|
11 | if (require.main === module) {
|
12 | const configurationFileName = process.argv.reduce((name, parameter) => {
|
13 | if (parameter === '--config') {
|
14 | return false
|
15 | }
|
16 | if (name === false) {
|
17 | return parameter
|
18 | }
|
19 | return name
|
20 | }, '') || 'reserve.json'
|
21 |
|
22 | const verbose = process.argv.includes('--verbose')
|
23 | const silent = process.argv.includes('--silent')
|
24 |
|
25 | read(configurationFileName)
|
26 | .catch(reason => {
|
27 | if (verbose) {
|
28 | console.error(colors.red(reason.toString()))
|
29 | }
|
30 | console.warn(colors.yellow(`'${configurationFileName}' not found or invalid, applying defaults`))
|
31 | return {}
|
32 | })
|
33 | .then(configuration => {
|
34 | let eventEmitter
|
35 | if (silent) {
|
36 | eventEmitter = serve(configuration)
|
37 | } else {
|
38 | eventEmitter = log(serve(configuration), verbose)
|
39 | }
|
40 | eventEmitter.on('ready', () => {
|
41 | if (process.send) {
|
42 | process.send('ready')
|
43 | }
|
44 | })
|
45 | })
|
46 | } else {
|
47 | module.exports = {
|
48 | body: require('./body'),
|
49 | capture: require('./capture'),
|
50 | check: require('./configuration').check,
|
51 | interpolate: require('./interpolate'),
|
52 | log,
|
53 | mock: require('./mock'),
|
54 | read,
|
55 | serve
|
56 | }
|
57 | }
|