-
Notifications
You must be signed in to change notification settings - Fork 16
/
smoke-cli.js
85 lines (75 loc) · 2.62 KB
/
smoke-cli.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const minimist = require('minimist');
const {createServer, startServer} = require('./lib/smoke');
const help = `Usage: smoke [<mocks_folder>] [options]
Base options:
-p, --port <num> Server port [default: 3000]
-h, --host <host> Server host [default: "localhost"]
-s, --set <name> Mocks set to use [default: none]
-n, --not-found <glob> Mocks for 404 errors [default: "404.*"]
-i, --ignore <glob> Files to ignore [default: none]
-k, --hooks <file> Middleware hooks [default: none]
-x, --proxy <host> Fallback proxy if no mock found
-o, --allow-cors [all|<hosts>] Enable CORS requests [default: none]
--https Enable secure request serving with HTTPS [default: false]
-l, --logs Enable server logs
-v, --version Show version
--help Show help
Mock recording:
-r, --record <host> Proxy & record requests if no mock found
-c, --collection <file> Save to single file mock collection
-d, --depth <N> Folder depth for mocks [default: 1]
-a, --save-headers Save response headers
-q, --save-query Save query parameters
`;
function run(args) {
const options = minimist(args, {
number: ['port', 'depth'],
string: ['host', 'set', 'not-found', 'record', 'ignore', 'hooks', 'proxy', 'collection', 'allow-cors'],
boolean: ['help', 'version', 'logs', 'save-headers', 'save-query', 'https'],
alias: {
p: 'port',
h: 'host',
s: 'set',
n: 'not-found',
v: 'version',
r: 'record',
c: 'collection',
d: 'depth',
a: 'save-headers',
q: 'save-query',
i: 'ignore',
k: 'hooks',
x: 'proxy',
o: 'allow-cors'
}
});
if (options.version) {
const pkg = require('./package.json');
return console.log(pkg.version);
}
if (options.help) {
return console.log(help);
}
const app = createServer({
basePath: options._[0],
port: options.port,
host: options.host,
set: options.set,
notFound: options['not-found'],
ignore: options.ignore,
hooks: options.hooks,
proxy: options.proxy,
logs: options.logs,
record: options.record,
collection: options.collection,
depth: options.depth,
https: options.https,
saveHeaders: options['save-headers'],
saveQueryParams: options['save-query'],
cors: options['allow-cors']
});
if (app) {
startServer(app);
}
}
module.exports = run;