-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·69 lines (57 loc) · 1.86 KB
/
index.js
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
64
65
66
67
68
69
#! /usr/bin/env node
const program = require('commander')
const path = require('path')
const pkg = require('./package')
const mock = require('./mock')
const verify = require('./verify')
const fs = require('fs')
const { dir } = require('./contract-loader')
const server = (contract, options) =>
mock(path.resolve(contract), options)
const logContractDir = dir => {
console.log(`watching for contract changes: ${dir}`)
return dir
}
const serverInWatchMode = (contract, options) => async (dir) => {
let srv = await server(contract, options)
fs.watch(dir, {recursive: true}, async (_, filename) => {
console.log(`${filename} changed, restarting...`)
await srv.stop()
srv = await server(contract, options)
})
}
// TODO: refactor this to functional code - so that server doesn't get mutated
const watch = (contract, options) => {
dir(contract)
.then(logContractDir)
.then(serverInWatchMode(contract, options))
}
const mockServer = (contract, options) => {
if (options.watch) {
return watch(contract, options)
}
server(contract, options)
}
program
.version(pkg.version)
program
.command('mock [contract]')
.option('-p, --port <port>', 'Port for running mock server. Defaults to 3000.')
.option('-w, --watch', 'Watch for changes.')
.option('-t, --tls', 'Use TLS. Defaults to false.')
.option('-C, --no-cors', 'Disable CORS support.')
.description('Provide mock responses for matching consumer requests.')
.action((contract = './contracts', options) => {
mockServer(contract, options)
})
program
.command('verify [contract]')
.option('-b, --baseUrl <port>', 'Base url to run verifications against.')
.description('Verify contracts against provider.')
.action((contract = './contracts', options) => {
verify(contract, options.baseUrl)
})
program.parse(process.argv)
if (program.args.length === 0) {
program.help()
}