1 |
|
2 | const yargs = require('yargs');
|
3 | const path = require('path');
|
4 | const run = require('supervisor/lib/supervisor').run
|
5 |
|
6 | const argv = yargs.usage(`
|
7 | Usage: mock [options] -d directory \n
|
8 | mock [options] -c ./config.json \n
|
9 | `)
|
10 | .alias('d', 'directory')
|
11 | .alias('c', 'config')
|
12 | .alias('w', 'watch')
|
13 | .alias('p', 'port')
|
14 | .alias('h', 'host')
|
15 | .alias('s', 'socket')
|
16 | .describe('d', 'directory of data')
|
17 | .describe('c', 'config file of mock')
|
18 | .describe('w',
|
19 | 'when a change to a js file or json file occurs, reload the server. Default is false'
|
20 | )
|
21 | .describe('p', 'port of mock serve')
|
22 | .describe('h', 'host of mock serve')
|
23 | .describe('s', 'config of socket')
|
24 | .example(`
|
25 | mock -w -p 80 -h 127.0.0.1 -d ./model\n
|
26 | mock -c ./config.json
|
27 | `)
|
28 | .epilog('By Newbility')
|
29 | .argv
|
30 |
|
31 | let args = [],
|
32 | configPath = argv.c || argv.config,
|
33 | config = {},
|
34 | port = argv.p || argv.port,
|
35 | host = argv.h || argv.host,
|
36 | isWatch = argv.w || argv.watch,
|
37 | modelDir = argv.d || argv.directory ? path.resolve(argv.d || argv.directory) : null
|
38 | server = path.resolve(__dirname, './lib/index.js')
|
39 |
|
40 | if(configPath) {
|
41 | configPath = path.resolve(process.cwd(), configPath);
|
42 | config = require(configPath);
|
43 | isWatch = config.watch;
|
44 | modelDir = config.directory && path.resolve(path.dirname(configPath), config.directory) || '';
|
45 | if (isWatch && modelDir) {
|
46 | args = args.concat(['-w', modelDir, '-e', 'js,json']);
|
47 | }
|
48 | args = args.concat(['--', server, '-c', configPath]);
|
49 | } else {
|
50 | isWatch && (args = args.concat(['-w', modelDir, '-e', 'js,json']));
|
51 | args = args.concat(['--', server]);
|
52 | port && (args = args.concat(['-p', port]));
|
53 | host && (args = args.concat(['-h', host]));
|
54 | modelDir && (args = args.concat(['-d', modelDir]));
|
55 | }
|
56 |
|
57 | run(args)
|