1 | #! /usr/bin/env node
|
2 |
|
3 | const { join } = require('path')
|
4 | const shell = require('shelljs')
|
5 | const yargs = require('yargs')
|
6 | const getFilePath = (file) => join(__dirname, '..', file)
|
7 |
|
8 | const thisIpAddress = require(getFilePath('./ipAddressHelpers')).getMyIpAddress()
|
9 |
|
10 | function startMainMockServer (argv) {
|
11 | const commandString = [`node "${getFilePath('server.js')}" --port ${argv['main-port']}`]
|
12 | if (argv.database) commandString.push(`--database ${argv.database}`)
|
13 | shell.exec(commandString.join(' '), {async: true})
|
14 | }
|
15 |
|
16 | function startStudioMockServer (argv) {
|
17 | const commandString = [`node "${getFilePath('app-server.js')}"`]
|
18 | commandString.push(`--port ${argv['studio-port']}`)
|
19 |
|
20 | const endpoint = argv.reportEndpoint || `http://${thisIpAddress}:${argv['main-port']}/studiomachines/report`
|
21 | commandString.push(`--report-endpoint ${endpoint}`)
|
22 |
|
23 | shell.exec(commandString.join(' '), {async: true})
|
24 | }
|
25 |
|
26 | yargs // eslint-disable-line no-unused-expressions
|
27 | .usage('$0 command')
|
28 | .option('database', {
|
29 | alias: 'd',
|
30 | describe: 'Specify a path to a database file. Make sure it is a proper relative path (starting with ./ or ../) or absolute path.',
|
31 | type: 'string'
|
32 | })
|
33 | .option('main-port', {
|
34 | describe: 'specify a port for the main server',
|
35 | type: 'number',
|
36 | default: 3000
|
37 | })
|
38 | .option('studio-port', {
|
39 | describe: 'specify a port',
|
40 | type: 'number',
|
41 | default: 4201
|
42 | })
|
43 | .option('report-endpoint', {
|
44 | describe: 'Override the remote endpoint for reporting this server\'s IP address/port. This option by default will point to the main mock server, even if you change its port.',
|
45 | type: 'string',
|
46 | defaultDescription: `http://${thisIpAddress}:3000/studiomachines/report`
|
47 | })
|
48 | .command(
|
49 | 'main',
|
50 | 'Starts the mock server for the main site',
|
51 | (yargs) => {},
|
52 | (argv) => startMainMockServer(argv)
|
53 | )
|
54 | .command(
|
55 | 'studio',
|
56 | 'starts the mock server for the local studio',
|
57 | (yargs) => {},
|
58 | (argv) => startStudioMockServer(argv)
|
59 | )
|
60 | .command(
|
61 | 'both',
|
62 | 'starts the mock servers for the local studio and the main studio',
|
63 | (yargs) => {},
|
64 | (argv) => {
|
65 | startMainMockServer(argv)
|
66 | setTimeout(() => startStudioMockServer(argv), 1000) // making sure the other is started
|
67 | }
|
68 | )
|
69 | .command(
|
70 | 'data',
|
71 | 'Echoes the database file so you can have a starting point to create your own. Perhaps you should redirect output into a file using ">".',
|
72 | (yargs) => {},
|
73 | (argv) => {
|
74 | const cat = shell.cat(`${getFilePath('db.js')}`).stdout
|
75 | console.log(cat)
|
76 | }
|
77 | )
|
78 | .demandCommand(1, 'must provide a valid command')
|
79 | .help('h')
|
80 | .alias('h', 'help')
|
81 | .argv
|