From 8ef57c4400ec871d3b5ffdc19d505ca48fbb1d0f Mon Sep 17 00:00:00 2001 From: Jannis Baum Date: Fri, 19 Jul 2024 13:26:28 +0200 Subject: [PATCH] feat(#111): parse args & print version --- src/app.ts | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/app.ts b/src/app.ts index dc8c9279..4376f3f4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -41,23 +41,39 @@ export const { clientsAt, messageClientsAt } = setupSockets( const address = `http://localhost:${config.port}`; const handleArgs = async () => { - await Promise.all( - process.argv.slice(2).map(async (path) => { - if (path.startsWith('-')) return; - if (!existsSync(path)) { - console.log(`File not found: ${path}`); - return; + try { + const args = process.argv.slice(2); + const options = args.filter((arg) => arg.startsWith('-')); + for (const option of options) { + switch (option) { + case '-v': + case '--version': + console.log(`vivify-server ${process.env.VERSION ?? 'dev'}`); + break; + default: + console.log(`unknown option "${option}"`); } - const target = preferredPath(presolve(path)); - const url = `${address}${pathToURL(target)}`; - await open(url); - }), - ); - 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'); + } + + const paths = args.filter((arg) => !arg.startsWith('-')); + await Promise.all( + paths.map(async (path) => { + if (!existsSync(path)) { + console.log(`File not found: ${path}`); + return; + } + const target = preferredPath(presolve(path)); + const url = `${address}${pathToURL(target)}`; + await open(url); + }), + ); + } 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'); + } } };