From 9d619e909d7d06cc8e63aa83d251fb76d25dd9df Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Fri, 27 Sep 2024 21:19:56 +0200 Subject: [PATCH] Move the option defaulting into the main script --- bin/server.js | 57 +++++++++++++++++++++++++++------------------- src/setup/setup.js | 6 ----- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/bin/server.js b/bin/server.js index 218aff4..2293d88 100644 --- a/bin/server.js +++ b/bin/server.js @@ -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'); @@ -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(); diff --git a/src/setup/setup.js b/src/setup/setup.js index e76eea0..ef8d682 100644 --- a/src/setup/setup.js +++ b/src/setup/setup.js @@ -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'; @@ -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();