From 04f905a8412efe94bc5e780984f3d8749c3f878a Mon Sep 17 00:00:00 2001 From: waltem01 Date: Thu, 14 Mar 2024 15:25:07 +0100 Subject: [PATCH] dynamically import optional env variables --- Webserver/src/lib/client/matrix.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Webserver/src/lib/client/matrix.ts b/Webserver/src/lib/client/matrix.ts index 8ecaafc..36aef55 100644 --- a/Webserver/src/lib/client/matrix.ts +++ b/Webserver/src/lib/client/matrix.ts @@ -1,9 +1,4 @@ -import { - PUBLIC_LED_CHAIN, - PUBLIC_LED_HEIGHT, - PUBLIC_LED_PARALLEL, - PUBLIC_LED_WIDTH -} from '$env/static/public'; +import * as env from '$env/static/public'; // Interface to represent the entire matrix data export interface Matrix { @@ -27,16 +22,19 @@ export interface MatrixCell { } // Initialize matrix object based on environment variables +// These variables might appear to throw an error if they are not set +// in the project-wide .env file. This is intentional. The optional- +// chaining operator will catch these errors. export function initializeMatrix(): Matrix { - const led_width = Number(PUBLIC_LED_WIDTH ?? '64'); - const led_height = Number(PUBLIC_LED_HEIGHT ?? '64'); + const led_width = Number(env?.PUBLIC_LED_WIDTH ?? '64'); + const led_height = Number(env?.PUBLIC_LED_HEIGHT ?? '64'); - const led_chain = Number(PUBLIC_LED_CHAIN ?? '1'); - const led_parallel = Number(PUBLIC_LED_PARALLEL ?? '1'); + const led_chain = Number(env?.PUBLIC_LED_CHAIN ?? '1'); + const led_parallel = Number(env?.PUBLIC_LED_PARALLEL ?? '1'); return { width: led_width * led_parallel, - height: led_height * led_chain, + height: led_height * led_chain }; }