UNPKG

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