#! /usr/bin/env node const { join } = require('path') const shell = require('shelljs') const yargs = require('yargs') const getFilePath = (file) => join(__dirname, '..', file) const thisIpAddress = require(getFilePath('./ipAddressHelpers')).getMyIpAddress() function startMainMockServer (argv) { const commandString = [`node "${getFilePath('server.js')}" --port ${argv['main-port']} --upload-dir ${argv.uploadDir}`] if (argv.database) commandString.push(`--database ${argv.database}`) shell.exec(commandString.join(' '), {async: true}) } function startStudioMockServer (argv) { const commandString = [`node "${getFilePath('app-server.js')}"`] commandString.push(`--port ${argv['studio-port']}`) const endpoint = argv.reportEndpoint || `http://${thisIpAddress}:${argv['main-port']}/studiomachines/report` commandString.push(`--report-endpoint ${endpoint}`) shell.exec(commandString.join(' '), {async: true}) } yargs // eslint-disable-line no-unused-expressions .usage('$0 command') .option('database', { alias: 'd', 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.)', type: 'string' }) .option('upload-dir', { alias: 'u', 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.', type: 'string', default: './uploads' }) .option('main-port', { describe: 'specify a port for the main server', type: 'number', default: 3000 }) .option('studio-port', { describe: 'specify a port', type: 'number', default: 4201 }) .option('report-endpoint', { 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.', type: 'string', defaultDescription: `http://${thisIpAddress}:3000/studiomachines/report` }) .command( 'main', 'Starts the mock server for the main site', (yargs) => {}, (argv) => startMainMockServer(argv) ) .command( 'studio', 'starts the mock server for the local studio', (yargs) => {}, (argv) => startStudioMockServer(argv) ) .command( 'both', 'starts the mock servers for the local studio and the main studio', (yargs) => {}, (argv) => { startMainMockServer(argv) setTimeout(() => startStudioMockServer(argv), 1000) // making sure the other is started } ) .command( 'data', 'Echoes the database file so you can have a starting point to create your own. Perhaps you should redirect output into a file using ">".', (yargs) => {}, (argv) => { const cat = shell.cat(`${getFilePath('db.js')}`).stdout console.log(cat) } ) .demandCommand(1, 'must provide a valid command') .help('h') .alias('h', 'help') .argv