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 | module.exports = {
|
11 | Request: require('./mock/Request'),
|
12 | Response: require('./mock/Response'),
|
13 | body: require('./body'),
|
14 | capture: require('./capture'),
|
15 | check: require('./configuration').check,
|
16 | interpolate: require('./interpolate'),
|
17 | log,
|
18 | mock: require('./mock'),
|
19 | read,
|
20 | serve
|
21 | }
|
22 |
|
23 |
|
24 | if (require.main === module) {
|
25 | const configurationFileNames = process.argv.reduce((name, parameter) => {
|
26 | if (parameter === '--config') {
|
27 | return false
|
28 | }
|
29 | if (name === false) {
|
30 | return parameter
|
31 | }
|
32 | return name
|
33 | }, '') || 'reserve.json'
|
34 |
|
35 | const verbose = process.argv.includes('--verbose')
|
36 | const silent = process.argv.includes('--silent')
|
37 | configurationFileNames.split(',').forEach(configurationFileName => {
|
38 | read(configurationFileName)
|
39 | .catch(reason => {
|
40 | if (verbose) {
|
41 | console.error(colors.red(reason.toString()))
|
42 | }
|
43 | console.warn(colors.yellow(`'${configurationFileName}' not found or invalid, applying defaults`))
|
44 | return {}
|
45 | })
|
46 | .then(configuration => {
|
47 | let eventEmitter
|
48 | if (silent) {
|
49 | eventEmitter = serve(configuration)
|
50 | } else {
|
51 | eventEmitter = log(serve(configuration), verbose)
|
52 | }
|
53 | eventEmitter.on('ready', () => {
|
54 | if (process.send) {
|
55 | process.send('ready')
|
56 | }
|
57 | })
|
58 | })
|
59 | })
|
60 | }
|