Skip to content

Commit

Permalink
Move the option defaulting into the main script
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennou committed Sep 27, 2024
1 parent 69c7170 commit 9d619e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
57 changes: 34 additions & 23 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { execSync } = require('child_process');
const { join } = require('path');
require('dotenv').config({ path: join(__dirname, '../.env') });

const nodeVersion = process.versions.node;
const nodeVersionMajor = Number(nodeVersion.split('.')[0]);
const { getPort } = nodeVersionMajor >= 14 ? require('get-port-please') : { getPort: async () => 3000 };

const minimist = require('minimist');
const { createLogger, format, transports } = require('winston');

Expand All @@ -27,27 +31,34 @@ const logger = createLogger({
})],
});

const cli = {
cmd: argv._.shift(),
args: argv,
logger,
};

switch (cli.cmd) {
case 'setup':
setup(cli);
break;

case 'start':
start(cli);
break;

case 'stop':
logger.info(`Stopping server from ${process.env.COMPOSE_FILE}`);
execSync('docker-compose stop');
break;

default:
logger.error(`expected command, got "${cli.cmd}"`);
break;
async function main() {
argv.grafanaPort = argv.grafanaPort ?? await getPort({ portRange: [3000, 4000] });
argv.serverPort = argv.serverPort ?? 21025;

const cli = {
cmd: argv._.shift(),
args: argv,
logger,
};

switch (cli.cmd) {
case 'setup':
setup(cli);
break;

case 'start':
start(cli);
break;

case 'stop':
logger.info(`Stopping server from ${process.env.COMPOSE_FILE}`);
execSync('docker-compose stop');
break;

default:
logger.error(`expected command, got "${cli.cmd}"`);
break;
}
}

main();
6 changes: 0 additions & 6 deletions src/setup/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ let argv;
/** @type {import('winston').Logger} */
let logger;

const nodeVersion = process.versions.node;
const nodeVersionMajor = Number(nodeVersion.split('.')[0]);
const { getPort } = nodeVersionMajor >= 14 ? require('get-port-please') : { getPort: async () => 3000 };

const isWindows = process.platform === 'win32';
const regexEscape = isWindows ? '\r\n' : '\n';

Expand Down Expand Up @@ -152,8 +148,6 @@ async function Setup(cli) {
argv = cli.args;
logger = cli.logger;

argv.grafanaPort = argv.grafanaPort ?? await getPort({ portRange: [3000, 4000] });

UpdateUsersFile();
UpdateEnvFile();
await UpdateDockerComposeFile();
Expand Down

0 comments on commit 9d619e9

Please sign in to comment.