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']} --upload-dir ${argv.uploadDir}`]
|
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. If you do not provide a database file, then default data will be used. (See the "data" command.)',
|
31 | type: 'string'
|
32 | })
|
33 | .option('upload-dir', {
|
34 | alias: 'u',
|
35 | describe: 'Specify a path to an uploaded file directory. This folder will be served statically and will receive any file uploads from the application. Make sure it is a proper relative path (starting with ./ or ../) or absolute path.',
|
36 | type: 'string',
|
37 | default: './uploads'
|
38 | })
|
39 | .option('main-port', {
|
40 | describe: 'specify a port for the main server',
|
41 | type: 'number',
|
42 | default: 3000
|
43 | })
|
44 | .option('studio-port', {
|
45 | describe: 'specify a port',
|
46 | type: 'number',
|
47 | default: 4201
|
48 | })
|
49 | .option('report-endpoint', {
|
50 | 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.',
|
51 | type: 'string',
|
52 | defaultDescription: `http://${thisIpAddress}:3000/studiomachines/report`
|
53 | })
|
54 | .command(
|
55 | 'main',
|
56 | 'Starts the mock server for the main site',
|
57 | (yargs) => {},
|
58 | (argv) => startMainMockServer(argv)
|
59 | )
|
60 | .command(
|
61 | 'studio',
|
62 | 'starts the mock server for the local studio',
|
63 | (yargs) => {},
|
64 | (argv) => startStudioMockServer(argv)
|
65 | )
|
66 | .command(
|
67 | 'both',
|
68 | 'starts the mock servers for the local studio and the main studio',
|
69 | (yargs) => {},
|
70 | (argv) => {
|
71 | startMainMockServer(argv)
|
72 | setTimeout(() => startStudioMockServer(argv), 1000) // making sure the other is started
|
73 | }
|
74 | )
|
75 | .command(
|
76 | 'data',
|
77 | 'Echoes the database file so you can have a starting point to create your own. Perhaps you should redirect output into a file using ">".',
|
78 | (yargs) => {},
|
79 | (argv) => {
|
80 | const cat = shell.cat(`${getFilePath('db.js')}`).stdout
|
81 | console.log(cat)
|
82 | }
|
83 | )
|
84 | .demandCommand(1, 'must provide a valid command')
|
85 | .help('h')
|
86 | .alias('h', 'help')
|
87 | .argv
|