From ac888d5aa19e3d447602807382f59f39088e13f8 Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Tue, 6 Aug 2024 07:58:47 +0200 Subject: [PATCH] feat(#159): always output version & complete startup --- src/app.ts | 28 +++++++++++++++++-------- src/cli.ts | 61 ++++++++++++++++++++++++++++-------------------------- viv | 2 +- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/app.ts b/src/app.ts index e4b92a9f..b44cfdc1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,7 +9,7 @@ import { router as viewerRouter } from './routes/viewer.js'; import { router as openRouter } from './routes/_open.js'; import { setupSockets } from './sockets.js'; import { urlToPath } from './utils/path.js'; -import { handleArgs } from './cli.js'; +import { completeStartup, handleArgs } from './cli.js'; const app = express(); app.use(express.json()); @@ -39,11 +39,21 @@ export const { clientsAt, messageClients, openAndMessage } = setupSockets( }, ); -get(`${address}/health`, async () => { - // server is already running - await handleArgs(); - process.exit(0); -}).on('error', () => { - // server is not running so we start it - server.listen(config.port, handleArgs); -}); +const openTargetsAndCompleteStartup = handleArgs(); +if (openTargetsAndCompleteStartup) { + try { + get(`${address}/health`, async () => { + // server is already running + await openTargetsAndCompleteStartup(); + process.exit(0); + }).on('error', () => { + // server is not running so we start it + server.listen(config.port, openTargetsAndCompleteStartup); + }); + } catch (error) { + console.log(error); + completeStartup(); + } +} else { + completeStartup(); +} diff --git a/src/cli.ts b/src/cli.ts index 176fb03f..1d19e4bd 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -49,37 +49,40 @@ const openTarget = async (target: string) => { } }; -export const handleArgs = async () => { - try { - const args = process.argv.slice(2); - const positionals: string[] = []; - let parseOptions = true; +export const completeStartup = () => { + if (process.env['NODE_ENV'] !== 'development') { + // - viv executable waits for this string and then stops printing + // vivify-server's output and terminates + // - the string itself is not shown to the user + console.log('STARTUP COMPLETE'); + } +}; - for (let i = 0; i < args.length; i++) { - const arg = args[i]; - if (!(arg.startsWith('-') && parseOptions)) { - positionals.push(arg); - continue; - } - switch (arg) { - case '-v': - case '--version': - console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`); - break; - case '--': - parseOptions = false; - break; - default: - console.log(`Unknown option "${arg}"`); - } +export const handleArgs = (): (() => Promise) | undefined => { + const args = process.argv.slice(2); + const positionals: string[] = []; + let parseOptions = true; + + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (!(arg.startsWith('-') && parseOptions)) { + positionals.push(arg); + continue; } - await Promise.all(positionals.map((target) => openTarget(target))); - } finally { - if (process.env['NODE_ENV'] !== 'development') { - // - viv executable waits for this string and then stops printing - // vivify-server's output and terminates - // - the string itself is not shown to the user - console.log('STARTUP COMPLETE'); + switch (arg) { + case '-v': + case '--version': + console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`); + return; + case '--': + parseOptions = false; + break; + default: + console.log(`Unknown option "${arg}"`); } } + return async () => { + await Promise.all(positionals.map((target) => openTarget(target))); + completeStartup(); + }; }; diff --git a/viv b/viv index 2730aec7..05a6a44d 100755 --- a/viv +++ b/viv @@ -12,7 +12,7 @@ arguments: source file options: --help show this help message and exit - --version show version information + --version show version information and exit EOF }